From cff7e3ee216ee3081462aac636cc4f301ec0d70c Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Wed, 2 Mar 2022 00:51:40 +0300 Subject: [PATCH 1/5] Add Odata.Core.V1.OptionalParameter --- .../Vocabulary/Core/CoreConstants.cs | 11 +++++++++++ .../Vocabulary/Core/OptionalParameterType.cs | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/CoreConstants.cs create mode 100644 src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/OptionalParameterType.cs diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/CoreConstants.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/CoreConstants.cs new file mode 100644 index 00000000..2f10ac53 --- /dev/null +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/CoreConstants.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.OpenApi.OData.Vocabulary.Core +{ + internal class CoreConstants + { + public const string OptionalParameter = "Org.OData.Core.V1.OptionalParameter"; + } +} diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/OptionalParameterType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/OptionalParameterType.cs new file mode 100644 index 00000000..943b8ae1 --- /dev/null +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/OptionalParameterType.cs @@ -0,0 +1,16 @@ +// ------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. +// ------------------------------------------------------------ + +namespace Microsoft.OpenApi.OData.Vocabulary.Core +{ + /// + /// Complex Type: Org.OData.Core.V1.OptionalParameterType + /// + [Term("Org.OData.Core.V1.OptionalParameter")] + internal class OptionalParameterType + { + public string DefaultValue { get; set; } + } +} From ec97cf6b27408ee5ad96c5754294364aeb92052d Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Thu, 10 Mar 2022 15:53:57 +0300 Subject: [PATCH 2/5] Optional parameters could be optional and marked in: query --- .../Generator/OpenApiParameterGenerator.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs index 91aaa2c7..582cd8b8 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 @@ -128,8 +127,8 @@ public static IList CreateParameters(this ODataContext context parameter = new OpenApiParameter { Name = parameterNameMapping == null ? edmParameter.Name : parameterNameMapping[edmParameter.Name], - In = ParameterLocation.Path, - Required = true, + In = edmParameter is IEdmOptionalParameter ? ParameterLocation.Query : ParameterLocation.Path, + Required = edmParameter is not IEdmOptionalParameter, Schema = context.CreateEdmTypeSchema(edmParameter.Type) }; } From 5b57bce129574999a02c445203a27128523f6006 Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Thu, 10 Mar 2022 15:54:29 +0300 Subject: [PATCH 3/5] Add test to validate optional parameters --- .../OpenApiParameterGeneratorTests.cs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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() From 3c8c1d36376d48cb04708d75404bcaf058e295e8 Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Thu, 10 Mar 2022 16:03:04 +0300 Subject: [PATCH 4/5] Remove unnecessary code --- .../Vocabulary/Core/CoreConstants.cs | 11 ----------- .../Vocabulary/Core/OptionalParameterType.cs | 16 ---------------- 2 files changed, 27 deletions(-) delete mode 100644 src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/CoreConstants.cs delete mode 100644 src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/OptionalParameterType.cs diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/CoreConstants.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/CoreConstants.cs deleted file mode 100644 index 2f10ac53..00000000 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/CoreConstants.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Microsoft.OpenApi.OData.Vocabulary.Core -{ - internal class CoreConstants - { - public const string OptionalParameter = "Org.OData.Core.V1.OptionalParameter"; - } -} diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/OptionalParameterType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/OptionalParameterType.cs deleted file mode 100644 index 943b8ae1..00000000 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/OptionalParameterType.cs +++ /dev/null @@ -1,16 +0,0 @@ -// ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. -// ------------------------------------------------------------ - -namespace Microsoft.OpenApi.OData.Vocabulary.Core -{ - /// - /// Complex Type: Org.OData.Core.V1.OptionalParameterType - /// - [Term("Org.OData.Core.V1.OptionalParameter")] - internal class OptionalParameterType - { - public string DefaultValue { get; set; } - } -} From b4ef91b009abb6741263004f3b1a87deb4d315e3 Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Thu, 10 Mar 2022 17:26:56 +0300 Subject: [PATCH 5/5] PR review suggestion --- .../Generator/OpenApiParameterGenerator.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs index 582cd8b8..9414debe 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs @@ -69,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)) { @@ -79,7 +79,7 @@ public static IList CreateParameters(this ODataContext context { continue; } - } + } OpenApiParameter parameter; // Structured or collection-valued parameters are represented as a parameter alias @@ -124,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 = edmParameter is IEdmOptionalParameter ? ParameterLocation.Query : ParameterLocation.Path, - Required = edmParameter is not IEdmOptionalParameter, + In = isOptionalParameter ? ParameterLocation.Query : ParameterLocation.Path, + Required = !isOptionalParameter, Schema = context.CreateEdmTypeSchema(edmParameter.Type) }; }