Last active
January 15, 2024 14:44
-
-
Save belltailjp/9d14aae5ac9deb19345e1abd535537a5 to your computer and use it in GitHub Desktop.
checking behavior of https://fastapi.tiangolo.com/how-to/separate-openapi-schemas/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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