question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

media type application/problem+json in additional responses

See original GitHub issue

First 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
neverittcommented, Apr 23, 2021

Such a solution generates the following for me:

"422": {
    "description": "Unprocessable Entity",
    "content": {
         "application/problem+json": {},
         "application/vnd.api+json": {
          "schema": {
              "$ref": "#/components/schemas/JsonApiError"
           }
        }
    }
}

While I am hoping to generate

"422": {
    "description": "Unprocessable Entity",
    "content": {
         "application/problem+json": {
          "schema": {
              "$ref": "#/components/schemas/JsonApiError"
           }
        }
    }
}

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

0reactions
github-actions[bot]commented, Dec 11, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The application/problem+json Content-Type
This is called the media type of the document, and if you google for IANA Media Types, you'll find a page of all...
Read more >
Understanding Problem JSON - Sander van Beek - Medium
The HTTP header Content-Type for a Problem response must be application/problem+json . This makes Problem easily identified by the consumer.
Read more >
Media type application/problem+json lost in combination ...
I decorate a controller (action method) with the ProducesAttribute to limit the response media type to application/json . If I return a ProblemDetails ......
Read more >
REST-API Different Content-Type on Error Response
When an error occurs in one of these services Spring-MVC will throw an HttpMediaTypeNotAcceptableException. Seems to be correct cause client ...
Read more >
Handling API errors with Problem JSON
Celonis Senior Software Engineer David Hettler explains how Celonis leverages Problem JSON to solve API error handling once and for all.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found