[C#] aspdotnetcore (C# server) generation does not support byte/binary body
Created by: grmcdorman
Description
When an OpenAPI spec describes the request with a body parameter of either: schema: { "type": "string", "format": "binary" } or schema: { "type": "string", "format": "byte" } the generated controller, correctly, has a [FromBody] System.IO.Stream or [FromBody] byte[] parameter. However, Swashbuckle out of the box does not support either type of parameter, resulting in 415 responses to any request sent, regardless of content type.
openapi-generator version
Discovered using the current git main branch.
OpenAPI declaration file content or url
'/data': { 'put': { 'parameters': [ { "name": "data", "in": "body", "description": "Binary body data", "required": true, "schema": { "type": "string", "format": "byte" } } }
Command line used for generation
java -jar <openapi-generator-cli.jar> generate -i <openapi.json> -g aspnetcore -o -c
Steps to reproduce
Specify an operation with the body as described above.
Related issues/PRs
Suggest a fix/enhancement
The scheme described at https://weblog.west-wind.com/posts/2017/Sep/14/Accepting-Raw-Request-Body-Content-in-ASPNET-Core-API-Controllers#Binary-Data provides a solution for forwarding the body in the appropriate format.
In addition, it is necessary to tell Swashbuckle that the content type is to be 'application/octet-stream'. (Currently OpenAPI-generator does not tell Swashbuckle the expected content type, or the response type, at all). Either a specific 'application/octect-stream' decorator (see https://stackoverflow.com/questions/41141137/how-can-i-tell-swashbuckle-that-the-body-content-is-required) or a more generic decorator in the style suggested at https://stackoverflow.com/questions/34990291/swashbuckle-swagger-how-to-annotate-content-types is needed.
Thus, there would be three steps to implement this scheme:
- Create new .mustache files to generate the BinaryPayloadAttribute and BinaryPayloadFilter (or equivalent generic classes). The same files can be used for ASP .Net Core 2.0 and 2.1.
- Decorate controller methods appropriately in controller.mustache.
- In Startup.mustache, add the new filter: // Support binary payloads. c.OperationFilter();