Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Added support for media types in query parameters #238

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -64,6 +64,7 @@ public IList<GenerationError> Apply(
{
var inValue = paramElement.Attribute(KnownXmlStrings.In)?.Value.Trim();
var name = paramElement.Attribute(KnownXmlStrings.Name)?.Value.Trim();
var mediaType = paramElement.Attribute(KnownXmlStrings.Type)?.Value ?? "";

if (settings.RemoveRoslynDuplicateStringFromParamName)
{
Expand Down Expand Up @@ -145,10 +146,20 @@ public IList<GenerationError> Apply(
In = parameterLocation,
Description = description,
Required = parameterLocation == ParameterLocation.Path || Convert.ToBoolean(isRequired),
Schema = schema,
Examples = examples.Any() ? examples : null
};

// If media type is specified add parameter as content for complex serialization
if (!string.IsNullOrEmpty(mediaType)) {
openApiParameter.Content = new Dictionary<string, OpenApiMediaType>
{
[mediaType] = new OpenApiMediaType { Schema = schema }
};
}
else {
openApiParameter.Schema = schema;
}

operation.Parameters.Add(openApiParameter);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

using System;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.Contracts;

namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis.Controllers
{
/// <summary>
/// Sample v7 controller.
/// </summary>
public class SampleV7Controller : ApiController
{
/// <summary>
/// Sample get 1
/// </summary>
/// <group>Sample V7</group>
/// <verb>GET</verb>
/// <url>https://myapi.sample.com/V7/samples/{id}?queryString={queryString}</url>
/// <param name="queryString" cref="string" in="query">Query param 1 with no media type</param>
/// <param name="sampleObjectInQuery2" cref="SampleObject1" in="query" type="application/json">Query param as application/json content</param>
/// <param name="id" cref="string" in="path">The object id</param>
/// <response code="200"><see cref="SampleObject1"/>Sample object retrieved</response>
/// <response code="400"><see cref="string"/>Bad request</response>
[HttpGet]
[Route("V7/samples/{id}")]
public async Task<SampleObject1> SampleGet1(string id, string queryString = null) {
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<Compile Include="Controllers\SampleV4Controller.cs" />
<Compile Include="Controllers\SampleV6Controller.cs" />
<Compile Include="Controllers\SampleV5Controller.cs" />
<Compile Include="Controllers\SampleV7Controller.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
<None Update="DocumentVariantTests\Input\AnnotationWithDuplicatePaths.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="OpenApiDocumentGeneratorTests\Input\AnnotationInQueryParamMediaType.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="OpenApiDocumentGeneratorTests\Output\AnnotationInQueryParamMediaType.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="OperationConfigTests\Input\AnnotationWithNoSimpleTypeReferenceInOperation.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,32 @@ public static IEnumerable<object[]> GetTestCasesForValidDocumentationShouldRetur
OutputDirectory,
"AnnotationWithRuntimeSerialization.Json")
};

// Valid XML document with object type in param tags and set with media type application/json
yield return new object[]
{
"Object Type in Param Tags",
new List<string>
{
Path.Combine(InputDirectory, "AnnotationInQueryParamMediaType.xml"),
Path.Combine(InputDirectory,
"Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.Contracts.xml")
},
new List<string>
{
Path.Combine(
InputDirectory,
"Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis.dll"),
Path.Combine(
InputDirectory,
"Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.Contracts.dll")
},
"1.0.0",
1,
Path.Combine(
OutputDirectory,
"AnnotationInQueryParamMediaType.Json")
};
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
{
"openapi": "3.0.1",
"info": {
"title": "Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis",
"version": "1.0.0"
},
"servers": [
{
"url": "https://myapi.sample.com"
}
],
"paths": {
"/V7/samples/{id}": {
"get": {
"tags": [
"Sample V7"
],
"summary": "Sample get 1",
"operationId": "getV7SamplesById",
"parameters": [
{
"name": "queryString",
"in": "query",
"description": "Query param 1 with no media type",
"schema": {
"type": "string"
}
},
{
"name": "sampleObjectInQuery2",
"in": "query",
"description": "Query param as application/json content",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.Contracts.SampleObject1"
}
}
}
},
{
"name": "id",
"in": "path",
"description": "The object id",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Sample object retrieved",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.Contracts.SampleObject1"
}
}
}
},
"400": {
"description": "Bad request",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.Contracts.SampleObject1": {
"required": [
"samplePropertyString3",
"samplePropertyString4",
"samplePropertyEnum"
],
"type": "object",
"properties": {
"samplePropertyBool": {
"type": "boolean",
"description": "Gets or sets the sample property bool"
},
"samplePropertyStringInt": {
"type": "integer",
"description": "Gets or sets the sample property int",
"format": "int32"
},
"samplePropertyString1": {
"type": "string",
"description": "Gets or sets the sample property string 1"
},
"samplePropertyString2": {
"type": "string",
"description": "Gets or sets the sample property string 2"
},
"samplePropertyString3": {
"type": "string",
"description": "Gets or sets the sample property string 3"
},
"samplePropertyString4": {
"type": "string",
"description": "Gets or sets the sample property string 4"
},
"samplePropertyEnum": {
"enum": [
"SampleEnumValue1",
"SampleEnumValue2"
],
"type": "string",
"description": "Gets or sets the sample property enum"
}
}
}
},
"securitySchemes": {
"http-bearer": {
"type": "http",
"description": "Test security",
"scheme": "bearer",
"bearerFormat": "JWT"
},
"oauth": {
"type": "oauth2",
"description": "Test security",
"flows": {
"implicit": {
"authorizationUrl": "https://example.com/api/oauth/dialog",
"refreshUrl": "https://example.com/api/oauth/dialog",
"scopes": {
"scope1": "Example flow description"
}
}
}
},
"openIdConnect": {
"type": "openIdConnect",
"description": "Test security",
"openIdConnectUrl": "https://example.com/api/oauth/dialog"
}
}
},
"security": [
{
"http-bearer": [],
"oauth": [
"scope1"
],
"openIdConnect": [
"scope1",
"scope2"
]
}
]
}