mirror of https://github.com/tiangolo/fastapi.git
✅ Simplify tests for schema_extra_example (#13197)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
2c83a11a7d
commit
3e12918325
|
|
@ -1,14 +1,22 @@
|
||||||
|
import importlib
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from ...utils import needs_pydanticv2
|
from ...utils import needs_py310, needs_pydanticv2
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
@pytest.fixture(
|
||||||
def get_client():
|
name="client",
|
||||||
from docs_src.schema_extra_example.tutorial001 import app
|
params=[
|
||||||
|
"tutorial001",
|
||||||
|
pytest.param("tutorial001_py310", marks=needs_py310),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def get_client(request: pytest.FixtureRequest):
|
||||||
|
mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}")
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(mod.app)
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,22 @@
|
||||||
|
import importlib
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from ...utils import needs_pydanticv1
|
from ...utils import needs_py310, needs_pydanticv1
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
@pytest.fixture(
|
||||||
def get_client():
|
name="client",
|
||||||
from docs_src.schema_extra_example.tutorial001_pv1 import app
|
params=[
|
||||||
|
"tutorial001_pv1",
|
||||||
|
pytest.param("tutorial001_pv1_py310", marks=needs_py310),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def get_client(request: pytest.FixtureRequest):
|
||||||
|
mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}")
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(mod.app)
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,129 +0,0 @@
|
||||||
import pytest
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py310, needs_pydanticv1
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.schema_extra_example.tutorial001_pv1_py310 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
@needs_pydanticv1
|
|
||||||
def test_post_body_example(client: TestClient):
|
|
||||||
response = client.put(
|
|
||||||
"/items/5",
|
|
||||||
json={
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
@needs_pydanticv1
|
|
||||||
def test_openapi_schema(client: TestClient):
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
# insert_assert(response.json())
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"put": {
|
|
||||||
"summary": "Update Item",
|
|
||||||
"operationId": "update_item_items__item_id__put",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"required": True,
|
|
||||||
"schema": {"type": "integer", "title": "Item Id"},
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {"$ref": "#/components/schemas/Item"}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
},
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {"application/json": {"schema": {}}},
|
|
||||||
},
|
|
||||||
"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",
|
|
||||||
},
|
|
||||||
"Item": {
|
|
||||||
"properties": {
|
|
||||||
"name": {"type": "string", "title": "Name"},
|
|
||||||
"description": {"type": "string", "title": "Description"},
|
|
||||||
"price": {"type": "number", "title": "Price"},
|
|
||||||
"tax": {"type": "number", "title": "Tax"},
|
|
||||||
},
|
|
||||||
"type": "object",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"title": "Item",
|
|
||||||
"examples": [
|
|
||||||
{
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
},
|
|
||||||
"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",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,135 +0,0 @@
|
||||||
import pytest
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py310, needs_pydanticv2
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.schema_extra_example.tutorial001_py310 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
@needs_pydanticv2
|
|
||||||
def test_post_body_example(client: TestClient):
|
|
||||||
response = client.put(
|
|
||||||
"/items/5",
|
|
||||||
json={
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
@needs_pydanticv2
|
|
||||||
def test_openapi_schema(client: TestClient):
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
# insert_assert(response.json())
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"put": {
|
|
||||||
"summary": "Update Item",
|
|
||||||
"operationId": "update_item_items__item_id__put",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
"required": True,
|
|
||||||
"schema": {"type": "integer", "title": "Item Id"},
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"required": True,
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {"$ref": "#/components/schemas/Item"}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {"application/json": {"schema": {}}},
|
|
||||||
},
|
|
||||||
"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",
|
|
||||||
},
|
|
||||||
"Item": {
|
|
||||||
"properties": {
|
|
||||||
"name": {"type": "string", "title": "Name"},
|
|
||||||
"description": {
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
||||||
"title": "Description",
|
|
||||||
},
|
|
||||||
"price": {"type": "number", "title": "Price"},
|
|
||||||
"tax": {
|
|
||||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
|
||||||
"title": "Tax",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"type": "object",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"title": "Item",
|
|
||||||
"examples": [
|
|
||||||
{
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"name": "Foo",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
},
|
|
||||||
"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",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,13 +1,30 @@
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
from dirty_equals import IsDict
|
from dirty_equals import IsDict
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from docs_src.schema_extra_example.tutorial004 import app
|
from ...utils import needs_py39, needs_py310
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
|
|
||||||
|
|
||||||
# Test required and embedded body parameters with no bodies sent
|
@pytest.fixture(
|
||||||
def test_post_body_example():
|
name="client",
|
||||||
|
params=[
|
||||||
|
"tutorial004",
|
||||||
|
pytest.param("tutorial004_py310", marks=needs_py310),
|
||||||
|
"tutorial004_an",
|
||||||
|
pytest.param("tutorial004_an_py39", marks=needs_py39),
|
||||||
|
pytest.param("tutorial004_an_py310", marks=needs_py310),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def get_client(request: pytest.FixtureRequest):
|
||||||
|
mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}")
|
||||||
|
|
||||||
|
client = TestClient(mod.app)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_body_example(client: TestClient):
|
||||||
response = client.put(
|
response = client.put(
|
||||||
"/items/5",
|
"/items/5",
|
||||||
json={
|
json={
|
||||||
|
|
@ -20,7 +37,7 @@ def test_post_body_example():
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
def test_openapi_schema():
|
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() == {
|
assert response.json() == {
|
||||||
|
|
|
||||||
|
|
@ -1,168 +0,0 @@
|
||||||
from dirty_equals import IsDict
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from docs_src.schema_extra_example.tutorial004_an import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
|
|
||||||
|
|
||||||
# Test required and embedded body parameters with no bodies sent
|
|
||||||
def test_post_body_example():
|
|
||||||
response = client.put(
|
|
||||||
"/items/5",
|
|
||||||
json={
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
def test_openapi_schema():
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"put": {
|
|
||||||
"summary": "Update Item",
|
|
||||||
"operationId": "update_item_items__item_id__put",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"required": True,
|
|
||||||
"schema": {"title": "Item Id", "type": "integer"},
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": IsDict(
|
|
||||||
{
|
|
||||||
"$ref": "#/components/schemas/Item",
|
|
||||||
"examples": [
|
|
||||||
{
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
{"name": "Bar", "price": "35.4"},
|
|
||||||
{
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{
|
|
||||||
"allOf": [
|
|
||||||
{"$ref": "#/components/schemas/Item"}
|
|
||||||
],
|
|
||||||
"title": "Item",
|
|
||||||
"examples": [
|
|
||||||
{
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
{"name": "Bar", "price": "35.4"},
|
|
||||||
{
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
},
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {"application/json": {"schema": {}}},
|
|
||||||
},
|
|
||||||
"422": {
|
|
||||||
"description": "Validation Error",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/HTTPValidationError"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"HTTPValidationError": {
|
|
||||||
"title": "HTTPValidationError",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"detail": {
|
|
||||||
"title": "Detail",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"Item": {
|
|
||||||
"title": "Item",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {"title": "Name", "type": "string"},
|
|
||||||
"description": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Description",
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Description", "type": "string"}
|
|
||||||
),
|
|
||||||
"price": {"title": "Price", "type": "number"},
|
|
||||||
"tax": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Tax",
|
|
||||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Tax", "type": "number"}
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"ValidationError": {
|
|
||||||
"title": "ValidationError",
|
|
||||||
"required": ["loc", "msg", "type"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"loc": {
|
|
||||||
"title": "Location",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"msg": {"title": "Message", "type": "string"},
|
|
||||||
"type": {"title": "Error Type", "type": "string"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,177 +0,0 @@
|
||||||
import pytest
|
|
||||||
from dirty_equals import IsDict
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py310
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.schema_extra_example.tutorial004_an_py310 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
# Test required and embedded body parameters with no bodies sent
|
|
||||||
@needs_py310
|
|
||||||
def test_post_body_example(client: TestClient):
|
|
||||||
response = client.put(
|
|
||||||
"/items/5",
|
|
||||||
json={
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
def test_openapi_schema(client: TestClient):
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"put": {
|
|
||||||
"summary": "Update Item",
|
|
||||||
"operationId": "update_item_items__item_id__put",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"required": True,
|
|
||||||
"schema": {"title": "Item Id", "type": "integer"},
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": IsDict(
|
|
||||||
{
|
|
||||||
"$ref": "#/components/schemas/Item",
|
|
||||||
"examples": [
|
|
||||||
{
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
{"name": "Bar", "price": "35.4"},
|
|
||||||
{
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{
|
|
||||||
"allOf": [
|
|
||||||
{"$ref": "#/components/schemas/Item"}
|
|
||||||
],
|
|
||||||
"title": "Item",
|
|
||||||
"examples": [
|
|
||||||
{
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
{"name": "Bar", "price": "35.4"},
|
|
||||||
{
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
},
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {"application/json": {"schema": {}}},
|
|
||||||
},
|
|
||||||
"422": {
|
|
||||||
"description": "Validation Error",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/HTTPValidationError"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"HTTPValidationError": {
|
|
||||||
"title": "HTTPValidationError",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"detail": {
|
|
||||||
"title": "Detail",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"Item": {
|
|
||||||
"title": "Item",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {"title": "Name", "type": "string"},
|
|
||||||
"description": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Description",
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Description", "type": "string"}
|
|
||||||
),
|
|
||||||
"price": {"title": "Price", "type": "number"},
|
|
||||||
"tax": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Tax",
|
|
||||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Tax", "type": "number"}
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"ValidationError": {
|
|
||||||
"title": "ValidationError",
|
|
||||||
"required": ["loc", "msg", "type"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"loc": {
|
|
||||||
"title": "Location",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"msg": {"title": "Message", "type": "string"},
|
|
||||||
"type": {"title": "Error Type", "type": "string"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,177 +0,0 @@
|
||||||
import pytest
|
|
||||||
from dirty_equals import IsDict
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py39
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.schema_extra_example.tutorial004_an_py39 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
# Test required and embedded body parameters with no bodies sent
|
|
||||||
@needs_py39
|
|
||||||
def test_post_body_example(client: TestClient):
|
|
||||||
response = client.put(
|
|
||||||
"/items/5",
|
|
||||||
json={
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py39
|
|
||||||
def test_openapi_schema(client: TestClient):
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"put": {
|
|
||||||
"summary": "Update Item",
|
|
||||||
"operationId": "update_item_items__item_id__put",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"required": True,
|
|
||||||
"schema": {"title": "Item Id", "type": "integer"},
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": IsDict(
|
|
||||||
{
|
|
||||||
"$ref": "#/components/schemas/Item",
|
|
||||||
"examples": [
|
|
||||||
{
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
{"name": "Bar", "price": "35.4"},
|
|
||||||
{
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{
|
|
||||||
"allOf": [
|
|
||||||
{"$ref": "#/components/schemas/Item"}
|
|
||||||
],
|
|
||||||
"title": "Item",
|
|
||||||
"examples": [
|
|
||||||
{
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
{"name": "Bar", "price": "35.4"},
|
|
||||||
{
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
},
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {"application/json": {"schema": {}}},
|
|
||||||
},
|
|
||||||
"422": {
|
|
||||||
"description": "Validation Error",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/HTTPValidationError"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"HTTPValidationError": {
|
|
||||||
"title": "HTTPValidationError",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"detail": {
|
|
||||||
"title": "Detail",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"Item": {
|
|
||||||
"title": "Item",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {"title": "Name", "type": "string"},
|
|
||||||
"description": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Description",
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Description", "type": "string"}
|
|
||||||
),
|
|
||||||
"price": {"title": "Price", "type": "number"},
|
|
||||||
"tax": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Tax",
|
|
||||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Tax", "type": "number"}
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"ValidationError": {
|
|
||||||
"title": "ValidationError",
|
|
||||||
"required": ["loc", "msg", "type"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"loc": {
|
|
||||||
"title": "Location",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"msg": {"title": "Message", "type": "string"},
|
|
||||||
"type": {"title": "Error Type", "type": "string"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,177 +0,0 @@
|
||||||
import pytest
|
|
||||||
from dirty_equals import IsDict
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py310
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.schema_extra_example.tutorial004_py310 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
# Test required and embedded body parameters with no bodies sent
|
|
||||||
@needs_py310
|
|
||||||
def test_post_body_example(client: TestClient):
|
|
||||||
response = client.put(
|
|
||||||
"/items/5",
|
|
||||||
json={
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
def test_openapi_schema(client: TestClient):
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"put": {
|
|
||||||
"summary": "Update Item",
|
|
||||||
"operationId": "update_item_items__item_id__put",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"required": True,
|
|
||||||
"schema": {"title": "Item Id", "type": "integer"},
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": IsDict(
|
|
||||||
{
|
|
||||||
"$ref": "#/components/schemas/Item",
|
|
||||||
"examples": [
|
|
||||||
{
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
{"name": "Bar", "price": "35.4"},
|
|
||||||
{
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{
|
|
||||||
"allOf": [
|
|
||||||
{"$ref": "#/components/schemas/Item"}
|
|
||||||
],
|
|
||||||
"title": "Item",
|
|
||||||
"examples": [
|
|
||||||
{
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
{"name": "Bar", "price": "35.4"},
|
|
||||||
{
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
},
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {"application/json": {"schema": {}}},
|
|
||||||
},
|
|
||||||
"422": {
|
|
||||||
"description": "Validation Error",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/HTTPValidationError"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"HTTPValidationError": {
|
|
||||||
"title": "HTTPValidationError",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"detail": {
|
|
||||||
"title": "Detail",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"Item": {
|
|
||||||
"title": "Item",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {"title": "Name", "type": "string"},
|
|
||||||
"description": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Description",
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Description", "type": "string"}
|
|
||||||
),
|
|
||||||
"price": {"title": "Price", "type": "number"},
|
|
||||||
"tax": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Tax",
|
|
||||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Tax", "type": "number"}
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"ValidationError": {
|
|
||||||
"title": "ValidationError",
|
|
||||||
"required": ["loc", "msg", "type"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"loc": {
|
|
||||||
"title": "Location",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"msg": {"title": "Message", "type": "string"},
|
|
||||||
"type": {"title": "Error Type", "type": "string"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,13 +1,26 @@
|
||||||
|
import importlib
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from dirty_equals import IsDict
|
from dirty_equals import IsDict
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from ...utils import needs_py39, needs_py310
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.schema_extra_example.tutorial005 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
@pytest.fixture(
|
||||||
|
name="client",
|
||||||
|
params=[
|
||||||
|
"tutorial005",
|
||||||
|
pytest.param("tutorial005_py310", marks=needs_py310),
|
||||||
|
"tutorial005_an",
|
||||||
|
pytest.param("tutorial005_an_py39", marks=needs_py39),
|
||||||
|
pytest.param("tutorial005_an_py310", marks=needs_py310),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def get_client(request: pytest.FixtureRequest):
|
||||||
|
mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}")
|
||||||
|
|
||||||
|
client = TestClient(mod.app)
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,166 +0,0 @@
|
||||||
import pytest
|
|
||||||
from dirty_equals import IsDict
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.schema_extra_example.tutorial005_an import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
def test_post_body_example(client: TestClient):
|
|
||||||
response = client.put(
|
|
||||||
"/items/5",
|
|
||||||
json={
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
def test_openapi_schema(client: TestClient) -> None:
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"put": {
|
|
||||||
"summary": "Update Item",
|
|
||||||
"operationId": "update_item_items__item_id__put",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"required": True,
|
|
||||||
"schema": {"title": "Item Id", "type": "integer"},
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": IsDict({"$ref": "#/components/schemas/Item"})
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{
|
|
||||||
"allOf": [
|
|
||||||
{"$ref": "#/components/schemas/Item"}
|
|
||||||
],
|
|
||||||
"title": "Item",
|
|
||||||
}
|
|
||||||
),
|
|
||||||
"examples": {
|
|
||||||
"normal": {
|
|
||||||
"summary": "A normal example",
|
|
||||||
"description": "A **normal** item works correctly.",
|
|
||||||
"value": {
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"converted": {
|
|
||||||
"summary": "An example with converted data",
|
|
||||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
|
||||||
"value": {"name": "Bar", "price": "35.4"},
|
|
||||||
},
|
|
||||||
"invalid": {
|
|
||||||
"summary": "Invalid data is rejected with an error",
|
|
||||||
"value": {
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
},
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {"application/json": {"schema": {}}},
|
|
||||||
},
|
|
||||||
"422": {
|
|
||||||
"description": "Validation Error",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/HTTPValidationError"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"HTTPValidationError": {
|
|
||||||
"title": "HTTPValidationError",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"detail": {
|
|
||||||
"title": "Detail",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"Item": {
|
|
||||||
"title": "Item",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {"title": "Name", "type": "string"},
|
|
||||||
"description": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Description",
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Description", "type": "string"}
|
|
||||||
),
|
|
||||||
"price": {"title": "Price", "type": "number"},
|
|
||||||
"tax": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Tax",
|
|
||||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Tax", "type": "number"}
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"ValidationError": {
|
|
||||||
"title": "ValidationError",
|
|
||||||
"required": ["loc", "msg", "type"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"loc": {
|
|
||||||
"title": "Location",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"msg": {"title": "Message", "type": "string"},
|
|
||||||
"type": {"title": "Error Type", "type": "string"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,170 +0,0 @@
|
||||||
import pytest
|
|
||||||
from dirty_equals import IsDict
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py310
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.schema_extra_example.tutorial005_an_py310 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
def test_post_body_example(client: TestClient):
|
|
||||||
response = client.put(
|
|
||||||
"/items/5",
|
|
||||||
json={
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
def test_openapi_schema(client: TestClient) -> None:
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"put": {
|
|
||||||
"summary": "Update Item",
|
|
||||||
"operationId": "update_item_items__item_id__put",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"required": True,
|
|
||||||
"schema": {"title": "Item Id", "type": "integer"},
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": IsDict({"$ref": "#/components/schemas/Item"})
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{
|
|
||||||
"allOf": [
|
|
||||||
{"$ref": "#/components/schemas/Item"}
|
|
||||||
],
|
|
||||||
"title": "Item",
|
|
||||||
}
|
|
||||||
),
|
|
||||||
"examples": {
|
|
||||||
"normal": {
|
|
||||||
"summary": "A normal example",
|
|
||||||
"description": "A **normal** item works correctly.",
|
|
||||||
"value": {
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"converted": {
|
|
||||||
"summary": "An example with converted data",
|
|
||||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
|
||||||
"value": {"name": "Bar", "price": "35.4"},
|
|
||||||
},
|
|
||||||
"invalid": {
|
|
||||||
"summary": "Invalid data is rejected with an error",
|
|
||||||
"value": {
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
},
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {"application/json": {"schema": {}}},
|
|
||||||
},
|
|
||||||
"422": {
|
|
||||||
"description": "Validation Error",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/HTTPValidationError"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"HTTPValidationError": {
|
|
||||||
"title": "HTTPValidationError",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"detail": {
|
|
||||||
"title": "Detail",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"Item": {
|
|
||||||
"title": "Item",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {"title": "Name", "type": "string"},
|
|
||||||
"description": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Description",
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Description", "type": "string"}
|
|
||||||
),
|
|
||||||
"price": {"title": "Price", "type": "number"},
|
|
||||||
"tax": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Tax",
|
|
||||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Tax", "type": "number"}
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"ValidationError": {
|
|
||||||
"title": "ValidationError",
|
|
||||||
"required": ["loc", "msg", "type"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"loc": {
|
|
||||||
"title": "Location",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"msg": {"title": "Message", "type": "string"},
|
|
||||||
"type": {"title": "Error Type", "type": "string"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,170 +0,0 @@
|
||||||
import pytest
|
|
||||||
from dirty_equals import IsDict
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py39
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.schema_extra_example.tutorial005_an_py39 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py39
|
|
||||||
def test_post_body_example(client: TestClient):
|
|
||||||
response = client.put(
|
|
||||||
"/items/5",
|
|
||||||
json={
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py39
|
|
||||||
def test_openapi_schema(client: TestClient) -> None:
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"put": {
|
|
||||||
"summary": "Update Item",
|
|
||||||
"operationId": "update_item_items__item_id__put",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"required": True,
|
|
||||||
"schema": {"title": "Item Id", "type": "integer"},
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": IsDict({"$ref": "#/components/schemas/Item"})
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{
|
|
||||||
"allOf": [
|
|
||||||
{"$ref": "#/components/schemas/Item"}
|
|
||||||
],
|
|
||||||
"title": "Item",
|
|
||||||
}
|
|
||||||
),
|
|
||||||
"examples": {
|
|
||||||
"normal": {
|
|
||||||
"summary": "A normal example",
|
|
||||||
"description": "A **normal** item works correctly.",
|
|
||||||
"value": {
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"converted": {
|
|
||||||
"summary": "An example with converted data",
|
|
||||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
|
||||||
"value": {"name": "Bar", "price": "35.4"},
|
|
||||||
},
|
|
||||||
"invalid": {
|
|
||||||
"summary": "Invalid data is rejected with an error",
|
|
||||||
"value": {
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
},
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {"application/json": {"schema": {}}},
|
|
||||||
},
|
|
||||||
"422": {
|
|
||||||
"description": "Validation Error",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/HTTPValidationError"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"HTTPValidationError": {
|
|
||||||
"title": "HTTPValidationError",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"detail": {
|
|
||||||
"title": "Detail",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"Item": {
|
|
||||||
"title": "Item",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {"title": "Name", "type": "string"},
|
|
||||||
"description": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Description",
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Description", "type": "string"}
|
|
||||||
),
|
|
||||||
"price": {"title": "Price", "type": "number"},
|
|
||||||
"tax": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Tax",
|
|
||||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Tax", "type": "number"}
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"ValidationError": {
|
|
||||||
"title": "ValidationError",
|
|
||||||
"required": ["loc", "msg", "type"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"loc": {
|
|
||||||
"title": "Location",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"msg": {"title": "Message", "type": "string"},
|
|
||||||
"type": {"title": "Error Type", "type": "string"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,170 +0,0 @@
|
||||||
import pytest
|
|
||||||
from dirty_equals import IsDict
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py310
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.schema_extra_example.tutorial005_py310 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
def test_post_body_example(client: TestClient):
|
|
||||||
response = client.put(
|
|
||||||
"/items/5",
|
|
||||||
json={
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
def test_openapi_schema(client: TestClient) -> None:
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"put": {
|
|
||||||
"summary": "Update Item",
|
|
||||||
"operationId": "update_item_items__item_id__put",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"required": True,
|
|
||||||
"schema": {"title": "Item Id", "type": "integer"},
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": IsDict({"$ref": "#/components/schemas/Item"})
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{
|
|
||||||
"allOf": [
|
|
||||||
{"$ref": "#/components/schemas/Item"}
|
|
||||||
],
|
|
||||||
"title": "Item",
|
|
||||||
}
|
|
||||||
),
|
|
||||||
"examples": {
|
|
||||||
"normal": {
|
|
||||||
"summary": "A normal example",
|
|
||||||
"description": "A **normal** item works correctly.",
|
|
||||||
"value": {
|
|
||||||
"name": "Foo",
|
|
||||||
"description": "A very nice Item",
|
|
||||||
"price": 35.4,
|
|
||||||
"tax": 3.2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"converted": {
|
|
||||||
"summary": "An example with converted data",
|
|
||||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
|
||||||
"value": {"name": "Bar", "price": "35.4"},
|
|
||||||
},
|
|
||||||
"invalid": {
|
|
||||||
"summary": "Invalid data is rejected with an error",
|
|
||||||
"value": {
|
|
||||||
"name": "Baz",
|
|
||||||
"price": "thirty five point four",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
},
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {"application/json": {"schema": {}}},
|
|
||||||
},
|
|
||||||
"422": {
|
|
||||||
"description": "Validation Error",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/HTTPValidationError"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"HTTPValidationError": {
|
|
||||||
"title": "HTTPValidationError",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"detail": {
|
|
||||||
"title": "Detail",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"Item": {
|
|
||||||
"title": "Item",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {"title": "Name", "type": "string"},
|
|
||||||
"description": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Description",
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Description", "type": "string"}
|
|
||||||
),
|
|
||||||
"price": {"title": "Price", "type": "number"},
|
|
||||||
"tax": IsDict(
|
|
||||||
{
|
|
||||||
"title": "Tax",
|
|
||||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| IsDict(
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
{"title": "Tax", "type": "number"}
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"ValidationError": {
|
|
||||||
"title": "ValidationError",
|
|
||||||
"required": ["loc", "msg", "type"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"loc": {
|
|
||||||
"title": "Location",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"msg": {"title": "Message", "type": "string"},
|
|
||||||
"type": {"title": "Error Type", "type": "string"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue