Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for optional parameters for operations #195

Merged
merged 5 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
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 @@ -128,8 +127,8 @@ public static IList<OpenApiParameter> 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,
irvinesunday marked this conversation as resolved.
Show resolved Hide resolved
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