[QUESTION] Can the OpenAPI Schema autogenerated from an UploadFile be customised?
See original GitHub issueFirst check
- [✔️] I used the GitHub search to find a similar issue and didn’t find it.
- [✔️] I searched the FastAPI documentation, with the integrated search.
- [✔️] I already searched in Google “How to X in FastAPI” and didn’t find any information.
Description
Is it possible to customise the documentation generated by fastapi when I define an UploadFile object to a POST method?
Additional context
Say for example I had a method like this where a file can be uploaded:
@router.post("/entity/new",)
async def upload_new_entity(file: UploadFile = File(...):
return {"filename": file.filename}
This seems to generate an entry in the schemas section of the docs with the pattern of Body<func_name><path><method>
So my example above will have a Schema entry with the name:
Body_upload_new_entity_entity_new_post
The docs for UploadFile don’t mention anything about OpenAPI docs.
Is it possible to specify a name for this UploadFile instead? And ideally also customise the details within the schema?
Thanks
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:12 (2 by maintainers)
Top Results From Across the Web
How to create an OpenAPI schema for an UploadFile in ...
This is a known issue that was reported on FastAPI#1442 in which the schema for file upload routes were auto-generated with no customization...
Read more >How to Generate an OpenAPI Spec From Code - BlazeMeter
You can download the autogenerated OpenAPI definition from your local web server, do your edits and then upload it to its final destination,...
Read more >An adventure in OpenAPI V3 code generation | Mux blog
OpenAPI is a way of describing your APIs in a YAML (or JSON) file. You model the data structures exposed and accepted by...
Read more >ReadMe: OpenAPI and Swagger for API Documentation
When you write your OpenAPI or Swagger docs, you can choose from either of two formats: JSON or YAML. Both will use the...
Read more >Extending OpenAPI - FastAPI - tiangolo
You can use the property .openapi_schema as a "cache", to store your generated schema. That way, your application won't have to generate the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
After some investigation this is possible, but it requires some monkey patching. Using the example given here, the solution looks like so:
Here is a solution to move the schema from the
Schemas
to the path itself in OpenAPI without touching the routing or anything:In this case, my main goal was to just not list this as a Schema in swagger. But presumably something similar could be done to rename the models (although I don’t think it’s very helpful to have them in the Schemas section, especially since they’re unique to each endpoint/method).
Since modifying/extending the OpenAPI schema is documented in the FastAPI docs and the OpenAPI schema spec itself is very well documented, I think this is a decent solution, though I’d be curious to get input from others on how reliable (or not) this might be since I feel there might be sharp edges.