OpenAPITools / OpenAPITools/openapi-generator

[C#] aspdotnetcore (C# server) generation does not support byte/binary body

Open
#1,327 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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:

  1. 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.
  2. Decorate controller methods appropriately in controller.mustache.
  3. In Startup.mustache, add the new filter:
    // Support binary payloads.
    c.OperationFilter();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the aspnetcore generator templates, especially controller.mustache and Startup.mustache, and review how ASP.NET Core 2.0 and 2.1 templates are organized. The issue proposes adding BinaryPayloadAttribute and BinaryPayloadFilter templates, decorating generated controller methods, and registering the filter in Startup.mustache. Done means generated byte and binary body endpoints accept requests without 415 responses and advertise application/octet-stream appropriately.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.