From 5c8ef30936d3c908aae1df022674ff8d1a4c7b56 Mon Sep 17 00:00:00 2001 From: Irvine Sunday <40403681+irvinesunday@users.noreply.github.com> Date: Fri, 11 Mar 2022 16:48:20 +0300 Subject: [PATCH] Adds support for optional parameters for operations (#195) * Add Odata.Core.V1.OptionalParameter * Optional parameters could be optional and marked in: query * Add test to validate optional parameters * Remove unnecessary code * PR review suggestion --- .../Generator/OpenApiParameterGenerator.cs | 10 ++++----- .../OpenApiParameterGeneratorTests.cs | 21 ++++++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs index 91aaa2c7..9414debe 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs @@ -11,7 +11,6 @@ using Microsoft.OpenApi.OData.Common; using Microsoft.OData.Edm.Vocabularies; using Microsoft.OpenApi.OData.Edm; -using Microsoft.OpenApi.OData.Vocabulary; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; namespace Microsoft.OpenApi.OData.Generator @@ -70,7 +69,7 @@ public static IList CreateParameters(this ODataContext context Utils.CheckArgumentNull(context, nameof(context)); Utils.CheckArgumentNull(function, nameof(function)); - IList parameters = new List(); + IList parameters = new List(); int skip = function.IsBound ? 1 : 0; foreach (IEdmOperationParameter edmParameter in function.Parameters.Skip(skip)) { @@ -80,7 +79,7 @@ public static IList CreateParameters(this ODataContext context { continue; } - } + } OpenApiParameter parameter; // Structured or collection-valued parameters are represented as a parameter alias @@ -125,11 +124,12 @@ public static IList CreateParameters(this ODataContext context else { // Primitive parameters use the same type mapping as described for primitive properties. + bool isOptionalParameter = edmParameter is IEdmOptionalParameter; parameter = new OpenApiParameter { Name = parameterNameMapping == null ? edmParameter.Name : parameterNameMapping[edmParameter.Name], - In = ParameterLocation.Path, - Required = true, + In = isOptionalParameter ? ParameterLocation.Query : ParameterLocation.Path, + Required = !isOptionalParameter, Schema = context.CreateEdmTypeSchema(edmParameter.Type) }; } diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiParameterGeneratorTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiParameterGeneratorTests.cs index 4534df6d..a6468d01 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiParameterGeneratorTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiParameterGeneratorTests.cs @@ -375,7 +375,7 @@ public void CreateParametersWorks() { // Arrange IEdmModel model = EdmModelHelper.GraphBetaModel; - ODataContext context = new ODataContext(model); + ODataContext context = new(model); IEdmSingleton deviceMgmt = model.EntityContainer.FindSingleton("deviceManagement"); Assert.NotNull(deviceMgmt); @@ -385,9 +385,13 @@ public void CreateParametersWorks() IEdmFunction function2 = model.SchemaElements.OfType().First(f => f.Name == "getRoleScopeTagsByResource"); Assert.NotNull(function2); + IEdmFunction function3 = model.SchemaElements.OfType().First(f => f.Name == "roleScheduleInstances"); + Assert.NotNull(function3); + // Act IList parameters1 = context.CreateParameters(function1); IList parameters2 = context.CreateParameters(function2); + IList parameters3 = context.CreateParameters(function3); // Assert Assert.NotNull(parameters1); @@ -396,6 +400,10 @@ public void CreateParametersWorks() Assert.NotNull(parameters2); OpenApiParameter parameter2 = Assert.Single(parameters2); + Assert.NotNull(parameters3); + Assert.Equal(4, parameters3.Count); + OpenApiParameter parameter3 = parameters3.First(); + string json1 = parameter1.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); string expectedPayload1 = $@"{{ ""name"": ""ids"", @@ -425,8 +433,19 @@ public void CreateParametersWorks() }} }}"; + string json3 = parameter3.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); + string expectedPayload3 = $@"{{ + ""name"": ""directoryScopeId"", + ""in"": ""query"", + ""schema"": {{ + ""type"": ""string"", + ""nullable"": true + }} +}}"; + Assert.Equal(expectedPayload1.ChangeLineBreaks(), json1); Assert.Equal(expectedPayload2.ChangeLineBreaks(), json2); + Assert.Equal(expectedPayload3.ChangeLineBreaks(), json3); } public static IEdmModel GetEdmModel()