aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Unable to set request body format to binary in minimal api

Open FranklinWhale opened this issue 3 years ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

requestBody in swagger.json of the code below

app.MapPut("/file", Created (HttpRequest request) => {
    return TypedResults.Created("https://example.org");
})
.Accepts<byte[]>("application/zip")
.Produces(StatusCodes.Status415UnsupportedMediaType)

is

"requestBody": {
  "content": {
    "application/zip": {
      "schema": {
        "type": "string",
        "format": "byte"
      }
    }
  }
}

while .Accepts<Stream>("application/zip")

leads to

"requestBody": {
  "content": {
    "application/zip": {
      "schema": {
        "$ref": "#/components/schemas/Stream"
      }
    }
  }
}

When I want to override requestBody by adding

.WithOpenApi(o => {
    o.RequestBody = new OpenApiRequestBody {
        Content = new Dictionary<string, OpenApiMediaType>() {
            {
                "application/zip",
                new OpenApiMediaType {
                    Schema = new OpenApiSchema {
                        Type = "string",
                        Format = "binary"
                    }
                }
            }
        }
    };
    return o;
});

I find that it has no effect.

How to set format to binary?

.NET Version

7.0.100

FranklinWhale avatar Nov 24 '22 14:11 FranklinWhale