Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/Microsoft.OpenApi.OData.Reader/Edm/EdmModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static bool IsAssignableFrom(this IEdmEntityType baseType, IEdmEntityType
}

/// <summary>
/// Check whether the operaiton is overload in the model.
/// Check whether the operation is overload in the model.
/// </summary>
/// <param name="model">The Edm model.</param>
/// <param name="operation">The test operations.</param>
Expand All @@ -172,7 +172,7 @@ public static bool IsOperationOverload(this IEdmModel model, IEdmOperation opera
Utils.CheckArgumentNull(model, nameof(model));
Utils.CheckArgumentNull(operation, nameof(operation));

return model.SchemaElements.OfType<IEdmOperation>()
return model.GetAllElements().OfType<IEdmOperation>()
.Where(o => o.IsBound == operation.IsBound && o.FullName() == operation.FullName() &&
o.Parameters.First().Type.Definition == operation.Parameters.First().Type.Definition
).Count() > 1;
Expand All @@ -197,5 +197,28 @@ public static bool IsOperationImportOverload(this IEdmModel model, IEdmOperation
return model.EntityContainer.OperationImports()
.Where(o => o.Operation.IsBound == operationImport.Operation.IsBound && o.Name == operationImport.Name).Count() > 1;
}

/// <summary>
/// Get all of the elements in the model and its referenced models.
/// </summary>
/// <returns>All the elements.</returns>
public static IEnumerable<IEdmSchemaElement> GetAllElements(this IEdmModel model)
{
foreach (var element in model.SchemaElements.Where(el =>
!ODataConstants.StandardNamespaces.Any(std => el.Namespace.StartsWith(std))))
{
yield return element;
}

foreach (var refModel in model.ReferencedModels)
{
foreach (var element in refModel.SchemaElements.Where(el =>
!ODataConstants.StandardNamespaces.Any(std => el.Namespace.StartsWith(std))))
{
yield return element;
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private IDictionary<string, IList<IEdmOperation>> LoadEdmOperations()
{
IDictionary<string, IList<IEdmOperation>> edmOperationDict = new Dictionary<string, IList<IEdmOperation>>();

foreach (var edmOperation in Model.SchemaElements.OfType<IEdmOperation>().Where(e => e.IsBound))
foreach (var edmOperation in Model.GetAllElements().OfType<IEdmOperation>().Where(e => e.IsBound))
{
IEdmOperationParameter bindingParameter = edmOperation.Parameters.First();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private bool ShouldExpandNavigationProperty(IEdmNavigationProperty navigationPro
/// </summary>
private void RetrieveBoundOperationPaths()
{
foreach (var edmOperation in _model.SchemaElements.OfType<IEdmOperation>().Where(e => e.IsBound))
foreach (var edmOperation in _model.GetAllElements().OfType<IEdmOperation>().Where(e => e.IsBound))
{
if (!CanFilter(edmOperation))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// ------------------------------------------------------------

using Microsoft.OData.Edm;
using Microsoft.OpenApi.OData.Edm;

namespace Microsoft.OpenApi.OData.Generator
{
Expand All @@ -30,7 +31,7 @@ public void Visit(IEdmModel model)
return;
}

foreach (var element in model.SchemaElements)
foreach (var element in model.GetAllElements())
{
switch (element.SchemaElementKind)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public static IDictionary<string, OpenApiExample> CreateExamples(this ODataConte

// Each entity type, complex type, enumeration type, and type definition directly
// or indirectly used in the paths field is represented as a name / value pair of the schemas map.
foreach (var element in context.Model.SchemaElements.Where(c => !c.Namespace.StartsWith("Org.OData.")))
// Ideally this would be driven off the types used in the paths, but in practice, it is simply
// all of the types present in the model.
IEnumerable<IEdmSchemaElement> elements = context.Model.GetAllElements();

foreach (var element in elements)
{
switch (element.SchemaElementKind)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public static IDictionary<string, OpenApiSchema> CreateSchemas(this ODataContext

// Each entity type, complex type, enumeration type, and type definition directly
// or indirectly used in the paths field is represented as a name / value pair of the schemas map.
foreach (var element in context.Model.SchemaElements.Where(c => !c.Namespace.StartsWith("Org.OData.")))
// Ideally this would be driven off the types used in the paths, but in practice, it is simply
// all of the types present in the model.
IEnumerable<IEdmSchemaElement> elements = context.Model.GetAllElements();

foreach (var element in elements)
{
switch (element.SchemaElementKind)
{
Expand Down
27 changes: 27 additions & 0 deletions src/Microsoft.OpenApi.OData.Reader/OData/ODataConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.OpenApi.OData
{
internal static class ODataConstants
{
/// <summary>
/// Namespaces used in standard included models.
/// </summary>
public static IList<string> StandardNamespaces = new List<string>
{
"Org.OData.",
"Edm",
"OData.Community.",
};

}
}
192 changes: 192 additions & 0 deletions src/OoasUtil/ComLineProcesser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public ComLineProcesser(string[] args)
/// </summary>
public OpenApiFormat? Format { get; private set; }

/// <summary>
/// Whether KeyAsSegment is used.
/// </summary>
public bool? KeyAsSegment { get; private set; }

/// <summary>
/// Output OpenApi Specification Version.
/// </summary>
Expand All @@ -60,6 +65,31 @@ public ComLineProcesser(string[] args)
/// </summary>
public bool IsLocalFile { get; private set; }

/// <summary>
/// Set the output to produce all derived types in responses.
/// </summary>
public bool? DerivedTypesReferencesForResponses { get; private set; }

/// <summary>
/// Set the output to expect all derived types in request bodies.
/// </summary>
public bool? DerivedTypesReferencesForRequestBody { get; private set; }

/// <summary>
/// Set the output to expose pagination for collections.
/// </summary>
public bool? EnablePagination { get; private set; }

/// <summary>
/// tSet the output to use unqualified calls for bound operations.
/// </summary>
public bool? EnableUnqualifiedCall { get; private set; }

/// <summary>
/// tDisable examples in the schema.
/// </summary>
public bool? DisableSchemaExamples { get; private set; }

/// <summary>
/// Process the arguments.
/// </summary>
Expand Down Expand Up @@ -98,6 +128,14 @@ public bool Process()
i++;
break;

case "--keyassegment":
case "-k":
if (!ProcessKeyAsSegment(true))
{
return false;
}
break;

case "--yaml":
case "-y":
if (!ProcessTarget(OpenApiFormat.Yaml))
Expand Down Expand Up @@ -132,6 +170,46 @@ public bool Process()
i++;
break;

case "--derivedtypesreferencesforresponses":
case "-drs":
if (!ProcessDerivedTypesReferencesForResponses(true))
{
return false;
}
break;

case "--derivedtypesreferencesforrequestbody":
case "-drq":
if (!ProcessDerivedTypesReferencesForRequestBody(true))
{
return false;
}
break;

case "--enablepagination":
case "-p":
if (!ProcessEnablePagination(true))
{
return false;
}
break;

case "--enableunqualifiedcall":
case "-u":
if (!ProcessEnableUnqualifiedCall(true))
{
return false;
}
break;

case "--disableschemaexamples":
case "-x":
if (!ProcessDisableSchemaExamples(true))
{
return false;
}
break;

default:
PrintUsage();
return false;
Expand All @@ -155,6 +233,36 @@ public bool Process()
Version = OpenApiSpecVersion.OpenApi3_0;
}

if (KeyAsSegment == null)
{
KeyAsSegment = false;
}

if (DerivedTypesReferencesForResponses == null)
{
DerivedTypesReferencesForResponses = false;
}

if (DerivedTypesReferencesForRequestBody == null)
{
DerivedTypesReferencesForRequestBody = false;
}

if (EnablePagination == null)
{
EnablePagination = false;
}

if (EnableUnqualifiedCall == null)
{
EnableUnqualifiedCall = false;
}

if (DisableSchemaExamples == null)
{
DisableSchemaExamples = false;
}

_continue = ValidateArguments();
return _continue;
}
Expand Down Expand Up @@ -198,6 +306,84 @@ private bool ProcessTarget(OpenApiFormat format)
return true;
}

private bool ProcessKeyAsSegment(bool keyAsSegment)
{
if (KeyAsSegment != null)
{
Console.WriteLine("[Error:] Multiple [--keyassegment|-k] are not allowed.\n");
PrintUsage();
return false;
}

KeyAsSegment = keyAsSegment;
return true;
}

private bool ProcessDerivedTypesReferencesForResponses(bool derivedTypesReferencesForResponses)
{
if (DerivedTypesReferencesForResponses != null)
{
Console.WriteLine("[Error:] Multiple [--derivedtypesreferencesforresponses|-drs] are not allowed.\n");
PrintUsage();
return false;
}

DerivedTypesReferencesForResponses = derivedTypesReferencesForResponses;
return true;
}

private bool ProcessDerivedTypesReferencesForRequestBody(bool derivedTypesReferencesForRequestBody)
{
if (DerivedTypesReferencesForRequestBody != null)
{
Console.WriteLine("[Error:] Multiple [--derivedtypesreferencesforrequestbody|-drq] are not allowed.\n");
PrintUsage();
return false;
}

DerivedTypesReferencesForRequestBody = derivedTypesReferencesForRequestBody;
return true;
}

private bool ProcessEnablePagination(bool enablePagination)
{
if (EnablePagination != null)
{
Console.WriteLine("[Error:] Multiple [--enablepagination|-p] are not allowed.\n");
PrintUsage();
return false;
}

EnablePagination = enablePagination;
return true;
}

private bool ProcessEnableUnqualifiedCall(bool enableUnqualifiedCall)
{
if (EnableUnqualifiedCall != null)
{
Console.WriteLine("[Error:] Multiple [--enableunqualifiedcall|-u] are not allowed.\n");
PrintUsage();
return false;
}

EnableUnqualifiedCall = enableUnqualifiedCall;
return true;
}

private bool ProcessDisableSchemaExamples(bool disableSchemaExamples)
{
if (DisableSchemaExamples != null)
{
Console.WriteLine("[Error:] Multiple [--disableschemaexamples|-x] are not allowed.\n");
PrintUsage();
return false;
}

DisableSchemaExamples = disableSchemaExamples;
return true;
}

private bool ProcessTarget(int version)
{
if (Version != null)
Expand Down Expand Up @@ -256,6 +442,12 @@ public static void PrintUsage()
sb.Append(" --version|-v\t\t\tDisplay version.\n");
sb.Append(" --input|-i CsdlFileOrUrl\tSet the CSDL file name or the OData Service Url.\n");
sb.Append(" --output|-o OutputFile\tSet the output file name.\n");
sb.Append(" --keyassegment|-k\t\t\tSet the output to use key-as-segment style URLs.\n");
sb.Append(" --derivedtypesreferencesforresponses|-drs\t\t\tSet the output to produce all derived types in responses.\n");
sb.Append(" --derivedtypesreferencesforrequestbody|-drq\t\t\tSet the output to expect all derived types in request bodies.\n");
sb.Append(" --enablepagination|-p\t\t\tSet the output to expose pagination for collections.\n");
sb.Append(" --enableunqualifiedcall|-u\t\t\tSet the output to use unqualified calls for bound operations.\n");
sb.Append(" --disableschemaexamples|-x\t\t\tDisable examples in the schema.\n");
sb.Append(" --json|-j\t\t\tSet the output format as JSON.\n");
sb.Append(" --yaml|-y\t\t\tSet the output format as YAML.\n");
sb.Append(" --specversion|-s IntVersion\tSet the OpenApi Specification version of the output. Only 2 or 3 are supported.\n");
Expand Down
Loading