diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs index 8519af38..aa62303e 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs @@ -94,13 +94,28 @@ public static IList 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 + { + { + 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]. diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiParameterGeneratorTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiParameterGeneratorTests.cs index d48df405..4534df6d 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiParameterGeneratorTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiParameterGeneratorTests.cs @@ -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 @@ 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().First(f => f.Name == "getRoleScopeTagsByIds"); + Assert.NotNull(function1); + + IEdmFunction function2 = model.SchemaElements.OfType().First(f => f.Name == "getRoleScopeTagsByResource"); + Assert.NotNull(function2); + + // Act + IList parameters1 = context.CreateParameters(function1); + IList 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 = @" diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Graph.Beta.OData.xml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Graph.Beta.OData.xml index ff463d9d..e31307df 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Graph.Beta.OData.xml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Graph.Beta.OData.xml @@ -9356,6 +9356,16 @@ + + + + + + + + + +