Skip to content

Commit

Permalink
Adds support for optional parameters for operations (#195)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
irvinesunday authored Mar 11, 2022
1 parent d48ef90 commit 5c8ef30
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,7 +69,7 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(function, nameof(function));

IList<OpenApiParameter> parameters = new List<OpenApiParameter>();
IList<OpenApiParameter> parameters = new List<OpenApiParameter>();
int skip = function.IsBound ? 1 : 0;
foreach (IEdmOperationParameter edmParameter in function.Parameters.Skip(skip))
{
Expand All @@ -80,7 +79,7 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
{
continue;
}
}
}

OpenApiParameter parameter;
// Structured or collection-valued parameters are represented as a parameter alias
Expand Down Expand Up @@ -125,11 +124,12 @@ public static IList<OpenApiParameter> 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)
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

IEdmFunction function3 = model.SchemaElements.OfType<IEdmFunction>().First(f => f.Name == "roleScheduleInstances");
Assert.NotNull(function3);

// Act
IList<OpenApiParameter> parameters1 = context.CreateParameters(function1);
IList<OpenApiParameter> parameters2 = context.CreateParameters(function2);
IList<OpenApiParameter> parameters3 = context.CreateParameters(function3);

// Assert
Assert.NotNull(parameters1);
Expand All @@ -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"",
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5c8ef30

Please sign in to comment.