-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
WithOpenApi IFormFile support is not generating valid swagger #47692
Comments
@cleftheris I believe that the issue that you're reporting is a dupe of #47526 which I ended up root causing to a problem with the way schemas are merged in Swashbuckle. You can read that analysis in #47526 (comment). The Swashbuckle issue is logged in domaindrivendev/Swashbuckle.AspNetCore#2625. I'm hoping to open a PR on the Swashbuckle repo to address this soon. |
Oh my, sorry for that I was so invested in reporting I did not do a proper search. What is amazing here is I had not realized that it worked without One lesser last thing I have stumbled across is the docs regarding Stream or pipereader that it is covered here #45265 but has been silent for a while. It generates "valid-ish" swagger.json only if I use the "/stream-without-accept": {
"post": {
"tags": [
"Issues - Stream"
],
"requestBody": {
"content": {},
"required": true
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/stream-withaccept": {
"post": {
"tags": [
"Issues - Stream"
],
"requestBody": {
"content": {
"application/octet-stream": {
"schema": {
"$ref": "#/components/schemas/Stream"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Success"
}
}
}
}, Thanks again u rule! |
Yep, you might've noticed from my comment on the issue that you linked that this is also an issue on the SB side. It looks like it's not producing the correct schema for
I'd be curious to learn about what you're doing in this front. You might be interested in upvoting #39761 or chiming in on that issue. |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
You're not trying to represent a stream in the swagger. That's purely a consumption time decision not one exposed to clients. The incoming data could be a Person, json formatted POCO and I could accept that body as a stream. |
@davidfowl I understand, but open api client generators prefer explicit specs otherwise they usually do not do the expected thing. It seemed to me as a preferable default for the OpenApi description to generate a file upload input in swagger ui. On the other hand it would limit the actual runtime behavior. Still it would be nice to use WithOpenApi extension to make it work as I intent without needing to go to Swashbuckle operation filters. |
I think we agree, I was mainly commenting on this:
That code snippet isn't the right thing to do. |
Yep it is not - I mean I agree (I am not a native speaker). I also re-read my comment above because I did not understand either why I phrased it like that😀. I was referring to the schema ref part Thanks for taking the time to check this out @davidfowl, @captainsafia |
Is there an existing issue for this?
Describe the bug
Following up on #47644 @captainsafia
When mapping any operation that has an
IFormFile
orIFormFileCollection
parameter the OpenApi configuration generated is not valid. The content type is correctly described asmultipart/form-data
but there are no 'in' parameters describing what the request should look like. Therefore the swagger ui client throws an exception when trying out the operation.So trying the example from the official docs
using WithOpenApi() and swagger-ui it fails with the the following exception
Although this actually works fine for calling the endpoint with postman (configured as form-data) the OpenApi spec is not accordingly describing it (Same goes for IFormFileCollection).
This is the fragment generated for single file upload
This is for the form file collection
So for the following
Expected Behavior
For this single file operation
the generated swagger should be the following
For this multiple files operation
the generated swagger should be the following
Steps To Reproduce
I have prepared a project here https://github.com/cleftheris/WithOpenApiBug
The relevant code is located in the UploadsApi.cs file
There are also some workarounds we have come up with in there as well but they also have shortcomings.
Just a footnote regarding our workarounds.
We use a request class with the
Accepts<UploadModel>("multiple/form-data")
and a customBindAsync
method.This works majestically and swagger ui behaves itself and the open api is valid (yey!!).
But client code generators like NSwag choke on schema references when it comes to multipart form data
requestBody
types.So for anyone who is interested in NSwag this is something to tackle with a swashbuckle operation filter (remove schema reference and copy schema parameters directly to operation request body for contentType "multipart/form-data").
Exceptions (if any)
No response
.NET Version
7.0.4
Anything else?
.NET SDK:
Version: 7.0.202
Commit: 6c74320bc3
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22621
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.202\
Host:
Version: 7.0.4
Architecture: x64
Commit: 0a396acafe
The text was updated successfully, but these errors were encountered: