Skip to content

Instantly share code, notes, and snippets.

@cjlotz
Created October 25, 2018 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjlotz/6cdd002d60b1dd63141c406a815df21d to your computer and use it in GitHub Desktop.
Save cjlotz/6cdd002d60b1dd63141c406a815df21d to your computer and use it in GitHub Desktop.
Issue with content types
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