committed by
Sebastián Ramírez
7 changed files with 200 additions and 122 deletions
@ -1,8 +1,24 @@ |
|||||
from fastapi import FastAPI |
from fastapi import FastAPI |
||||
|
from fastapi.routing import APIRoute |
||||
|
|
||||
app = FastAPI() |
app = FastAPI() |
||||
|
|
||||
|
|
||||
@app.get("/items/", include_in_schema=False) |
@app.get("/items/") |
||||
async def read_items(): |
async def read_items(): |
||||
return [{"item_id": "Foo"}] |
return [{"item_id": "Foo"}] |
||||
|
|
||||
|
|
||||
|
def use_route_names_as_operation_ids(app: FastAPI) -> None: |
||||
|
""" |
||||
|
Simplify operation IDs so that generated API clients have simpler function |
||||
|
names. |
||||
|
|
||||
|
Should be called only after all routes have been added. |
||||
|
""" |
||||
|
for route in app.routes: |
||||
|
if isinstance(route, APIRoute): |
||||
|
route.operation_id = route.name # in this case, 'read_items' |
||||
|
|
||||
|
|
||||
|
use_route_names_as_operation_ids(app) |
||||
|
@ -1,30 +1,8 @@ |
|||||
from typing import Set |
|
||||
|
|
||||
from fastapi import FastAPI |
from fastapi import FastAPI |
||||
from pydantic import BaseModel |
|
||||
|
|
||||
app = FastAPI() |
app = FastAPI() |
||||
|
|
||||
|
|
||||
class Item(BaseModel): |
@app.get("/items/", include_in_schema=False) |
||||
name: str |
async def read_items(): |
||||
description: str = None |
return [{"item_id": "Foo"}] |
||||
price: float |
|
||||
tax: float = None |
|
||||
tags: Set[str] = [] |
|
||||
|
|
||||
|
|
||||
@app.post("/items/", response_model=Item, summary="Create an item") |
|
||||
async def create_item(*, item: Item): |
|
||||
""" |
|
||||
Create an item with all the information: |
|
||||
|
|
||||
- **name**: each item must have a name |
|
||||
- **description**: a long description |
|
||||
- **price**: required |
|
||||
- **tax**: if the item doesn't have tax, you can omit this |
|
||||
- **tags**: a set of unique tag strings for this item |
|
||||
\f |
|
||||
:param item: User input. |
|
||||
""" |
|
||||
return item |
|
||||
|
@ -0,0 +1,30 @@ |
|||||
|
from typing import Set |
||||
|
|
||||
|
from fastapi import FastAPI |
||||
|
from pydantic import BaseModel |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
class Item(BaseModel): |
||||
|
name: str |
||||
|
description: str = None |
||||
|
price: float |
||||
|
tax: float = None |
||||
|
tags: Set[str] = [] |
||||
|
|
||||
|
|
||||
|
@app.post("/items/", response_model=Item, summary="Create an item") |
||||
|
async def create_item(*, item: Item): |
||||
|
""" |
||||
|
Create an item with all the information: |
||||
|
|
||||
|
- **name**: each item must have a name |
||||
|
- **description**: a long description |
||||
|
- **price**: required |
||||
|
- **tax**: if the item doesn't have tax, you can omit this |
||||
|
- **tags**: a set of unique tag strings for this item |
||||
|
\f |
||||
|
:param item: User input. |
||||
|
""" |
||||
|
return item |
@ -0,0 +1,112 @@ |
|||||
|
from starlette.testclient import TestClient |
||||
|
|
||||
|
from path_operation_advanced_configuration.tutorial004 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "Fast API", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/": { |
||||
|
"post": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": {"$ref": "#/components/schemas/Item"} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"summary": "Create an item", |
||||
|
"description": "Create an item with all the information:\n\n- **name**: each item must have a name\n- **description**: a long description\n- **price**: required\n- **tax**: if the item doesn't have tax, you can omit this\n- **tags**: a set of unique tag strings for this item\n", |
||||
|
"operationId": "create_item_items__post", |
||||
|
"requestBody": { |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": {"$ref": "#/components/schemas/Item"} |
||||
|
} |
||||
|
}, |
||||
|
"required": True, |
||||
|
}, |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
"components": { |
||||
|
"schemas": { |
||||
|
"Item": { |
||||
|
"title": "Item", |
||||
|
"required": ["name", "price"], |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"name": {"title": "Name", "type": "string"}, |
||||
|
"price": {"title": "Price", "type": "number"}, |
||||
|
"description": {"title": "Description", "type": "string"}, |
||||
|
"tax": {"title": "Tax", "type": "number"}, |
||||
|
"tags": { |
||||
|
"title": "Tags", |
||||
|
"uniqueItems": True, |
||||
|
"type": "array", |
||||
|
"items": {"type": "string"}, |
||||
|
"default": [], |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"ValidationError": { |
||||
|
"title": "ValidationError", |
||||
|
"required": ["loc", "msg", "type"], |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"loc": { |
||||
|
"title": "Location", |
||||
|
"type": "array", |
||||
|
"items": {"type": "string"}, |
||||
|
}, |
||||
|
"msg": {"title": "Message", "type": "string"}, |
||||
|
"type": {"title": "Error Type", "type": "string"}, |
||||
|
}, |
||||
|
}, |
||||
|
"HTTPValidationError": { |
||||
|
"title": "HTTPValidationError", |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"detail": { |
||||
|
"title": "Detail", |
||||
|
"type": "array", |
||||
|
"items": {"$ref": "#/components/schemas/ValidationError"}, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_openapi_schema(): |
||||
|
response = client.get("/openapi.json") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == openapi_schema |
||||
|
|
||||
|
|
||||
|
def test_query_params_str_validations(): |
||||
|
response = client.post("/items/", json={"name": "Foo", "price": 42}) |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == { |
||||
|
"name": "Foo", |
||||
|
"price": 42, |
||||
|
"description": None, |
||||
|
"tax": None, |
||||
|
"tags": [], |
||||
|
} |
Loading…
Reference in new issue