media type application/problem+json in additional responses
See original GitHub issueFirst check
- I added a very descriptive title to this issue.
- 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.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
- After submitting this, I commit to:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- Or, I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
Example
Here’s a self-contained minimal, reproducible, example with my use case:
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
app = FastAPI()
class JsonApiResponse(JSONResponse):
media_type = "application/vnd.api+json"
class JsonApiError(BaseModel):
type: str
title: str
detail: str
@app.get(
"/a/{id}",
response_class=JsonApiResponse,
responses={422: {
"model": JsonApiError,
"media_type": "application/problem+json"
},
},
)
async def a(id):
pass # pragma: no cover
Description
I would like to use a Pydantic model to specify an error message as conforming to application/problem+json, i.e. setting the media type to application/problem+json.
However, afaik, in the openAPI specification the media type is fixed to “application/json”, unless “response_class” is set with media_type
as in the above example. However, this results in all responses having that media type.
The solution you would like
I would like to be able to set the media type individually for each response.
Describe alternatives you’ve considered
I propose that in the response dict
’s the key “media_type” should be used to set the media type.
Naturally, if “media_type” is present it should take precedence over response_class which in turn overrides the default “application/json”.
@app.get(
"/a/{id}",
response_class=JsonApiResponse,
responses={422: {
"model": JsonApiError,
"media_type": "application/problem+json"
},
},
)
async def a(id):
pass # pragma: no cover
I have not come up with any other ideas on how to solve this.
In case you find my proposal reasonable, I’d be happy to provide a pull request.
Environment
- OS: macOS
- FastAPI Version: 0.63.0
- Python version: 3.8.7
Additional context
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top GitHub Comments
Such a solution generates the following for me:
While I am hoping to generate
I’m pretty sure I could solve my problem by customizing the api as described in the doc though. However I was hoping for a more elegant solution
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.