Add support for extensions and updates to the OpenAPI schema in path operations (#1922)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
Edouard Lavery-Plante
2021-07-29 16:01:13 -04:00
committed by GitHub
parent 7db359182d
commit 836bb97a2d
13 changed files with 487 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
from fastapi.testclient import TestClient
from docs_src.path_operation_advanced_configuration.tutorial005 import app
client = TestClient(app)
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/items/": {
"get": {
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}
},
"summary": "Read Items",
"operationId": "read_items_items__get",
"x-aperture-labs-portal": "blue",
}
}
},
}
def test_openapi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == openapi_schema
def test_get():
response = client.get("/items/")
assert response.status_code == 200, response.text

View File

@@ -0,0 +1,59 @@
from fastapi.testclient import TestClient
from docs_src.path_operation_advanced_configuration.tutorial006 import app
client = TestClient(app)
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/items/": {
"post": {
"summary": "Create Item",
"operationId": "create_item_items__post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"required": ["name", "price"],
"type": "object",
"properties": {
"name": {"type": "string"},
"price": {"type": "number"},
"description": {"type": "string"},
},
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}
},
}
}
},
}
def test_openapi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == openapi_schema
def test_post():
response = client.post("/items/", data=b"this is actually not validated")
assert response.status_code == 200, response.text
assert response.json() == {
"size": 30,
"content": {
"name": "Maaaagic",
"price": 42,
"description": "Just kiddin', no magic here. ✨",
},
}

View File

@@ -0,0 +1,97 @@
from fastapi.testclient import TestClient
from docs_src.path_operation_advanced_configuration.tutorial007 import app
client = TestClient(app)
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/items/": {
"post": {
"summary": "Create Item",
"operationId": "create_item_items__post",
"requestBody": {
"content": {
"application/x-yaml": {
"schema": {
"title": "Item",
"required": ["name", "tags"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"tags": {
"title": "Tags",
"type": "array",
"items": {"type": "string"},
},
},
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}
},
}
}
},
}
def test_openapi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == openapi_schema
def test_post():
yaml_data = """
name: Deadpoolio
tags:
- x-force
- x-men
- x-avengers
"""
response = client.post("/items/", data=yaml_data)
assert response.status_code == 200, response.text
assert response.json() == {
"name": "Deadpoolio",
"tags": ["x-force", "x-men", "x-avengers"],
}
def test_post_broken_yaml():
yaml_data = """
name: Deadpoolio
tags:
x - x-force
x - x-men
x - x-avengers
"""
response = client.post("/items/", data=yaml_data)
assert response.status_code == 422, response.text
assert response.json() == {"detail": "Invalid YAML"}
def test_post_invalid():
yaml_data = """
name: Deadpoolio
tags:
- x-force
- x-men
- x-avengers
- sneaky: object
"""
response = client.post("/items/", data=yaml_data)
assert response.status_code == 422, response.text
assert response.json() == {
"detail": [
{"loc": ["tags", 3], "msg": "str type expected", "type": "type_error.str"}
]
}