mirror of
https://github.com/rjNemo/fastapi
synced 2026-06-11 21:16:45 +00:00
parent
2ddb804940
commit
b7d184363f
2 changed files with 113 additions and 3 deletions
|
|
@ -99,7 +99,7 @@ class SchemaBase(BaseModel):
|
||||||
not_: Optional[List[Any]] = PSchema(None, alias="not") # type: ignore
|
not_: Optional[List[Any]] = PSchema(None, alias="not") # type: ignore
|
||||||
items: Optional[Any] = None
|
items: Optional[Any] = None
|
||||||
properties: Optional[Dict[str, Any]] = None
|
properties: Optional[Dict[str, Any]] = None
|
||||||
additionalProperties: Optional[Union[bool, Any]] = None
|
additionalProperties: Optional[Union[Dict[str, Any], bool]] = None
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
format: Optional[str] = None
|
format: Optional[str] = None
|
||||||
default: Optional[Any] = None
|
default: Optional[Any] = None
|
||||||
|
|
@ -120,7 +120,7 @@ class Schema(SchemaBase):
|
||||||
not_: Optional[List[SchemaBase]] = PSchema(None, alias="not") # type: ignore
|
not_: Optional[List[SchemaBase]] = PSchema(None, alias="not") # type: ignore
|
||||||
items: Optional[SchemaBase] = None
|
items: Optional[SchemaBase] = None
|
||||||
properties: Optional[Dict[str, SchemaBase]] = None
|
properties: Optional[Dict[str, SchemaBase]] = None
|
||||||
additionalProperties: Optional[Union[bool, SchemaBase]] = None
|
additionalProperties: Optional[Union[SchemaBase, bool]] = None
|
||||||
|
|
||||||
|
|
||||||
class Example(BaseModel):
|
class Example(BaseModel):
|
||||||
|
|
@ -220,7 +220,7 @@ class Operation(BaseModel):
|
||||||
operationId: Optional[str] = None
|
operationId: Optional[str] = None
|
||||||
parameters: Optional[List[Union[Parameter, Reference]]] = None
|
parameters: Optional[List[Union[Parameter, Reference]]] = None
|
||||||
requestBody: Optional[Union[RequestBody, Reference]] = None
|
requestBody: Optional[Union[RequestBody, Reference]] = None
|
||||||
responses: Union[Responses, Dict[Union[str], Response]]
|
responses: Union[Responses, Dict[str, Response]]
|
||||||
# Workaround OpenAPI recursive reference
|
# Workaround OpenAPI recursive reference
|
||||||
callbacks: Optional[Dict[str, Union[Dict[str, Any], Reference]]] = None
|
callbacks: Optional[Dict[str, Union[Dict[str, Any], Reference]]] = None
|
||||||
deprecated: Optional[bool] = None
|
deprecated: Optional[bool] = None
|
||||||
|
|
|
||||||
110
tests/test_additional_properties.py
Normal file
110
tests/test_additional_properties.py
Normal file
|
|
@ -0,0 +1,110 @@
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from starlette.testclient import TestClient
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
class Items(BaseModel):
|
||||||
|
items: Dict[str, int]
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/foo")
|
||||||
|
def foo(items: Items):
|
||||||
|
return items.items
|
||||||
|
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
openapi_schema = {
|
||||||
|
"openapi": "3.0.2",
|
||||||
|
"info": {"title": "Fast API", "version": "0.1.0"},
|
||||||
|
"paths": {
|
||||||
|
"/foo": {
|
||||||
|
"post": {
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Validation Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/HTTPValidationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"summary": "Foo Post",
|
||||||
|
"operationId": "foo_foo_post",
|
||||||
|
"requestBody": {
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {"$ref": "#/components/schemas/Items"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"Items": {
|
||||||
|
"title": "Items",
|
||||||
|
"required": ["items"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"items": {
|
||||||
|
"title": "Items",
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": {"type": "integer"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"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_additional_properties_schema():
|
||||||
|
response = client.get("/openapi.json")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == openapi_schema
|
||||||
|
|
||||||
|
|
||||||
|
def test_additional_properties_post():
|
||||||
|
response = client.post("/foo", json={"items": {"foo": 1, "bar": 2}})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"foo": 1, "bar": 2}
|
||||||
Loading…
Reference in a new issue