Skip to content

Instantly share code, notes, and snippets.

@belltailjp
Last active January 15, 2024 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belltailjp/9d14aae5ac9deb19345e1abd535537a5 to your computer and use it in GitHub Desktop.
Save belltailjp/9d14aae5ac9deb19345e1abd535537a5 to your computer and use it in GitHub Desktop.
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class A(BaseModel):
hoge: str
class B(BaseModel):
a: A
class C(BaseModel):
c: B
@app.post("/c")
def post_hoge(c: C) -> C:
return c
from fastapi.openapi.utils import get_openapi
openapi_schema = get_openapi(
title="myapi",
version="0.0.1",
routes=app.routes,
# separate_input_output_schemas=False,
)
print(openapi_schema)
{
"openapi": "3.1.0",
"info": {
"title": "myapi",
"version": "0.0.1"
},
"paths": {
"/c": {
"post": {
"summary": "Post Hoge",
"operationId": "post_hoge_c_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/C"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/C"
}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"A": {
"properties": {
"hoge": {
"type": "string",
"title": "Hoge"
}
},
"type": "object",
"required": [
"hoge"
],
"title": "A"
},
"B": {
"properties": {
"a": {
"$ref": "#/components/schemas/A"
}
},
"type": "object",
"required": [
"a"
],
"title": "B"
},
"C": {
"properties": {
"c": {
"$ref": "#/components/schemas/B"
}
},
"type": "object",
"required": [
"c"
],
"title": "C"
},
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError"
},
"type": "array",
"title": "Detail"
}
},
"type": "object",
"title": "HTTPValidationError"
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "integer"
}
]
},
"type": "array",
"title": "Location"
},
"msg": {
"type": "string",
"title": "Message"
},
"type": {
"type": "string",
"title": "Error Type"
}
},
"type": "object",
"required": [
"loc",
"msg",
"type"
],
"title": "ValidationError"
}
}
}
}
{
"openapi": "3.1.0",
"info": {
"title": "myapi",
"version": "0.0.1"
},
"paths": {
"/c": {
"post": {
"summary": "Post Hoge",
"operationId": "post_hoge_c_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/C-Input"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/C-Output"
}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"A": {
"properties": {
"hoge": {
"type": "string",
"title": "Hoge"
}
},
"type": "object",
"required": [
"hoge"
],
"title": "A"
},
"B": {
"properties": {
"a": {
"$ref": "#/components/schemas/A"
}
},
"type": "object",
"required": [
"a"
],
"title": "B"
},
"C-Input": {
"properties": {
"c": {
"$ref": "#/components/schemas/B"
}
},
"type": "object",
"required": [
"c"
],
"title": "C"
},
"C-Output": {
"properties": {
"c": {
"$ref": "#/components/schemas/B"
}
},
"type": "object",
"required": [
"c"
],
"title": "C"
},
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError"
},
"type": "array",
"title": "Detail"
}
},
"type": "object",
"title": "HTTPValidationError"
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "integer"
}
]
},
"type": "array",
"title": "Location"
},
"msg": {
"type": "string",
"title": "Message"
},
"type": {
"type": "string",
"title": "Error Type"
}
},
"type": "object",
"required": [
"loc",
"msg",
"type"
],
"title": "ValidationError"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment