openapi

Set the media type of a custom Error Response via a pydantic model in FastAPI

Set the media type of a custom Error Response via a pydantic model in FastAPI Question: In my FastAPI application I want to return my errors as RFC Problem JSON: from pydantic import BaseModel class RFCProblemJSON(BaseModel): type: str title: str detail: str | None status: int | None I can set the response model in …

Total answers: 1

Validate Pydantic dynamic float enum by name with OpenAPI description

Validate Pydantic dynamic float enum by name with OpenAPI description Question: Following on from this question and this discussion I am now trying to create a Pydantic BaseModel that has a field with a float Enum that is created dynamically and is validated by name. (Down the track I will probably want to use Decimal …

Total answers: 3

TypeError: Object of type 'type' is not JSON serializable

TypeError: Object of type 'type' is not JSON serializable Question: The code works fine in Postman and provides a valid response but fails to generate the OpenAPI/Swagger UI automatic docs. class Role(str, Enum): Internal = "internal" External = "external" class Info(BaseModel): id: int role: Role class AppInfo(Info): info: str @app.post("/api/v1/create", status_code=status.HTTP_200_OK) async def create(info: Info, …

Total answers: 2

How and why to supply scalar scope to FastAPI oAuth2 scopes dict?

How and why to supply scalar scope to FastAPI oAuth2 scopes dict? Question: I am using FastAPI with Python 3.9. I haven’t been able to get the available oAuth2 dependencies to work with our particular Azure token authentication, and my initial attempt at using fastapi-azure-auth didn’t seem to match either. I am therefore currently sub-classing …

Total answers: 1

Enable "Try it out" in OpenAPI so that no need to click

Enable "Try it out" in OpenAPI so that no need to click Question: I’m using FastAPI and OpenAPI/Swagger UI to see and test my endpoints. Each time I use an endpoint for the first time, in order to test it, I have to first click the Try it out button, which is getting tedious. Is …

Total answers: 1

How to add drop down menu to Swagger UI autodocs based on BaseModel using FastAPI?

How to add drop down menu to Swagger UI autodocs based on BaseModel using FastAPI? Question: I have this following class: class Quiz(BaseModel): question: str subject: str choice: str = Query(choices=(‘eu’, ‘us’, ‘cn’, ‘ru’)) I can render the form bases on this class like this @api.post("/postdata") def post_data(form_data: Quiz = Depends()): return form_data How can …

Total answers: 1

How to authorize OpenAPI/Swagger UI page in FastAPI?

How to authorize OpenAPI/Swagger UI page in FastAPI? Question: I’m building a FastAPI application with OAuth2 and JWT authentication. I’ve got two endpoints that create the JWT token. The first is hidden from the OpenAPI page but is used by the page Authorize button. The second does the same functionality but is available to the …

Total answers: 1

REST related/nested objects URL standard

REST related/nested objects URL standard Question: if /wallet returns a list a wallets and each wallet has a list of transactions. What is the standard OpenAPI/REST standard? For example, http://localhost:8000/api/wallets/ gives me { "count": 1, "next": null, "previous": null, "results": [ { "user": 1, "address": "3E8ociqZa9mZUSwGdSmAEMAoAxBK3FNDcd", "balance": "2627199.00000000" } ] } http://localhost:8000/api/wallets/3E8ociqZa9mZUSwGdSmAEMAoAxBK3FNDcd/ gives me { …

Total answers: 1

How to add image to Swagger UI autodocs using FastAPI?

How to add image to Swagger UI autodocs using FastAPI? Question: I would like to add an image to the FastAPI automatic documentation (provided by Swagger UI), but I can’t figure out how to do this. This is the code: @api.get(path=’/carbon-credit/’, responses={ 200: {‘description’: ‘Ok’, "content": { "image/jpeg": { "example": ‘https://picsum.photos/seed/picsum/200/300’ } }}, 404: {"description": …

Total answers: 1

Remove default `application/json` header from fastapi response

Remove default `application/json` header from fastapi response Question: I have taken this (https://fastapi.tiangolo.com/advanced/response-directly/#returning-a-custom-response) example from fastapi documentation regarding how to return a custom response from a fastapi application. This is my example code that i came up with to test it : from http.client import responses from fastapi import FastAPI, Response app = FastAPI() response_examples …

Total answers: 1