Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

How to change the response type of /openapi.json #3026

Closed
captaincoordinates opened this issue Mar 31, 2021 · 4 comments
Closed

How to change the response type of /openapi.json #3026

captaincoordinates opened this issue Mar 31, 2021 · 4 comments
Labels
question Question or problem question-migrate

Comments

@captaincoordinates
Copy link

captaincoordinates commented Mar 31, 2021

There appears to be an active discussion around the preferred MIME type for OpenAPI documentation. FastAPI provides /openapi.json with the application/json MIME type.

I am attempting to use FastAPI to implement the OGC API - Features - Part 1: Core 1.0 spec which requires OpenAPI documentation be provided with MIME type application/vnd.oai.openapi+json;version=3.0 (see here).

Is it possible to change the response type of /openapi.json within FastAPI? Looking through the documentation and searching around the internet I don't see any way to do this.

@captaincoordinates captaincoordinates added the question Question or problem label Mar 31, 2021
@waynerv
Copy link
Contributor

waynerv commented Apr 1, 2021

Thanks for the flexiable of FastAPI, this is pretty much easy. You just need create a custom Response class, and ues it as response_class in route:

from typing import List, Optional

from fastapi import FastAPI
from pydantic import BaseModel
from starlette.responses import JSONResponse

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: List[str] = []


class VndResponse(JSONResponse):
    media_type = 'application/vnd.oai.openapi+json;version=3.0'


@app.post("/items/", response_model=Item, response_class=VndResponse)
async def create_item(item: Item):
    return item

image

@juntatalor
Copy link
Contributor

@captaincoordinates Hi!
The openapi handler is declared inside the FastAPI.setup() as an inner function that returns JSONresponse. I'm afraid that the only one way is to subclass FastAPI and write your own setup implementation.

@captaincoordinates
Copy link
Author

@juntatalor thanks for the info. Anyone else who's looking for this I found the described method here.

@tiangolo
Copy link
Owner

Thanks for the help here everyone! 👏 🙇

Thanks for reporting back and closing the issue @captaincoordinates 👍

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@tiangolo tiangolo reopened this Feb 27, 2023
Repository owner locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6760 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

4 participants