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

Fixes structured & collection-valued parameters of functions #133

Merged
merged 3 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -94,13 +94,28 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
Name = parameterNameMapping == null ? edmParameter.Name : parameterNameMapping[edmParameter.Name],
In = ParameterLocation.Query, // as query option
Required = true,
Schema = new OpenApiSchema
{
Type = "string",

// These Parameter Objects optionally can contain the field description,
// whose value is the value of the unqualified annotation Core.Description of the parameter.
Description = context.Model.GetDescriptionAnnotation(edmParameter)
// Create schema in the Content property to indicate that the parameters are serialized as JSON
Content = new Dictionary<string, OpenApiMediaType>
{
{
Constants.ApplicationJsonMediaType,
new OpenApiMediaType
{
Schema = new OpenApiSchema
{
Type = "array",
Items = new OpenApiSchema
{
Type = "string"
},

// These Parameter Objects optionally can contain the field description,
// whose value is the value of the unqualified annotation Core.Description of the parameter.
Description = context.Model.GetDescriptionAnnotation(edmParameter)
}
}
}
},

// The parameter description describes the format this URL-encoded JSON object or array, and/or reference to [OData-URL].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// ------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.OData.Edm;
Expand Down Expand Up @@ -369,6 +370,65 @@ private void VerifyCreateExpandParameter(IEdmElement edmElement, ODataContext co
Assert.Equal(expected.ChangeLineBreaks(), json);
}

[Fact]
public void CreateParametersWorks()
{
// Arrange
IEdmModel model = EdmModelHelper.GraphBetaModel;
ODataContext context = new ODataContext(model);
IEdmSingleton deviceMgmt = model.EntityContainer.FindSingleton("deviceManagement");
Assert.NotNull(deviceMgmt);

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

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

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

// Assert
Assert.NotNull(parameters1);
OpenApiParameter parameter1 = Assert.Single(parameters1);

Assert.NotNull(parameters2);
OpenApiParameter parameter2 = Assert.Single(parameters2);

string json1 = parameter1.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
string expectedPayload1 = $@"{{
""name"": ""ids"",
""in"": ""query"",
""description"": ""The URL-encoded JSON object"",
""required"": true,
""content"": {{
""application/json"": {{
""schema"": {{
""type"": ""array"",
""items"": {{
""type"": ""string""
}}
}}
}}
}}
}}";

string json2 = parameter2.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
string expectedPayload2 = $@"{{
""name"": ""resource"",
""in"": ""path"",
""required"": true,
""schema"": {{
""type"": ""string"",
""nullable"": true
}}
}}";

Assert.Equal(expectedPayload1.ChangeLineBreaks(), json1);
Assert.Equal(expectedPayload2.ChangeLineBreaks(), json2);
}

public static IEdmModel GetEdmModel()
{
const string modelText = @"<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9356,6 +9356,16 @@
<Property Name="failureCategory" Type="microsoft.graph.deviceEnrollmentFailureReason" Nullable="false" />
<Property Name="failureReason" Type="Edm.String" Unicode="false" />
</EntityType>
<Function Name="getRoleScopeTagsByIds" IsBound="true">
<Parameter Name="bindingParameter" Type="graph.deviceManagement" />
<Parameter Name="ids" Type="Collection(Edm.String)" Unicode="false" />
<ReturnType Type="Collection(graph.roleScopeTag)" />
</Function>
<Function Name="getRoleScopeTagsByResource" IsBound="true">
<Parameter Name="bindingParameter" Type="graph.deviceManagement" />
<Parameter Name="resource" Type="Edm.String" Unicode="false" />
<ReturnType Type="Collection(graph.roleScopeTag)" />
</Function>
<Function Name="delta" IsBound="true">
<Parameter Name="bindingParameter" Type="Collection(microsoft.graph.user)" />
<ReturnType Type="Collection(microsoft.graph.user)" />
Expand Down