Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set request body format to binary in minimal api #45265

Closed
1 task done
FranklinWhale opened this issue Nov 24, 2022 · 3 comments
Closed
1 task done

Unable to set request body format to binary in minimal api #45265

FranklinWhale opened this issue Nov 24, 2022 · 3 comments
Labels
area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Status: No Recent Activity

Comments

@FranklinWhale
Copy link
Contributor

FranklinWhale commented Nov 24, 2022

Is there an existing issue for this?

  • 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

@javiercn javiercn added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Nov 24, 2022
@captainsafia
Copy link
Member

captainsafia commented Nov 30, 2022

Triage: This behavior occurs because the moment schema generation for types in OpenAPI is facilitated by Swashbuckle. Even if you set a schema specifically using WithOpenApi, it gets overridden in Swashbuckle's schema generator. See domaindrivendev/Swashbuckle.AspNetCore#2521 for more info. By default, the correct scheme isn't set here.

For now, you might want to consider using an OperationFilter in Swashbuckle to set the types whenever the return type is a stream. It would look something like this:

builder.Services.AddSwaggerGen(c =>
{
    c.OperationFilter<StreamParameterOperationFilter>();
});

public class StreamParameterOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
      var acceptsMetadata = context.ApiDescription.ActionDescriptor.EndpointMetadata.OfType<IAcceptsMetadata>();
      if (acceptsMetadata.RequestType == typeof(Stream)
      {
         // modify operation here
      }
    }

@captainsafia captainsafia added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Dec 1, 2022
@ghost
Copy link

ghost commented Dec 1, 2022

Hi @FranklinWhale. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@ghost
Copy link

ghost commented Dec 5, 2022

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Dec 8, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jan 8, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Status: No Recent Activity
Projects
None yet
Development

No branches or pull requests

3 participants