Browse Source

🐛 Support custom OpenAPI / JSON Schema fields in the generated output OpenAPI (#1429)

Co-authored-by: Sebastián Ramírez <[email protected]>
pull/3457/head
Jacob Magnusson 4 years ago
committed by GitHub
parent
commit
0ed6c92341
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      fastapi/openapi/models.py
  2. 51
      tests/test_custom_schema_fields.py

3
fastapi/openapi/models.py

@ -117,6 +117,9 @@ class SchemaBase(BaseModel):
example: Optional[Any] = None
deprecated: Optional[bool] = None
class Config:
extra: str = "allow"
class Schema(SchemaBase):
allOf: Optional[List[SchemaBase]] = None

51
tests/test_custom_schema_fields.py

@ -0,0 +1,51 @@
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
class Config:
schema_extra = {
"x-something-internal": {"level": 4},
}
@app.get("/foo", response_model=Item)
def foo():
return {"name": "Foo item"}
client = TestClient(app)
item_schema = {
"title": "Item",
"required": ["name"],
"type": "object",
"x-something-internal": {
"level": 4,
},
"properties": {
"name": {
"title": "Name",
"type": "string",
}
},
}
def test_custom_response_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json()["components"]["schemas"]["Item"] == item_schema
def test_response():
# For coverage
response = client.get("/foo")
assert response.status_code == 200, response.text
assert response.json() == {"name": "Foo item"}
Loading…
Cancel
Save