Browse Source

🎨 Format code

pull/14611/head
Sebastián Ramírez 7 months ago
parent
commit
07b9a62ff2
  1. 188
      tests/test_additional_responses_custom_model_in_callback.py
  2. 18
      tests/test_multi_body_errors.py
  3. 52
      tests/test_openapi_servers.py
  4. 140
      tests/test_regex_deprecated_body.py
  5. 58
      tests/test_tutorial/test_behind_a_proxy/test_tutorial003.py
  6. 56
      tests/test_tutorial/test_behind_a_proxy/test_tutorial004.py
  7. 92
      tests/test_tutorial/test_dataclasses/test_tutorial002.py
  8. 190
      tests/test_tutorial/test_extra_data_types/test_tutorial001.py
  9. 184
      tests/test_tutorial/test_extra_models/test_tutorial003.py
  10. 212
      tests/test_tutorial/test_request_files/test_tutorial001_02.py
  11. 186
      tests/test_tutorial/test_response_model/test_tutorial003.py
  12. 185
      tests/test_tutorial/test_response_model/test_tutorial003_01.py
  13. 164
      tests/test_tutorial/test_response_model/test_tutorial004.py
  14. 212
      tests/test_tutorial/test_response_model/test_tutorial005.py
  15. 212
      tests/test_tutorial/test_response_model/test_tutorial006.py
  16. 340
      tests/test_tutorial/test_security/test_tutorial005.py

188
tests/test_additional_responses_custom_model_in_callback.py

@ -32,110 +32,114 @@ client = TestClient(app)
def test_openapi_schema(): def test_openapi_schema():
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/": { "paths": {
"post": { "/": {
"summary": "Main Route", "post": {
"operationId": "main_route__post", "summary": "Main Route",
"parameters": [ "operationId": "main_route__post",
{ "parameters": [
"required": True, {
"schema": { "required": True,
"title": "Callback Url", "schema": {
"maxLength": 2083, "title": "Callback Url",
"minLength": 1, "maxLength": 2083,
"type": "string", "minLength": 1,
"format": "uri", "type": "string",
"format": "uri",
},
"name": "callback_url",
"in": "query",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}, },
"name": "callback_url", "422": {
"in": "query", "description": "Validation Error",
} "content": {
], "application/json": {
"responses": { "schema": {
"200": { "$ref": "#/components/schemas/HTTPValidationError"
"description": "Successful Response", }
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
} }
} },
}, },
}, },
}, "callbacks": {
"callbacks": { "callback_route": {
"callback_route": { "{$callback_url}/callback/": {
"{$callback_url}/callback/": { "get": {
"get": { "summary": "Callback Route",
"summary": "Callback Route", "operationId": "callback_route__callback_url__callback__get",
"operationId": "callback_route__callback_url__callback__get", "responses": {
"responses": { "400": {
"400": { "content": {
"content": { "application/json": {
"application/json": { "schema": {
"schema": { "$ref": "#/components/schemas/CustomModel"
"$ref": "#/components/schemas/CustomModel" }
} }
} },
"description": "Bad Request",
}, },
"description": "Bad Request", "200": {
}, "description": "Successful Response",
"200": { "content": {
"description": "Successful Response", "application/json": {"schema": {}}
"content": { },
"application/json": {"schema": {}}
}, },
}, },
}, }
} }
} }
} },
}, }
} }
} },
}, "components": {
"components": { "schemas": {
"schemas": { "CustomModel": {
"CustomModel": { "title": "CustomModel",
"title": "CustomModel", "required": ["a"],
"required": ["a"], "type": "object",
"type": "object", "properties": {"a": {"title": "A", "type": "integer"}},
"properties": {"a": {"title": "A", "type": "integer"}}, },
}, "HTTPValidationError": {
"HTTPValidationError": { "title": "HTTPValidationError",
"title": "HTTPValidationError", "type": "object",
"type": "object", "properties": {
"properties": { "detail": {
"detail": { "title": "Detail",
"title": "Detail", "type": "array",
"type": "array", "items": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "$ref": "#/components/schemas/ValidationError"
} },
}
},
}, },
}, "ValidationError": {
"ValidationError": { "title": "ValidationError",
"title": "ValidationError", "required": ["loc", "msg", "type"],
"required": ["loc", "msg", "type"], "type": "object",
"type": "object", "properties": {
"properties": { "loc": {
"loc": { "title": "Location",
"title": "Location", "type": "array",
"type": "array", "items": {
"items": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"anyOf": [{"type": "string"}, {"type": "integer"}] },
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
}, }
} },
}, }
}) )

18
tests/test_multi_body_errors.py

@ -25,14 +25,16 @@ client = TestClient(app)
def test_put_correct_body(): def test_put_correct_body():
response = client.post("/items/", json=[{"name": "Foo", "age": 5}]) response = client.post("/items/", json=[{"name": "Foo", "age": 5}])
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"item": [ {
{ "item": [
"name": "Foo", {
"age": "5", "name": "Foo",
} "age": "5",
] }
}) ]
}
)
def test_jsonable_encoder_requiring_error(): def test_jsonable_encoder_requiring_error():

52
tests/test_openapi_servers.py

@ -30,29 +30,31 @@ def test_app():
def test_openapi_schema(): def test_openapi_schema():
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"servers": [ "info": {"title": "FastAPI", "version": "0.1.0"},
{"url": "/", "description": "Default, relative server"}, "servers": [
{ {"url": "/", "description": "Default, relative server"},
"url": "http://staging.localhost.tiangolo.com:8000", {
"description": "Staging but actually localhost still", "url": "http://staging.localhost.tiangolo.com:8000",
}, "description": "Staging but actually localhost still",
{"url": "https://prod.example.com"}, },
], {"url": "https://prod.example.com"},
"paths": { ],
"/foo": { "paths": {
"get": { "/foo": {
"summary": "Foo", "get": {
"operationId": "foo_foo_get", "summary": "Foo",
"responses": { "operationId": "foo_foo_get",
"200": { "responses": {
"description": "Successful Response", "200": {
"content": {"application/json": {"schema": {}}}, "description": "Successful Response",
} "content": {"application/json": {"schema": {}}},
}, }
},
}
} }
} },
}, }
}) )

140
tests/test_regex_deprecated_body.py

@ -65,82 +65,88 @@ def test_openapi_schema():
client = get_client() client = get_client()
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/items/": { "paths": {
"post": { "/items/": {
"summary": "Read Items", "post": {
"operationId": "read_items_items__post", "summary": "Read Items",
"requestBody": { "operationId": "read_items_items__post",
"content": { "requestBody": {
"application/x-www-form-urlencoded": {
"schema": {"$ref": "#/components/schemas/Body_read_items_items__post"}
}
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": { "content": {
"application/json": { "application/x-www-form-urlencoded": {
"schema": { "schema": {
"$ref": "#/components/schemas/HTTPValidationError" "$ref": "#/components/schemas/Body_read_items_items__post"
} }
} }
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
}, },
}, },
}, }
} }
} },
}, "components": {
"components": { "schemas": {
"schemas": { "Body_read_items_items__post": {
"Body_read_items_items__post": { "properties": {
"properties": { "q": {
"q": { "anyOf": [
"anyOf": [ {"type": "string", "pattern": "^fixedquery$"},
{"type": "string", "pattern": "^fixedquery$"}, {"type": "null"},
{"type": "null"}, ],
], "title": "Q",
"title": "Q", }
} },
"type": "object",
"title": "Body_read_items_items__post",
}, },
"type": "object", "HTTPValidationError": {
"title": "Body_read_items_items__post", "properties": {
}, "detail": {
"HTTPValidationError": { "items": {
"properties": { "$ref": "#/components/schemas/ValidationError"
"detail": { },
"items": {"$ref": "#/components/schemas/ValidationError"}, "type": "array",
"type": "array", "title": "Detail",
"title": "Detail", }
} },
"type": "object",
"title": "HTTPValidationError",
}, },
"type": "object", "ValidationError": {
"title": "HTTPValidationError", "properties": {
}, "loc": {
"ValidationError": { "items": {
"properties": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"loc": { },
"items": { "type": "array",
"anyOf": [{"type": "string"}, {"type": "integer"}] "title": "Location",
}, },
"type": "array", "msg": {"type": "string", "title": "Message"},
"title": "Location", "type": {"type": "string", "title": "Error Type"},
}, },
"msg": {"type": "string", "title": "Message"}, "type": "object",
"type": {"type": "string", "title": "Error Type"}, "required": ["loc", "msg", "type"],
"title": "ValidationError",
}, },
"type": "object", }
"required": ["loc", "msg", "type"], },
"title": "ValidationError", }
}, )
}
},
})

58
tests/test_tutorial/test_behind_a_proxy/test_tutorial003.py

@ -15,32 +15,34 @@ def test_main():
def test_openapi_schema(): def test_openapi_schema():
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"servers": [ "info": {"title": "FastAPI", "version": "0.1.0"},
{"url": "/api/v1"}, "servers": [
{ {"url": "/api/v1"},
"url": "https://stag.example.com", {
"description": "Staging environment", "url": "https://stag.example.com",
}, "description": "Staging environment",
{ },
"url": "https://prod.example.com", {
"description": "Production environment", "url": "https://prod.example.com",
}, "description": "Production environment",
], },
"paths": { ],
"/app": { "paths": {
"get": { "/app": {
"summary": "Read Main", "get": {
"operationId": "read_main_app_get", "summary": "Read Main",
"responses": { "operationId": "read_main_app_get",
"200": { "responses": {
"description": "Successful Response", "200": {
"content": {"application/json": {"schema": {}}}, "description": "Successful Response",
} "content": {"application/json": {"schema": {}}},
}, }
},
}
} }
} },
}, }
}) )

56
tests/test_tutorial/test_behind_a_proxy/test_tutorial004.py

@ -15,31 +15,33 @@ def test_main():
def test_openapi_schema(): def test_openapi_schema():
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"servers": [ "info": {"title": "FastAPI", "version": "0.1.0"},
{ "servers": [
"url": "https://stag.example.com", {
"description": "Staging environment", "url": "https://stag.example.com",
}, "description": "Staging environment",
{ },
"url": "https://prod.example.com", {
"description": "Production environment", "url": "https://prod.example.com",
}, "description": "Production environment",
], },
"paths": { ],
"/app": { "paths": {
"get": { "/app": {
"summary": "Read Main", "get": {
"operationId": "read_main_app_get", "summary": "Read Main",
"responses": { "operationId": "read_main_app_get",
"200": { "responses": {
"description": "Successful Response", "200": {
"content": {"application/json": {"schema": {}}}, "description": "Successful Response",
} "content": {"application/json": {"schema": {}}},
}, }
},
}
} }
} },
}, }
}) )

92
tests/test_tutorial/test_dataclasses/test_tutorial002.py

@ -37,51 +37,53 @@ def test_get_item(client: TestClient):
def test_openapi_schema(client: TestClient): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/items/next": { "paths": {
"get": { "/items/next": {
"summary": "Read Next Item", "get": {
"operationId": "read_next_item_items_next_get", "summary": "Read Next Item",
"responses": { "operationId": "read_next_item_items_next_get",
"200": { "responses": {
"description": "Successful Response", "200": {
"content": { "description": "Successful Response",
"application/json": { "content": {
"schema": {"$ref": "#/components/schemas/Item"} "application/json": {
} "schema": {"$ref": "#/components/schemas/Item"}
}, }
} },
}, }
}
}
},
"components": {
"schemas": {
"Item": {
"title": "Item",
"required": ["name", "price"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"price": {"title": "Price", "type": "number"},
"tags": {
"title": "Tags",
"type": "array",
"items": {"type": "string"},
}, },
"description": { }
"title": "Description", }
"anyOf": [{"type": "string"}, {"type": "null"}], },
}, "components": {
"tax": { "schemas": {
"title": "Tax", "Item": {
"anyOf": [{"type": "number"}, {"type": "null"}], "title": "Item",
"required": ["name", "price"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"price": {"title": "Price", "type": "number"},
"tags": {
"title": "Tags",
"type": "array",
"items": {"type": "string"},
},
"description": {
"title": "Description",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
"tax": {
"title": "Tax",
"anyOf": [{"type": "number"}, {"type": "null"}],
},
}, },
}, }
} }
} },
}, }
}) )

190
tests/test_tutorial/test_extra_data_types/test_tutorial001.py

@ -47,111 +47,117 @@ def test_extra_types(client: TestClient):
def test_openapi_schema(client: TestClient): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/items/{item_id}": { "paths": {
"put": { "/items/{item_id}": {
"responses": { "put": {
"200": { "responses": {
"description": "Successful Response", "200": {
"content": {"application/json": {"schema": {}}}, "description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
}, },
"422": { "summary": "Read Items",
"description": "Validation Error", "operationId": "read_items_items__item_id__put",
"parameters": [
{
"required": True,
"schema": {
"title": "Item Id",
"type": "string",
"format": "uuid",
},
"name": "item_id",
"in": "path",
}
],
"requestBody": {
"required": True,
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"$ref": "#/components/schemas/HTTPValidationError" "$ref": "#/components/schemas/Body_read_items_items__item_id__put"
} }
} }
}, },
}, },
}, }
"summary": "Read Items", }
"operationId": "read_items_items__item_id__put", },
"parameters": [ "components": {
{ "schemas": {
"required": True, "Body_read_items_items__item_id__put": {
"schema": { "title": "Body_read_items_items__item_id__put",
"title": "Item Id", "type": "object",
"properties": {
"start_datetime": {
"title": "Start Datetime",
"type": "string", "type": "string",
"format": "uuid", "format": "date-time",
},
"end_datetime": {
"title": "End Datetime",
"type": "string",
"format": "date-time",
},
"repeat_at": {
"title": "Repeat At",
"anyOf": [
{"type": "string", "format": "time"},
{"type": "null"},
],
},
"process_after": {
"title": "Process After",
"type": "string",
"format": "duration",
}, },
"name": "item_id",
"in": "path",
}
],
"requestBody": {
"required": True,
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/Body_read_items_items__item_id__put"}
}
},
},
}
}
},
"components": {
"schemas": {
"Body_read_items_items__item_id__put": {
"title": "Body_read_items_items__item_id__put",
"type": "object",
"properties": {
"start_datetime": {
"title": "Start Datetime",
"type": "string",
"format": "date-time",
},
"end_datetime": {
"title": "End Datetime",
"type": "string",
"format": "date-time",
},
"repeat_at": {
"title": "Repeat At",
"anyOf": [
{"type": "string", "format": "time"},
{"type": "null"},
],
},
"process_after": {
"title": "Process After",
"type": "string",
"format": "duration",
}, },
"required": ["start_datetime", "end_datetime", "process_after"],
}, },
"required": ["start_datetime", "end_datetime", "process_after"], "ValidationError": {
}, "title": "ValidationError",
"ValidationError": { "required": ["loc", "msg", "type"],
"title": "ValidationError", "type": "object",
"required": ["loc", "msg", "type"], "properties": {
"type": "object", "loc": {
"properties": { "title": "Location",
"loc": { "type": "array",
"title": "Location", "items": {
"type": "array", "anyOf": [{"type": "string"}, {"type": "integer"}]
"items": { },
"anyOf": [{"type": "string"}, {"type": "integer"}]
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
}, "HTTPValidationError": {
"HTTPValidationError": { "title": "HTTPValidationError",
"title": "HTTPValidationError", "type": "object",
"type": "object", "properties": {
"properties": { "detail": {
"detail": { "title": "Detail",
"title": "Detail", "type": "array",
"type": "array", "items": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "$ref": "#/components/schemas/ValidationError"
} },
}
},
}, },
}, }
} },
}, }
}) )

184
tests/test_tutorial/test_extra_models/test_tutorial003.py

@ -43,99 +43,115 @@ def test_get_plane(client: TestClient):
def test_openapi_schema(client: TestClient): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/items/{item_id}": { "paths": {
"get": { "/items/{item_id}": {
"responses": { "get": {
"200": { "responses": {
"description": "Successful Response", "200": {
"content": { "description": "Successful Response",
"application/json": { "content": {
"schema": { "application/json": {
"title": "Response Read Item Items Item Id Get", "schema": {
"anyOf": [ "title": "Response Read Item Items Item Id Get",
{"$ref": "#/components/schemas/PlaneItem"}, "anyOf": [
{"$ref": "#/components/schemas/CarItem"}, {
], "$ref": "#/components/schemas/PlaneItem"
},
{
"$ref": "#/components/schemas/CarItem"
},
],
}
} }
} },
}, },
}, "422": {
"422": { "description": "Validation Error",
"description": "Validation Error", "content": {
"content": { "application/json": {
"application/json": { "schema": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError"
"$ref": "#/components/schemas/HTTPValidationError" }
} }
} },
}, },
}, },
}, "summary": "Read Item",
"summary": "Read Item", "operationId": "read_item_items__item_id__get",
"operationId": "read_item_items__item_id__get", "parameters": [
"parameters": [ {
{ "required": True,
"required": True, "schema": {"title": "Item Id", "type": "string"},
"schema": {"title": "Item Id", "type": "string"}, "name": "item_id",
"name": "item_id", "in": "path",
"in": "path", }
} ],
], }
} }
} },
}, "components": {
"components": { "schemas": {
"schemas": { "PlaneItem": {
"PlaneItem": { "title": "PlaneItem",
"title": "PlaneItem", "required": ["description", "size"],
"required": ["description", "size"], "type": "object",
"type": "object", "properties": {
"properties": { "description": {"title": "Description", "type": "string"},
"description": {"title": "Description", "type": "string"}, "type": {
"type": {"title": "Type", "type": "string", "default": "plane"}, "title": "Type",
"size": {"title": "Size", "type": "integer"}, "type": "string",
"default": "plane",
},
"size": {"title": "Size", "type": "integer"},
},
}, },
}, "CarItem": {
"CarItem": { "title": "CarItem",
"title": "CarItem", "required": ["description"],
"required": ["description"], "type": "object",
"type": "object", "properties": {
"properties": { "description": {"title": "Description", "type": "string"},
"description": {"title": "Description", "type": "string"}, "type": {
"type": {"title": "Type", "type": "string", "default": "car"}, "title": "Type",
"type": "string",
"default": "car",
},
},
}, },
}, "ValidationError": {
"ValidationError": { "title": "ValidationError",
"title": "ValidationError", "required": ["loc", "msg", "type"],
"required": ["loc", "msg", "type"], "type": "object",
"type": "object", "properties": {
"properties": { "loc": {
"loc": { "title": "Location",
"title": "Location", "type": "array",
"type": "array", "items": {
"items": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"anyOf": [{"type": "string"}, {"type": "integer"}] },
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
}, "HTTPValidationError": {
"HTTPValidationError": { "title": "HTTPValidationError",
"title": "HTTPValidationError", "type": "object",
"type": "object", "properties": {
"properties": { "detail": {
"detail": { "title": "Detail",
"title": "Detail", "type": "array",
"type": "array", "items": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "$ref": "#/components/schemas/ValidationError"
} },
}
},
}, },
}, }
} },
}, }
}) )

212
tests/test_tutorial/test_request_files/test_tutorial001_02.py

@ -59,124 +59,132 @@ def test_post_upload_file(tmp_path: Path, client: TestClient):
def test_openapi_schema(client: TestClient): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/files/": { "paths": {
"post": { "/files/": {
"summary": "Create File", "post": {
"operationId": "create_file_files__post", "summary": "Create File",
"requestBody": { "operationId": "create_file_files__post",
"content": { "requestBody": {
"multipart/form-data": {
"schema": {"$ref": "#/components/schemas/Body_create_file_files__post"}
}
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": { "content": {
"application/json": { "multipart/form-data": {
"schema": { "schema": {
"$ref": "#/components/schemas/HTTPValidationError" "$ref": "#/components/schemas/Body_create_file_files__post"
} }
} }
},
},
},
}
},
"/uploadfile/": {
"post": {
"summary": "Create Upload File",
"operationId": "create_upload_file_uploadfile__post",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {"$ref": "#/components/schemas/Body_create_upload_file_uploadfile__post"}
} }
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}, },
"422": { "responses": {
"description": "Validation Error", "200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
"/uploadfile/": {
"post": {
"summary": "Create Upload File",
"operationId": "create_upload_file_uploadfile__post",
"requestBody": {
"content": { "content": {
"application/json": { "multipart/form-data": {
"schema": { "schema": {
"$ref": "#/components/schemas/HTTPValidationError" "$ref": "#/components/schemas/Body_create_upload_file_uploadfile__post"
} }
} }
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
}, },
}, },
}, }
} },
}, },
}, "components": {
"components": { "schemas": {
"schemas": { "Body_create_file_files__post": {
"Body_create_file_files__post": { "title": "Body_create_file_files__post",
"title": "Body_create_file_files__post", "type": "object",
"type": "object", "properties": {
"properties": { "file": {
"file": { "title": "File",
"title": "File", "anyOf": [
"anyOf": [ {"type": "string", "format": "binary"},
{"type": "string", "format": "binary"}, {"type": "null"},
{"type": "null"}, ],
], }
} },
}, },
}, "Body_create_upload_file_uploadfile__post": {
"Body_create_upload_file_uploadfile__post": { "title": "Body_create_upload_file_uploadfile__post",
"title": "Body_create_upload_file_uploadfile__post", "type": "object",
"type": "object", "properties": {
"properties": { "file": {
"file": { "title": "File",
"title": "File", "anyOf": [
"anyOf": [ {"type": "string", "format": "binary"},
{"type": "string", "format": "binary"}, {"type": "null"},
{"type": "null"}, ],
], }
} },
}, },
}, "HTTPValidationError": {
"HTTPValidationError": { "title": "HTTPValidationError",
"title": "HTTPValidationError", "type": "object",
"type": "object", "properties": {
"properties": { "detail": {
"detail": { "title": "Detail",
"title": "Detail", "type": "array",
"type": "array", "items": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "$ref": "#/components/schemas/ValidationError"
} },
}
},
}, },
}, "ValidationError": {
"ValidationError": { "title": "ValidationError",
"title": "ValidationError", "required": ["loc", "msg", "type"],
"required": ["loc", "msg", "type"], "type": "object",
"type": "object", "properties": {
"properties": { "loc": {
"loc": { "title": "Location",
"title": "Location", "type": "array",
"type": "array", "items": {
"items": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"anyOf": [{"type": "string"}, {"type": "integer"}] },
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
}, }
} },
}, }
}) )

186
tests/test_tutorial/test_response_model/test_tutorial003.py

@ -42,109 +42,115 @@ def test_post_user(client: TestClient):
def test_openapi_schema(client: TestClient): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/user/": { "paths": {
"post": { "/user/": {
"responses": { "post": {
"200": { "responses": {
"description": "Successful Response", "200": {
"content": { "description": "Successful Response",
"application/json": { "content": {
"schema": {"$ref": "#/components/schemas/UserOut"} "application/json": {
} "schema": {
"$ref": "#/components/schemas/UserOut"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
}, },
}, },
"422": { "summary": "Create User",
"description": "Validation Error", "operationId": "create_user_user__post",
"requestBody": {
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {"$ref": "#/components/schemas/UserIn"}
"$ref": "#/components/schemas/HTTPValidationError"
}
} }
}, },
"required": True,
}, },
}, }
"summary": "Create User",
"operationId": "create_user_user__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/UserIn"}
}
},
"required": True,
},
} }
} },
}, "components": {
"components": { "schemas": {
"schemas": { "UserOut": {
"UserOut": { "title": "UserOut",
"title": "UserOut", "required": ["username", "email"],
"required": ["username", "email"], "type": "object",
"type": "object", "properties": {
"properties": { "username": {"title": "Username", "type": "string"},
"username": {"title": "Username", "type": "string"}, "email": {
"email": { "title": "Email",
"title": "Email", "type": "string",
"type": "string", "format": "email",
"format": "email", },
}, "full_name": {
"full_name": { "title": "Full Name",
"title": "Full Name", "anyOf": [{"type": "string"}, {"type": "null"}],
"anyOf": [{"type": "string"}, {"type": "null"}], },
}, },
}, },
}, "UserIn": {
"UserIn": { "title": "UserIn",
"title": "UserIn", "required": ["username", "password", "email"],
"required": ["username", "password", "email"], "type": "object",
"type": "object", "properties": {
"properties": { "username": {"title": "Username", "type": "string"},
"username": {"title": "Username", "type": "string"}, "password": {"title": "Password", "type": "string"},
"password": {"title": "Password", "type": "string"}, "email": {
"email": { "title": "Email",
"title": "Email", "type": "string",
"type": "string", "format": "email",
"format": "email", },
}, "full_name": {
"full_name": { "title": "Full Name",
"title": "Full Name", "anyOf": [{"type": "string"}, {"type": "null"}],
"anyOf": [{"type": "string"}, {"type": "null"}], },
}, },
}, },
}, "ValidationError": {
"ValidationError": { "title": "ValidationError",
"title": "ValidationError", "required": ["loc", "msg", "type"],
"required": ["loc", "msg", "type"], "type": "object",
"type": "object", "properties": {
"properties": { "loc": {
"loc": { "title": "Location",
"title": "Location", "type": "array",
"type": "array", "items": {
"items": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"anyOf": [{"type": "string"}, {"type": "integer"}] },
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
}, "HTTPValidationError": {
"HTTPValidationError": { "title": "HTTPValidationError",
"title": "HTTPValidationError", "type": "object",
"type": "object", "properties": {
"properties": { "detail": {
"detail": { "title": "Detail",
"title": "Detail", "type": "array",
"type": "array", "items": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "$ref": "#/components/schemas/ValidationError"
} },
}
},
}, },
}, }
} },
}, }
}) )

185
tests/test_tutorial/test_response_model/test_tutorial003_01.py

@ -42,114 +42,115 @@ def test_post_user(client: TestClient):
def test_openapi_schema(client: TestClient): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/user/": { "paths": {
"post": { "/user/": {
"summary": "Create User", "post": {
"operationId": "create_user_user__post", "summary": "Create User",
"requestBody": { "operationId": "create_user_user__post",
"content": { "requestBody": {
"application/json": {
"schema": {"$ref": "#/components/schemas/UserIn"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": { "content": {
"application/json": { "application/json": {
"schema": {"$ref": "#/components/schemas/BaseUser"} "schema": {"$ref": "#/components/schemas/UserIn"}
} }
}, },
"required": True,
}, },
"422": { "responses": {
"description": "Validation Error", "200": {
"content": { "description": "Successful Response",
"application/json": { "content": {
"schema": { "application/json": {
"$ref": "#/components/schemas/HTTPValidationError" "schema": {
"$ref": "#/components/schemas/BaseUser"
}
} }
} },
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
}, },
}, },
}, }
} }
} },
}, "components": {
"components": { "schemas": {
"schemas": { "BaseUser": {
"BaseUser": { "title": "BaseUser",
"title": "BaseUser", "required": ["username", "email"],
"required": ["username", "email"], "type": "object",
"type": "object", "properties": {
"properties": { "username": {"title": "Username", "type": "string"},
"username": {"title": "Username", "type": "string"}, "email": {
"email": { "title": "Email",
"title": "Email", "type": "string",
"type": "string", "format": "email",
"format": "email", },
}, "full_name": {
"full_name":
{
"title": "Full Name", "title": "Full Name",
"anyOf": [{"type": "string"}, {"type": "null"}], "anyOf": [{"type": "string"}, {"type": "null"}],
} },
},
,
},
},
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
}, },
}, "HTTPValidationError": {
"UserIn": { "title": "HTTPValidationError",
"title": "UserIn", "type": "object",
"required": ["username", "email", "password"], "properties": {
"type": "object", "detail": {
"properties": { "title": "Detail",
"username": {"title": "Username", "type": "string"}, "type": "array",
"email": { "items": {
"title": "Email", "$ref": "#/components/schemas/ValidationError"
"type": "string", },
"format": "email", }
}, },
"full_name": },
{ "UserIn": {
"title": "UserIn",
"required": ["username", "email", "password"],
"type": "object",
"properties": {
"username": {"title": "Username", "type": "string"},
"email": {
"title": "Email",
"type": "string",
"format": "email",
},
"full_name": {
"title": "Full Name", "title": "Full Name",
"anyOf": [{"type": "string"}, {"type": "null"}], "anyOf": [{"type": "string"}, {"type": "null"}],
} },
, "password": {"title": "Password", "type": "string"},
"password": {"title": "Password", "type": "string"}, },
}, },
}, "ValidationError": {
"ValidationError": { "title": "ValidationError",
"title": "ValidationError", "required": ["loc", "msg", "type"],
"required": ["loc", "msg", "type"], "type": "object",
"type": "object", "properties": {
"properties": { "loc": {
"loc": { "title": "Location",
"title": "Location", "type": "array",
"type": "array", "items": {
"items": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"anyOf": [{"type": "string"}, {"type": "integer"}] },
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
}, }
} },
}, }
}) )

164
tests/test_tutorial/test_response_model/test_tutorial004.py

@ -50,94 +50,98 @@ def test_get(url, data, client: TestClient):
def test_openapi_schema(client: TestClient): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/items/{item_id}": { "paths": {
"get": { "/items/{item_id}": {
"responses": { "get": {
"200": { "responses": {
"description": "Successful Response", "200": {
"content": { "description": "Successful Response",
"application/json": { "content": {
"schema": {"$ref": "#/components/schemas/Item"} "application/json": {
} "schema": {"$ref": "#/components/schemas/Item"}
}
},
}, },
}, "422": {
"422": { "description": "Validation Error",
"description": "Validation Error", "content": {
"content": { "application/json": {
"application/json": { "schema": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError"
"$ref": "#/components/schemas/HTTPValidationError" }
} }
} },
}, },
}, },
}, "summary": "Read Item",
"summary": "Read Item", "operationId": "read_item_items__item_id__get",
"operationId": "read_item_items__item_id__get", "parameters": [
"parameters": [ {
{ "required": True,
"required": True, "schema": {"title": "Item Id", "type": "string"},
"schema": {"title": "Item Id", "type": "string"}, "name": "item_id",
"name": "item_id", "in": "path",
"in": "path", }
} ],
], }
} }
} },
}, "components": {
"components": { "schemas": {
"schemas": { "Item": {
"Item": { "title": "Item",
"title": "Item", "required": ["name", "price"],
"required": ["name", "price"], "type": "object",
"type": "object", "properties": {
"properties": { "name": {"title": "Name", "type": "string"},
"name": {"title": "Name", "type": "string"}, "price": {"title": "Price", "type": "number"},
"price": {"title": "Price", "type": "number"}, "description": {
"description": { "title": "Description",
"title": "Description", "anyOf": [{"type": "string"}, {"type": "null"}],
"anyOf": [{"type": "string"}, {"type": "null"}], },
}, "tax": {"title": "Tax", "type": "number", "default": 10.5},
"tax": {"title": "Tax", "type": "number", "default": 10.5}, "tags": {
"tags": { "title": "Tags",
"title": "Tags", "type": "array",
"type": "array", "items": {"type": "string"},
"items": {"type": "string"}, "default": [],
"default": [], },
}, },
}, },
}, "ValidationError": {
"ValidationError": { "title": "ValidationError",
"title": "ValidationError", "required": ["loc", "msg", "type"],
"required": ["loc", "msg", "type"], "type": "object",
"type": "object", "properties": {
"properties": { "loc": {
"loc": { "title": "Location",
"title": "Location", "type": "array",
"type": "array", "items": {
"items": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"anyOf": [{"type": "string"}, {"type": "integer"}] },
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
}, "HTTPValidationError": {
"HTTPValidationError": { "title": "HTTPValidationError",
"title": "HTTPValidationError", "type": "object",
"type": "object", "properties": {
"properties": { "detail": {
"detail": { "title": "Detail",
"title": "Detail", "type": "array",
"type": "array", "items": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "$ref": "#/components/schemas/ValidationError"
} },
}
},
}, },
}, }
} },
}, }
}) )

212
tests/test_tutorial/test_response_model/test_tutorial005.py

@ -40,122 +40,126 @@ def test_read_item_public_data(client: TestClient):
def test_openapi_schema(client: TestClient): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/items/{item_id}/name": { "paths": {
"get": { "/items/{item_id}/name": {
"responses": { "get": {
"200": { "responses": {
"description": "Successful Response", "200": {
"content": { "description": "Successful Response",
"application/json": { "content": {
"schema": {"$ref": "#/components/schemas/Item"} "application/json": {
} "schema": {"$ref": "#/components/schemas/Item"}
}
},
}, },
}, "422": {
"422": { "description": "Validation Error",
"description": "Validation Error", "content": {
"content": { "application/json": {
"application/json": { "schema": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError"
"$ref": "#/components/schemas/HTTPValidationError" }
} }
} },
}, },
}, },
}, "summary": "Read Item Name",
"summary": "Read Item Name", "operationId": "read_item_name_items__item_id__name_get",
"operationId": "read_item_name_items__item_id__name_get", "parameters": [
"parameters": [ {
{ "required": True,
"required": True, "schema": {"title": "Item Id", "type": "string"},
"schema": {"title": "Item Id", "type": "string"}, "name": "item_id",
"name": "item_id", "in": "path",
"in": "path", }
} ],
], }
} },
}, "/items/{item_id}/public": {
"/items/{item_id}/public": { "get": {
"get": { "responses": {
"responses": { "200": {
"200": { "description": "Successful Response",
"description": "Successful Response", "content": {
"content": { "application/json": {
"application/json": { "schema": {"$ref": "#/components/schemas/Item"}
"schema": {"$ref": "#/components/schemas/Item"} }
} },
}, },
}, "422": {
"422": { "description": "Validation Error",
"description": "Validation Error", "content": {
"content": { "application/json": {
"application/json": { "schema": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError"
"$ref": "#/components/schemas/HTTPValidationError" }
} }
} },
}, },
}, },
}, "summary": "Read Item Public Data",
"summary": "Read Item Public Data", "operationId": "read_item_public_data_items__item_id__public_get",
"operationId": "read_item_public_data_items__item_id__public_get", "parameters": [
"parameters": [ {
{ "required": True,
"required": True, "schema": {"title": "Item Id", "type": "string"},
"schema": {"title": "Item Id", "type": "string"}, "name": "item_id",
"name": "item_id", "in": "path",
"in": "path", }
} ],
], }
} },
}, },
}, "components": {
"components": { "schemas": {
"schemas": { "Item": {
"Item": { "title": "Item",
"title": "Item", "required": ["name", "price"],
"required": ["name", "price"], "type": "object",
"type": "object", "properties": {
"properties": { "name": {"title": "Name", "type": "string"},
"name": {"title": "Name", "type": "string"}, "price": {"title": "Price", "type": "number"},
"price": {"title": "Price", "type": "number"}, "description": {
"description": { "title": "Description",
"title": "Description", "anyOf": [{"type": "string"}, {"type": "null"}],
"anyOf": [{"type": "string"}, {"type": "null"}], },
"tax": {"title": "Tax", "type": "number", "default": 10.5},
}, },
"tax": {"title": "Tax", "type": "number", "default": 10.5},
}, },
}, "ValidationError": {
"ValidationError": { "title": "ValidationError",
"title": "ValidationError", "required": ["loc", "msg", "type"],
"required": ["loc", "msg", "type"], "type": "object",
"type": "object", "properties": {
"properties": { "loc": {
"loc": { "title": "Location",
"title": "Location", "type": "array",
"type": "array", "items": {
"items": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"anyOf": [{"type": "string"}, {"type": "integer"}] },
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
}, "HTTPValidationError": {
"HTTPValidationError": { "title": "HTTPValidationError",
"title": "HTTPValidationError", "type": "object",
"type": "object", "properties": {
"properties": { "detail": {
"detail": { "title": "Detail",
"title": "Detail", "type": "array",
"type": "array", "items": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "$ref": "#/components/schemas/ValidationError"
} },
}
},
}, },
}, }
} },
}, }
}) )

212
tests/test_tutorial/test_response_model/test_tutorial006.py

@ -40,122 +40,126 @@ def test_read_item_public_data(client: TestClient):
def test_openapi_schema(client: TestClient): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/items/{item_id}/name": { "paths": {
"get": { "/items/{item_id}/name": {
"responses": { "get": {
"200": { "responses": {
"description": "Successful Response", "200": {
"content": { "description": "Successful Response",
"application/json": { "content": {
"schema": {"$ref": "#/components/schemas/Item"} "application/json": {
} "schema": {"$ref": "#/components/schemas/Item"}
}
},
}, },
}, "422": {
"422": { "description": "Validation Error",
"description": "Validation Error", "content": {
"content": { "application/json": {
"application/json": { "schema": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError"
"$ref": "#/components/schemas/HTTPValidationError" }
} }
} },
}, },
}, },
}, "summary": "Read Item Name",
"summary": "Read Item Name", "operationId": "read_item_name_items__item_id__name_get",
"operationId": "read_item_name_items__item_id__name_get", "parameters": [
"parameters": [ {
{ "required": True,
"required": True, "schema": {"title": "Item Id", "type": "string"},
"schema": {"title": "Item Id", "type": "string"}, "name": "item_id",
"name": "item_id", "in": "path",
"in": "path", }
} ],
], }
} },
}, "/items/{item_id}/public": {
"/items/{item_id}/public": { "get": {
"get": { "responses": {
"responses": { "200": {
"200": { "description": "Successful Response",
"description": "Successful Response", "content": {
"content": { "application/json": {
"application/json": { "schema": {"$ref": "#/components/schemas/Item"}
"schema": {"$ref": "#/components/schemas/Item"} }
} },
}, },
}, "422": {
"422": { "description": "Validation Error",
"description": "Validation Error", "content": {
"content": { "application/json": {
"application/json": { "schema": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError"
"$ref": "#/components/schemas/HTTPValidationError" }
} }
} },
}, },
}, },
}, "summary": "Read Item Public Data",
"summary": "Read Item Public Data", "operationId": "read_item_public_data_items__item_id__public_get",
"operationId": "read_item_public_data_items__item_id__public_get", "parameters": [
"parameters": [ {
{ "required": True,
"required": True, "schema": {"title": "Item Id", "type": "string"},
"schema": {"title": "Item Id", "type": "string"}, "name": "item_id",
"name": "item_id", "in": "path",
"in": "path", }
} ],
], }
} },
}, },
}, "components": {
"components": { "schemas": {
"schemas": { "Item": {
"Item": { "title": "Item",
"title": "Item", "required": ["name", "price"],
"required": ["name", "price"], "type": "object",
"type": "object", "properties": {
"properties": { "name": {"title": "Name", "type": "string"},
"name": {"title": "Name", "type": "string"}, "price": {"title": "Price", "type": "number"},
"price": {"title": "Price", "type": "number"}, "description": {
"description": { "title": "Description",
"title": "Description", "anyOf": [{"type": "string"}, {"type": "null"}],
"anyOf": [{"type": "string"}, {"type": "null"}], },
"tax": {"title": "Tax", "type": "number", "default": 10.5},
}, },
"tax": {"title": "Tax", "type": "number", "default": 10.5},
}, },
}, "ValidationError": {
"ValidationError": { "title": "ValidationError",
"title": "ValidationError", "required": ["loc", "msg", "type"],
"required": ["loc", "msg", "type"], "type": "object",
"type": "object", "properties": {
"properties": { "loc": {
"loc": { "title": "Location",
"title": "Location", "type": "array",
"type": "array", "items": {
"items": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"anyOf": [{"type": "string"}, {"type": "integer"}] },
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
}, "HTTPValidationError": {
"HTTPValidationError": { "title": "HTTPValidationError",
"title": "HTTPValidationError", "type": "object",
"type": "object", "properties": {
"properties": { "detail": {
"detail": { "title": "Detail",
"title": "Detail", "type": "array",
"type": "array", "items": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "$ref": "#/components/schemas/ValidationError"
} },
}
},
}, },
}, }
} },
}, }
}) )

340
tests/test_tutorial/test_security/test_tutorial005.py

@ -215,192 +215,200 @@ def test_openapi_schema(mod: ModuleType):
client = TestClient(mod.app) client = TestClient(mod.app)
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == snapshot({ assert response.json() == snapshot(
"openapi": "3.1.0", {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/token": { "paths": {
"post": { "/token": {
"responses": { "post": {
"200": { "responses": {
"description": "Successful Response", "200": {
"content": { "description": "Successful Response",
"application/json": { "content": {
"schema": {"$ref": "#/components/schemas/Token"} "application/json": {
} "schema": {"$ref": "#/components/schemas/Token"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
}, },
}, },
"422": { "summary": "Login For Access Token",
"description": "Validation Error", "operationId": "login_for_access_token_token_post",
"requestBody": {
"content": { "content": {
"application/json": { "application/x-www-form-urlencoded": {
"schema": { "schema": {
"$ref": "#/components/schemas/HTTPValidationError" "$ref": "#/components/schemas/Body_login_for_access_token_token_post"
} }
} }
}, },
"required": True,
}, },
}, }
"summary": "Login For Access Token", },
"operationId": "login_for_access_token_token_post", "/users/me/": {
"requestBody": { "get": {
"content": { "responses": {
"application/x-www-form-urlencoded": { "200": {
"schema": { "description": "Successful Response",
"$ref": "#/components/schemas/Body_login_for_access_token_token_post" "content": {
} "application/json": {
"schema": {"$ref": "#/components/schemas/User"}
}
},
} }
}, },
"required": True, "summary": "Read Users Me",
}, "operationId": "read_users_me_users_me__get",
} "security": [{"OAuth2PasswordBearer": ["me"]}],
}, }
"/users/me/": {
"get": {
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/User"}
}
},
}
},
"summary": "Read Users Me",
"operationId": "read_users_me_users_me__get",
"security": [{"OAuth2PasswordBearer": ["me"]}],
}
},
"/users/me/items/": {
"get": {
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}
},
"summary": "Read Own Items",
"operationId": "read_own_items_users_me_items__get",
"security": [{"OAuth2PasswordBearer": ["items", "me"]}],
}
},
"/status/": {
"get": {
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}
},
"summary": "Read System Status",
"operationId": "read_system_status_status__get",
"security": [{"OAuth2PasswordBearer": []}],
}
},
},
"components": {
"schemas": {
"User": {
"title": "User",
"required": ["username"],
"type": "object",
"properties": {
"username": {"title": "Username", "type": "string"},
"email": {
"title": "Email",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
"full_name": {
"title": "Full Name",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
"disabled": {
"title": "Disabled",
"anyOf": [{"type": "boolean"}, {"type": "null"}],
},
},
}, },
"Token": { "/users/me/items/": {
"title": "Token", "get": {
"required": ["access_token", "token_type"], "responses": {
"type": "object", "200": {
"properties": { "description": "Successful Response",
"access_token": {"title": "Access Token", "type": "string"}, "content": {"application/json": {"schema": {}}},
"token_type": {"title": "Token Type", "type": "string"}, }
}, },
"summary": "Read Own Items",
"operationId": "read_own_items_users_me_items__get",
"security": [{"OAuth2PasswordBearer": ["items", "me"]}],
}
}, },
"Body_login_for_access_token_token_post": { "/status/": {
"title": "Body_login_for_access_token_token_post", "get": {
"required": ["username", "password"], "responses": {
"type": "object", "200": {
"properties": { "description": "Successful Response",
"grant_type": { "content": {"application/json": {"schema": {}}},
"title": "Grant Type", }
"anyOf": [
{"pattern": "^password$", "type": "string"},
{"type": "null"},
],
}, },
"username": {"title": "Username", "type": "string"}, "summary": "Read System Status",
"password": { "operationId": "read_system_status_status__get",
"title": "Password", "security": [{"OAuth2PasswordBearer": []}],
"type": "string", }
"format": "password", },
},
"components": {
"schemas": {
"User": {
"title": "User",
"required": ["username"],
"type": "object",
"properties": {
"username": {"title": "Username", "type": "string"},
"email": {
"title": "Email",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
"full_name": {
"title": "Full Name",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
"disabled": {
"title": "Disabled",
"anyOf": [{"type": "boolean"}, {"type": "null"}],
},
}, },
"scope": {"title": "Scope", "type": "string", "default": ""}, },
"client_id": { "Token": {
"title": "Client Id", "title": "Token",
"anyOf": [{"type": "string"}, {"type": "null"}], "required": ["access_token", "token_type"],
"type": "object",
"properties": {
"access_token": {"title": "Access Token", "type": "string"},
"token_type": {"title": "Token Type", "type": "string"},
}, },
"client_secret": { },
"title": "Client Secret", "Body_login_for_access_token_token_post": {
"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Body_login_for_access_token_token_post",
"format": "password", "required": ["username", "password"],
"type": "object",
"properties": {
"grant_type": {
"title": "Grant Type",
"anyOf": [
{"pattern": "^password$", "type": "string"},
{"type": "null"},
],
},
"username": {"title": "Username", "type": "string"},
"password": {
"title": "Password",
"type": "string",
"format": "password",
},
"scope": {
"title": "Scope",
"type": "string",
"default": "",
},
"client_id": {
"title": "Client Id",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
"client_secret": {
"title": "Client Secret",
"anyOf": [{"type": "string"}, {"type": "null"}],
"format": "password",
},
}, },
}, },
}, "ValidationError": {
"ValidationError": { "title": "ValidationError",
"title": "ValidationError", "required": ["loc", "msg", "type"],
"required": ["loc", "msg", "type"], "type": "object",
"type": "object", "properties": {
"properties": { "loc": {
"loc": { "title": "Location",
"title": "Location", "type": "array",
"type": "array", "items": {
"items": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"anyOf": [{"type": "string"}, {"type": "integer"}] },
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
}, },
}, "HTTPValidationError": {
"HTTPValidationError": { "title": "HTTPValidationError",
"title": "HTTPValidationError", "type": "object",
"type": "object", "properties": {
"properties": { "detail": {
"detail": { "title": "Detail",
"title": "Detail", "type": "array",
"type": "array", "items": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "$ref": "#/components/schemas/ValidationError"
} },
}
},
}, },
}, },
"securitySchemes": {
"OAuth2PasswordBearer": {
"type": "oauth2",
"flows": {
"password": {
"scopes": {
"me": "Read information about the current user.",
"items": "Read items.",
},
"tokenUrl": "token",
}
},
}
},
}, },
"securitySchemes": { }
"OAuth2PasswordBearer": { )
"type": "oauth2",
"flows": {
"password": {
"scopes": {
"me": "Read information about the current user.",
"items": "Read items.",
},
"tokenUrl": "token",
}
},
}
},
},
})

Loading…
Cancel
Save