Created
October 25, 2018 08:10
-
-
Save cjlotz/6cdd002d60b1dd63141c406a815df21d to your computer and use it in GitHub Desktop.
Issue with content types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async Task<SwaggerParameter> AddBodyParameterAsync(ResourceActionProcessorContext context, ResourceActionParameterDescriptor apiParameter) | |
{ | |
var operation = context.OperationDescription.Operation; | |
JsonSchema4 schema = await GenerateSchema(context, apiParameter); | |
string contentType = context.ResourceAction.Action.Request.ContentType; | |
// NOTE CL: Workaround for bug in NSwag that overrides the custom content type - https://github.com/RSuter/NSwag/issues/1681 | |
// By adding a Body parameter upfront instead of it being added implicitly when setting the operation.RequestBody, we | |
// prevent NSwag of overwriting our custom content type into application/json | |
var operationParameter = new SwaggerParameter | |
{ | |
Kind = SwaggerParameterKind.Body, | |
Position = apiParameter.Position | |
}; | |
operation.Parameters.Add(operationParameter); | |
var body = new OpenApiRequestBody | |
{ | |
Name = apiParameter.Name, | |
IsRequired = apiParameter.IsRequired(_settings.RequireParametersWithoutDefault), | |
Description = await apiParameter.GetDocumentationAsync(), | |
Position = apiParameter.Position | |
}; | |
var mediaType = new OpenApiMediaType { Schema = schema }; | |
var example = new OpenApiExample | |
{ | |
Description = "JSON request exmaple", | |
ExternalValue = $"http://pragmaproducts.github.io/onkeyapi/examples/{context.ResourceAction.Group.Name}/{context.ResourceAction.Resource.Name}/{context.ResourceAction.Action.Name}.json" | |
}; | |
mediaType.Examples.Add("Payload", example); | |
body.Content.Add(contentType, mediaType); | |
operation.Consumes = new List<string> { contentType }; | |
operation.RequestBody = body; | |
return operationParameter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment