Skip to content

Commit 5c8ef30

Browse files
authored
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
1 parent d48ef90 commit 5c8ef30

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.OpenApi.OData.Common;
1212
using Microsoft.OData.Edm.Vocabularies;
1313
using Microsoft.OpenApi.OData.Edm;
14-
using Microsoft.OpenApi.OData.Vocabulary;
1514
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
1615

1716
namespace Microsoft.OpenApi.OData.Generator
@@ -70,7 +69,7 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
7069
Utils.CheckArgumentNull(context, nameof(context));
7170
Utils.CheckArgumentNull(function, nameof(function));
7271

73-
IList<OpenApiParameter> parameters = new List<OpenApiParameter>();
72+
IList<OpenApiParameter> parameters = new List<OpenApiParameter>();
7473
int skip = function.IsBound ? 1 : 0;
7574
foreach (IEdmOperationParameter edmParameter in function.Parameters.Skip(skip))
7675
{
@@ -80,7 +79,7 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
8079
{
8180
continue;
8281
}
83-
}
82+
}
8483

8584
OpenApiParameter parameter;
8685
// Structured or collection-valued parameters are represented as a parameter alias
@@ -125,11 +124,12 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
125124
else
126125
{
127126
// Primitive parameters use the same type mapping as described for primitive properties.
127+
bool isOptionalParameter = edmParameter is IEdmOptionalParameter;
128128
parameter = new OpenApiParameter
129129
{
130130
Name = parameterNameMapping == null ? edmParameter.Name : parameterNameMapping[edmParameter.Name],
131-
In = ParameterLocation.Path,
132-
Required = true,
131+
In = isOptionalParameter ? ParameterLocation.Query : ParameterLocation.Path,
132+
Required = !isOptionalParameter,
133133
Schema = context.CreateEdmTypeSchema(edmParameter.Type)
134134
};
135135
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiParameterGeneratorTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ public void CreateParametersWorks()
375375
{
376376
// Arrange
377377
IEdmModel model = EdmModelHelper.GraphBetaModel;
378-
ODataContext context = new ODataContext(model);
378+
ODataContext context = new(model);
379379
IEdmSingleton deviceMgmt = model.EntityContainer.FindSingleton("deviceManagement");
380380
Assert.NotNull(deviceMgmt);
381381

@@ -385,9 +385,13 @@ public void CreateParametersWorks()
385385
IEdmFunction function2 = model.SchemaElements.OfType<IEdmFunction>().First(f => f.Name == "getRoleScopeTagsByResource");
386386
Assert.NotNull(function2);
387387

388+
IEdmFunction function3 = model.SchemaElements.OfType<IEdmFunction>().First(f => f.Name == "roleScheduleInstances");
389+
Assert.NotNull(function3);
390+
388391
// Act
389392
IList<OpenApiParameter> parameters1 = context.CreateParameters(function1);
390393
IList<OpenApiParameter> parameters2 = context.CreateParameters(function2);
394+
IList<OpenApiParameter> parameters3 = context.CreateParameters(function3);
391395

392396
// Assert
393397
Assert.NotNull(parameters1);
@@ -396,6 +400,10 @@ public void CreateParametersWorks()
396400
Assert.NotNull(parameters2);
397401
OpenApiParameter parameter2 = Assert.Single(parameters2);
398402

403+
Assert.NotNull(parameters3);
404+
Assert.Equal(4, parameters3.Count);
405+
OpenApiParameter parameter3 = parameters3.First();
406+
399407
string json1 = parameter1.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
400408
string expectedPayload1 = $@"{{
401409
""name"": ""ids"",
@@ -425,8 +433,19 @@ public void CreateParametersWorks()
425433
}}
426434
}}";
427435

436+
string json3 = parameter3.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
437+
string expectedPayload3 = $@"{{
438+
""name"": ""directoryScopeId"",
439+
""in"": ""query"",
440+
""schema"": {{
441+
""type"": ""string"",
442+
""nullable"": true
443+
}}
444+
}}";
445+
428446
Assert.Equal(expectedPayload1.ChangeLineBreaks(), json1);
429447
Assert.Equal(expectedPayload2.ChangeLineBreaks(), json2);
448+
Assert.Equal(expectedPayload3.ChangeLineBreaks(), json3);
430449
}
431450

432451
public static IEdmModel GetEdmModel()

0 commit comments

Comments
 (0)