mirror of https://github.com/tiangolo/fastapi.git
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
This commit is contained in:
parent
932659f49b
commit
0cb111e4b8
|
|
@ -347,12 +347,14 @@ def get_openapi_path(
|
||||||
response_schema = {}
|
response_schema = {}
|
||||||
|
|
||||||
route_responses = dict((str(k), v) for k, v in route.responses.items())
|
route_responses = dict((str(k), v) for k, v in route.responses.items())
|
||||||
if status_code not in route_responses or not route_responses.get(status_code).get('superimpose'):
|
if status_code not in route_responses or not route_responses.get(
|
||||||
|
status_code
|
||||||
|
).get("superimpose"):
|
||||||
operation.setdefault("responses", {}).setdefault(
|
operation.setdefault("responses", {}).setdefault(
|
||||||
status_code, {}
|
status_code, {}
|
||||||
).setdefault("content", {}).setdefault(route_response_media_type, {})[
|
).setdefault("content", {}).setdefault(
|
||||||
"schema"
|
route_response_media_type, {}
|
||||||
] = response_schema
|
)["schema"] = response_schema
|
||||||
|
|
||||||
if route.responses:
|
if route.responses:
|
||||||
operation_responses = operation.setdefault("responses", {})
|
operation_responses = operation.setdefault("responses", {})
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,10 @@
|
||||||
over the default route's response class's media_type.
|
over the default route's response class's media_type.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pydantic import BaseModel
|
|
||||||
from starlette.status import HTTP_200_OK, HTTP_500_INTERNAL_SERVER_ERROR
|
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from starlette.status import HTTP_200_OK, HTTP_500_INTERNAL_SERVER_ERROR
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
|
|
@ -22,7 +21,7 @@ class Error(BaseModel):
|
||||||
"/a",
|
"/a",
|
||||||
responses={
|
responses={
|
||||||
HTTP_200_OK: {"superimpose": True, "content": {"text/event-stream": {}}},
|
HTTP_200_OK: {"superimpose": True, "content": {"text/event-stream": {}}},
|
||||||
HTTP_500_INTERNAL_SERVER_ERROR: {"description": "Error", "model": Error}
|
HTTP_500_INTERNAL_SERVER_ERROR: {"description": "Error", "model": Error},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
def a():
|
def a():
|
||||||
|
|
@ -33,14 +32,19 @@ def a():
|
||||||
"/b",
|
"/b",
|
||||||
responses={
|
responses={
|
||||||
str(HTTP_200_OK): {"content": {"text/event-stream": {}}},
|
str(HTTP_200_OK): {"content": {"text/event-stream": {}}},
|
||||||
HTTP_500_INTERNAL_SERVER_ERROR: {"description": "Error", "model": Error}
|
HTTP_500_INTERNAL_SERVER_ERROR: {"description": "Error", "model": Error},
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
def b():
|
def b():
|
||||||
pass # pragma: no cover
|
pass # pragma: no cover
|
||||||
|
|
||||||
|
|
||||||
@app.get("/c", responses={HTTP_500_INTERNAL_SERVER_ERROR: {"description": "Error", "model": Error}})
|
@app.get(
|
||||||
|
"/c",
|
||||||
|
responses={
|
||||||
|
HTTP_500_INTERNAL_SERVER_ERROR: {"description": "Error", "model": Error}
|
||||||
|
},
|
||||||
|
)
|
||||||
def c():
|
def c():
|
||||||
pass # pragma: no cover
|
pass # pragma: no cover
|
||||||
|
|
||||||
|
|
@ -56,10 +60,7 @@ def test_openapi_schema():
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
"openapi": "3.1.0",
|
"openapi": "3.1.0",
|
||||||
"info": {
|
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||||
"title": "FastAPI",
|
|
||||||
"version": "0.1.0"
|
|
||||||
},
|
|
||||||
"paths": {
|
"paths": {
|
||||||
"/a": {
|
"/a": {
|
||||||
"get": {
|
"get": {
|
||||||
|
|
@ -68,23 +69,17 @@ def test_openapi_schema():
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "Successful Response",
|
"description": "Successful Response",
|
||||||
"content": {
|
"content": {"text/event-stream": {}},
|
||||||
"text/event-stream": {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"500": {
|
"500": {
|
||||||
"description": "Error",
|
"description": "Error",
|
||||||
"content": {
|
"content": {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
"schema": {
|
"schema": {"$ref": "#/components/schemas/Error"}
|
||||||
"$ref": "#/components/schemas/Error"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/b": {
|
"/b": {
|
||||||
|
|
@ -95,27 +90,19 @@ def test_openapi_schema():
|
||||||
"200": {
|
"200": {
|
||||||
"description": "Successful Response",
|
"description": "Successful Response",
|
||||||
"content": {
|
"content": {
|
||||||
"application/json": {
|
"application/json": {"schema": {}},
|
||||||
"schema": {
|
"text/event-stream": {},
|
||||||
|
},
|
||||||
}
|
|
||||||
},
|
|
||||||
"text/event-stream": {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"500": {
|
"500": {
|
||||||
"description": "Error",
|
"description": "Error",
|
||||||
"content": {
|
"content": {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
"schema": {
|
"schema": {"$ref": "#/components/schemas/Error"}
|
||||||
"$ref": "#/components/schemas/Error"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/c": {
|
"/c": {
|
||||||
|
|
@ -125,25 +112,17 @@ def test_openapi_schema():
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "Successful Response",
|
"description": "Successful Response",
|
||||||
"content": {
|
"content": {"application/json": {"schema": {}}},
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"500": {
|
"500": {
|
||||||
"description": "Error",
|
"description": "Error",
|
||||||
"content": {
|
"content": {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
"schema": {
|
"schema": {"$ref": "#/components/schemas/Error"}
|
||||||
"$ref": "#/components/schemas/Error"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/d": {
|
"/d": {
|
||||||
|
|
@ -153,38 +132,23 @@ def test_openapi_schema():
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "Successful Response",
|
"description": "Successful Response",
|
||||||
"content": {
|
"content": {"application/json": {"schema": {}}},
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
"components": {
|
"components": {
|
||||||
"schemas": {
|
"schemas": {
|
||||||
"Error": {
|
"Error": {
|
||||||
"properties": {
|
"properties": {
|
||||||
"status": {
|
"status": {"type": "string", "title": "Status"},
|
||||||
"type": "string",
|
"title": {"type": "string", "title": "Title"},
|
||||||
"title": "Status"
|
|
||||||
},
|
|
||||||
"title": {
|
|
||||||
"type": "string",
|
|
||||||
"title": "Title"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": ["status", "title"],
|
||||||
"status",
|
"title": "Error",
|
||||||
"title"
|
|
||||||
],
|
|
||||||
"title": "Error"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue