Fixes structured & collection-valued parameters of functions (#133)

* Use content property in collection-valued & structured parameters

Add content property and a media type object to indicate that the parameters are serialized as JSON

* Adds test for CreateParameters

Co-authored-by: Sam Xu <saxu@microsoft.com>
This commit is contained in:
Irvine Sunday 2021-11-18 20:42:34 +03:00 committed by GitHub
parent 73ad41ec0e
commit 03b6000a84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 6 deletions

View file

@ -94,13 +94,28 @@ namespace Microsoft.OpenApi.OData.Generator
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].

View file

@ -4,6 +4,7 @@
// ------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.OData.Edm;
@ -369,6 +370,65 @@ schema:
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"">

View file

@ -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)" />