Browse Source

Update tests, separate schemas for models with the same name in different files for v1 and v2

pull/14168/head
Sebastián Ramírez 10 months ago
parent
commit
e1eedaf1ab
  1. 353
      tests/test_openapi_separate_input_output_schemas.py
  2. 447
      tests/test_pydantic_v1_v2_multifile/test_multifile.py

353
tests/test_openapi_separate_input_output_schemas.py

@ -2,6 +2,7 @@ from typing import List, Optional
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from pydantic import BaseModel from pydantic import BaseModel
from .utils import PYDANTIC_V2, needs_pydanticv2 from .utils import PYDANTIC_V2, needs_pydanticv2
@ -135,217 +136,223 @@ def test_openapi_schema():
client = get_app_client() client = get_app_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() == { 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": {
"get": { "/items/": {
"summary": "Read Items", "get": {
"operationId": "read_items_items__get", "summary": "Read Items",
"responses": { "operationId": "read_items_items__get",
"200": { "responses": {
"description": "Successful Response", "200": {
"content": { "description": "Successful Response",
"application/json": { "content": {
"schema": { "application/json": {
"items": { "schema": {
"$ref": "#/components/schemas/Item-Output" "items": {
}, "$ref": "#/components/schemas/Item-Output"
"type": "array", },
"title": "Response Read Items Items Get", "type": "array",
"title": "Response Read Items Items Get",
}
} }
} },
},
}
},
},
"post": {
"summary": "Create Item",
"operationId": "create_item_items__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/Item-Input"}
} }
}, },
"required": True,
}, },
"responses": { "post": {
"200": { "summary": "Create Item",
"description": "Successful Response", "operationId": "create_item_items__post",
"requestBody": {
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"$ref": "#/components/schemas/Item-Output" "$ref": "#/components/schemas/Item-Input"
} }
} }
}, },
"required": True,
}, },
"402": { "responses": {
"description": "Payment Required", "200": {
"content": { "description": "Successful Response",
"application/json": { "content": {
"schema": { "application/json": {
"$ref": "#/components/schemas/Item-Output" "schema": {
"$ref": "#/components/schemas/Item-Output"
}
} }
} },
}, },
}, "402": {
"422": { "description": "Payment Required",
"description": "Validation Error", "content": {
"content": { "application/json": {
"application/json": { "schema": {
"schema": { "$ref": "#/components/schemas/Item-Output"
"$ref": "#/components/schemas/HTTPValidationError" }
} }
} },
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
}, },
}, },
}, },
}, },
}, "/items-list/": {
"/items-list/": { "post": {
"post": { "summary": "Create Item List",
"summary": "Create Item List", "operationId": "create_item_list_items_list__post",
"operationId": "create_item_list_items_list__post", "requestBody": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/Item-Input"
},
"type": "array",
"title": "Item",
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"$ref": "#/components/schemas/HTTPValidationError" "items": {
"$ref": "#/components/schemas/Item-Input"
},
"type": "array",
"title": "Item",
} }
} }
}, },
"required": True,
}, },
}, "responses": {
} "200": {
}, "description": "Successful Response",
}, "content": {"application/json": {"schema": {}}},
"components": { },
"schemas": { "422": {
"HTTPValidationError": { "description": "Validation Error",
"properties": { "content": {
"detail": { "application/json": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "schema": {
"type": "array", "$ref": "#/components/schemas/HTTPValidationError"
"title": "Detail", }
} }
}, },
"type": "object", },
"title": "HTTPValidationError",
},
"Item-Input": {
"properties": {
"name": {"type": "string", "title": "Name"},
"description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "Description",
},
"sub": {
"anyOf": [
{"$ref": "#/components/schemas/SubItem-Input"},
{"type": "null"},
]
}, },
}, }
"type": "object",
"required": ["name"],
"title": "Item",
}, },
"Item-Output": { },
"properties": { "components": {
"name": {"type": "string", "title": "Name"}, "schemas": {
"description": { "HTTPValidationError": {
"anyOf": [{"type": "string"}, {"type": "null"}], "properties": {
"title": "Description", "detail": {
}, "items": {
"sub": { "$ref": "#/components/schemas/ValidationError"
"anyOf": [ },
{"$ref": "#/components/schemas/SubItem-Output"}, "type": "array",
{"type": "null"}, "title": "Detail",
] }
}, },
"type": "object",
"title": "HTTPValidationError",
}, },
"type": "object", "Item-Input": {
"required": ["name", "description", "sub"], "properties": {
"title": "Item", "name": {"type": "string", "title": "Name"},
}, "description": {
"SubItem-Input": { "anyOf": [{"type": "string"}, {"type": "null"}],
"properties": { "title": "Description",
"subname": {"type": "string", "title": "Subname"}, },
"sub_description": { "sub": {
"anyOf": [{"type": "string"}, {"type": "null"}], "anyOf": [
"title": "Sub Description", {"$ref": "#/components/schemas/SubItem-Input"},
{"type": "null"},
]
},
}, },
"tags": { "type": "object",
"items": {"type": "string"}, "required": ["name"],
"type": "array", "title": "Item",
"title": "Tags", },
"default": [], "Item-Output": {
"properties": {
"name": {"type": "string", "title": "Name"},
"description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "Description",
},
"sub": {
"anyOf": [
{"$ref": "#/components/schemas/SubItem-Output"},
{"type": "null"},
]
},
}, },
"type": "object",
"required": ["name", "description", "sub"],
"title": "Item",
}, },
"type": "object", "SubItem-Input": {
"required": ["subname"], "properties": {
"title": "SubItem", "subname": {"type": "string", "title": "Subname"},
}, "sub_description": {
"SubItem-Output": { "anyOf": [{"type": "string"}, {"type": "null"}],
"properties": { "title": "Sub Description",
"subname": {"type": "string", "title": "Subname"}, },
"sub_description": { "tags": {
"anyOf": [{"type": "string"}, {"type": "null"}], "items": {"type": "string"},
"title": "Sub Description", "type": "array",
"title": "Tags",
"default": [],
},
}, },
"tags": { "type": "object",
"items": {"type": "string"}, "required": ["subname"],
"type": "array", "title": "SubItem",
"title": "Tags", },
"default": [], "SubItem-Output": {
"properties": {
"subname": {"type": "string", "title": "Subname"},
"sub_description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "Sub Description",
},
"tags": {
"items": {"type": "string"},
"type": "array",
"title": "Tags",
"default": [],
},
}, },
"type": "object",
"required": ["subname", "sub_description", "tags"],
"title": "SubItem",
}, },
"type": "object", "ValidationError": {
"required": ["subname", "sub_description", "tags"], "properties": {
"title": "SubItem", "loc": {
}, "items": {
"ValidationError": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"properties": { },
"loc": { "type": "array",
"items": { "title": "Location",
"anyOf": [{"type": "string"}, {"type": "integer"}]
}, },
"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", }
}, )
}
},
}
@needs_pydanticv2 @needs_pydanticv2

447
tests/test_pydantic_v1_v2_multifile/test_multifile.py

@ -225,4 +225,449 @@ def test_v2_to_v1_list_to_item_empty():
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"},
"paths": {
"/v1-to-v2/item": {
"post": {
"summary": "Handle V1 Item To V2",
"operationId": "handle_v1_item_to_v2_v1_to_v2_item_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__Item"
}
],
"title": "Data",
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
"/v2-to-v1/item": {
"post": {
"summary": "Handle V2 Item To V1",
"operationId": "handle_v2_item_to_v1_v2_to_v1_item_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item"
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__Item"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
"/v1-to-v2/item-to-list": {
"post": {
"summary": "Handle V1 Item To V2 List",
"operationId": "handle_v1_item_to_v2_list_v1_to_v2_item_to_list_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__Item"
}
],
"title": "Data",
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item"
},
"type": "array",
"title": "Response Handle V1 Item To V2 List V1 To V2 Item To List Post",
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
"/v1-to-v2/list-to-list": {
"post": {
"summary": "Handle V1 List To V2 List",
"operationId": "handle_v1_list_to_v2_list_v1_to_v2_list_to_list_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__Item"
},
"type": "array",
"title": "Data",
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item"
},
"type": "array",
"title": "Response Handle V1 List To V2 List V1 To V2 List To List Post",
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
"/v1-to-v2/list-to-item": {
"post": {
"summary": "Handle V1 List To V2 Item",
"operationId": "handle_v1_list_to_v2_item_v1_to_v2_list_to_item_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__Item"
},
"type": "array",
"title": "Data",
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
"/v2-to-v1/item-to-list": {
"post": {
"summary": "Handle V2 Item To V1 List",
"operationId": "handle_v2_item_to_v1_list_v2_to_v1_item_to_list_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item"
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__Item"
},
"type": "array",
"title": "Response Handle V2 Item To V1 List V2 To V1 Item To List Post",
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
"/v2-to-v1/list-to-list": {
"post": {
"summary": "Handle V2 List To V1 List",
"operationId": "handle_v2_list_to_v1_list_v2_to_v1_list_to_list_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item"
},
"type": "array",
"title": "Data",
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__Item"
},
"type": "array",
"title": "Response Handle V2 List To V1 List V2 To V1 List To List Post",
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
"/v2-to-v1/list-to-item": {
"post": {
"summary": "Handle V2 List To V1 Item",
"operationId": "handle_v2_list_to_v1_item_v2_to_v1_list_to_item_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item"
},
"type": "array",
"title": "Data",
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__Item"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
},
"components": {
"schemas": {
"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",
},
"tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem": {
"properties": {"name": {"type": "string", "title": "Name"}},
"type": "object",
"required": ["name"],
"title": "SubItem",
},
"tests__test_pydantic_v1_v2_multifile__modelsv1__Item": {
"properties": {
"title": {"type": "string", "title": "Title"},
"size": {"type": "integer", "title": "Size"},
"description": {"type": "string", "title": "Description"},
"sub": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem"
},
"multi": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem"
},
"type": "array",
"title": "Multi",
"default": [],
},
},
"type": "object",
"required": ["title", "size", "sub"],
"title": "Item",
},
"tests__test_pydantic_v1_v2_multifile__modelsv2__Item": {
"properties": {
"new_title": {"type": "string", "title": "New Title"},
"new_size": {"type": "integer", "title": "New Size"},
"new_description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "New Description",
},
"new_sub": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem"
},
"new_multi": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem"
},
"type": "array",
"title": "New Multi",
"default": [],
},
},
"type": "object",
"required": ["new_title", "new_size", "new_sub"],
"title": "Item",
},
"tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem": {
"properties": {
"new_sub_name": {"type": "string", "title": "New Sub Name"}
},
"type": "object",
"required": ["new_sub_name"],
"title": "SubItem",
},
}
},
}
)

Loading…
Cancel
Save