mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-27 08:10:57 -05:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd4e78ca7b | ||
|
|
8a198fc1ed | ||
|
|
6bd4f53531 | ||
|
|
07e1dea467 | ||
|
|
0f105d9076 | ||
|
|
4d83f984cc |
@@ -3,6 +3,16 @@
|
||||
## Latest Changes
|
||||
|
||||
|
||||
## 0.99.1
|
||||
|
||||
### Fixes
|
||||
|
||||
* 🐛 Fix JSON Schema accepting bools as valid JSON Schemas, e.g. `additionalProperties: false`. PR [#9781](https://github.com/tiangolo/fastapi/pull/9781) by [@tiangolo](https://github.com/tiangolo).
|
||||
|
||||
### Docs
|
||||
|
||||
* 📝 Update source examples to use new JSON Schema examples field. PR [#9776](https://github.com/tiangolo/fastapi/pull/9776) by [@tiangolo](https://github.com/tiangolo).
|
||||
|
||||
## 0.99.0
|
||||
|
||||
### Features
|
||||
|
||||
@@ -116,19 +116,19 @@ You can of course also pass multiple `examples`:
|
||||
|
||||
=== "Python 3.10+"
|
||||
|
||||
```Python hl_lines="23-49"
|
||||
```Python hl_lines="23-38"
|
||||
{!> ../../../docs_src/schema_extra_example/tutorial004_an_py310.py!}
|
||||
```
|
||||
|
||||
=== "Python 3.9+"
|
||||
|
||||
```Python hl_lines="23-49"
|
||||
```Python hl_lines="23-38"
|
||||
{!> ../../../docs_src/schema_extra_example/tutorial004_an_py39.py!}
|
||||
```
|
||||
|
||||
=== "Python 3.6+"
|
||||
|
||||
```Python hl_lines="24-50"
|
||||
```Python hl_lines="24-39"
|
||||
{!> ../../../docs_src/schema_extra_example/tutorial004_an.py!}
|
||||
```
|
||||
|
||||
@@ -137,7 +137,7 @@ You can of course also pass multiple `examples`:
|
||||
!!! tip
|
||||
Prefer to use the `Annotated` version if possible.
|
||||
|
||||
```Python hl_lines="19-45"
|
||||
```Python hl_lines="19-34"
|
||||
{!> ../../../docs_src/schema_extra_example/tutorial004_py310.py!}
|
||||
```
|
||||
|
||||
@@ -146,7 +146,7 @@ You can of course also pass multiple `examples`:
|
||||
!!! tip
|
||||
Prefer to use the `Annotated` version if possible.
|
||||
|
||||
```Python hl_lines="21-47"
|
||||
```Python hl_lines="21-36"
|
||||
{!> ../../../docs_src/schema_extra_example/tutorial004.py!}
|
||||
```
|
||||
|
||||
|
||||
@@ -20,29 +20,18 @@ async def update_item(
|
||||
item: Item = Body(
|
||||
examples=[
|
||||
{
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
{
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
{
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
],
|
||||
),
|
||||
|
||||
@@ -23,29 +23,18 @@ async def update_item(
|
||||
Body(
|
||||
examples=[
|
||||
{
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
{
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
{
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
],
|
||||
),
|
||||
|
||||
@@ -22,29 +22,18 @@ async def update_item(
|
||||
Body(
|
||||
examples=[
|
||||
{
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
{
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
{
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
],
|
||||
),
|
||||
|
||||
@@ -22,29 +22,18 @@ async def update_item(
|
||||
Body(
|
||||
examples=[
|
||||
{
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
{
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
{
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
],
|
||||
),
|
||||
|
||||
@@ -18,29 +18,18 @@ async def update_item(
|
||||
item: Item = Body(
|
||||
examples=[
|
||||
{
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
{
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
"name": "Bar",
|
||||
"price": "35.4",
|
||||
},
|
||||
{
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
],
|
||||
),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""FastAPI framework, high performance, easy to learn, fast to code, ready for production"""
|
||||
|
||||
__version__ = "0.99.0"
|
||||
__version__ = "0.99.1"
|
||||
|
||||
from starlette import status as status
|
||||
|
||||
|
||||
@@ -114,27 +114,30 @@ class Schema(BaseModel):
|
||||
dynamicAnchor: Optional[str] = Field(default=None, alias="$dynamicAnchor")
|
||||
ref: Optional[str] = Field(default=None, alias="$ref")
|
||||
dynamicRef: Optional[str] = Field(default=None, alias="$dynamicRef")
|
||||
defs: Optional[Dict[str, "Schema"]] = Field(default=None, alias="$defs")
|
||||
defs: Optional[Dict[str, "SchemaOrBool"]] = Field(default=None, alias="$defs")
|
||||
comment: Optional[str] = Field(default=None, alias="$comment")
|
||||
# Ref: JSON Schema 2020-12: https://json-schema.org/draft/2020-12/json-schema-core.html#name-a-vocabulary-for-applying-s
|
||||
# A Vocabulary for Applying Subschemas
|
||||
allOf: Optional[List["Schema"]] = None
|
||||
anyOf: Optional[List["Schema"]] = None
|
||||
oneOf: Optional[List["Schema"]] = None
|
||||
not_: Optional["Schema"] = Field(default=None, alias="not")
|
||||
if_: Optional["Schema"] = Field(default=None, alias="if")
|
||||
then: Optional["Schema"] = None
|
||||
else_: Optional["Schema"] = Field(default=None, alias="else")
|
||||
dependentSchemas: Optional[Dict[str, "Schema"]] = None
|
||||
prefixItems: Optional[List["Schema"]] = None
|
||||
items: Optional[Union["Schema", List["Schema"]]] = None
|
||||
contains: Optional["Schema"] = None
|
||||
properties: Optional[Dict[str, "Schema"]] = None
|
||||
patternProperties: Optional[Dict[str, "Schema"]] = None
|
||||
additionalProperties: Optional["Schema"] = None
|
||||
propertyNames: Optional["Schema"] = None
|
||||
unevaluatedItems: Optional["Schema"] = None
|
||||
unevaluatedProperties: Optional["Schema"] = None
|
||||
allOf: Optional[List["SchemaOrBool"]] = None
|
||||
anyOf: Optional[List["SchemaOrBool"]] = None
|
||||
oneOf: Optional[List["SchemaOrBool"]] = None
|
||||
not_: Optional["SchemaOrBool"] = Field(default=None, alias="not")
|
||||
if_: Optional["SchemaOrBool"] = Field(default=None, alias="if")
|
||||
then: Optional["SchemaOrBool"] = None
|
||||
else_: Optional["SchemaOrBool"] = Field(default=None, alias="else")
|
||||
dependentSchemas: Optional[Dict[str, "SchemaOrBool"]] = None
|
||||
prefixItems: Optional[List["SchemaOrBool"]] = None
|
||||
# TODO: uncomment and remove below when deprecating Pydantic v1
|
||||
# It generales a list of schemas for tuples, before prefixItems was available
|
||||
# items: Optional["SchemaOrBool"] = None
|
||||
items: Optional[Union["SchemaOrBool", List["SchemaOrBool"]]] = None
|
||||
contains: Optional["SchemaOrBool"] = None
|
||||
properties: Optional[Dict[str, "SchemaOrBool"]] = None
|
||||
patternProperties: Optional[Dict[str, "SchemaOrBool"]] = None
|
||||
additionalProperties: Optional["SchemaOrBool"] = None
|
||||
propertyNames: Optional["SchemaOrBool"] = None
|
||||
unevaluatedItems: Optional["SchemaOrBool"] = None
|
||||
unevaluatedProperties: Optional["SchemaOrBool"] = None
|
||||
# Ref: JSON Schema Validation 2020-12: https://json-schema.org/draft/2020-12/json-schema-validation.html#name-a-vocabulary-for-structural
|
||||
# A Vocabulary for Structural Validation
|
||||
type: Optional[str] = None
|
||||
@@ -164,7 +167,7 @@ class Schema(BaseModel):
|
||||
# A Vocabulary for the Contents of String-Encoded Data
|
||||
contentEncoding: Optional[str] = None
|
||||
contentMediaType: Optional[str] = None
|
||||
contentSchema: Optional["Schema"] = None
|
||||
contentSchema: Optional["SchemaOrBool"] = None
|
||||
# Ref: JSON Schema Validation 2020-12: https://json-schema.org/draft/2020-12/json-schema-validation.html#name-a-vocabulary-for-basic-meta
|
||||
# A Vocabulary for Basic Meta-Data Annotations
|
||||
title: Optional[str] = None
|
||||
@@ -191,6 +194,11 @@ class Schema(BaseModel):
|
||||
extra: str = "allow"
|
||||
|
||||
|
||||
# Ref: https://json-schema.org/draft/2020-12/json-schema-core.html#name-json-schema-documents
|
||||
# A JSON Schema MUST be an object or a boolean.
|
||||
SchemaOrBool = Union[Schema, bool]
|
||||
|
||||
|
||||
class Example(BaseModel):
|
||||
summary: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
|
||||
115
tests/test_additional_properties_bool.py
Normal file
115
tests/test_additional_properties_bool.py
Normal file
@@ -0,0 +1,115 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class FooBaseModel(BaseModel):
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
|
||||
class Foo(FooBaseModel):
|
||||
pass
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.post("/")
|
||||
async def post(
|
||||
foo: Union[Foo, None] = None,
|
||||
):
|
||||
return foo
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_call_invalid():
|
||||
response = client.post("/", json={"foo": {"bar": "baz"}})
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_call_valid():
|
||||
response = client.post("/", json={})
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {}
|
||||
|
||||
|
||||
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": {
|
||||
"/": {
|
||||
"post": {
|
||||
"summary": "Post",
|
||||
"operationId": "post__post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/Foo"}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {"application/json": {"schema": {}}},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"Foo": {
|
||||
"properties": {},
|
||||
"additionalProperties": False,
|
||||
"type": "object",
|
||||
"title": "Foo",
|
||||
},
|
||||
"HTTPValidationError": {
|
||||
"properties": {
|
||||
"detail": {
|
||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||
"type": "array",
|
||||
"title": "Detail",
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"title": "HTTPValidationError",
|
||||
},
|
||||
"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",
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -46,26 +46,15 @@ def test_openapi_schema():
|
||||
"title": "Item",
|
||||
"examples": [
|
||||
{
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
{"name": "Bar", "price": "35.4"},
|
||||
{
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {"name": "Bar", "price": "35.4"},
|
||||
},
|
||||
{
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
@@ -46,26 +46,15 @@ def test_openapi_schema():
|
||||
"title": "Item",
|
||||
"examples": [
|
||||
{
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
{"name": "Bar", "price": "35.4"},
|
||||
{
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {"name": "Bar", "price": "35.4"},
|
||||
},
|
||||
{
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
@@ -55,26 +55,15 @@ def test_openapi_schema(client: TestClient):
|
||||
"title": "Item",
|
||||
"examples": [
|
||||
{
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
{"name": "Bar", "price": "35.4"},
|
||||
{
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {"name": "Bar", "price": "35.4"},
|
||||
},
|
||||
{
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
@@ -55,26 +55,15 @@ def test_openapi_schema(client: TestClient):
|
||||
"title": "Item",
|
||||
"examples": [
|
||||
{
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
{"name": "Bar", "price": "35.4"},
|
||||
{
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {"name": "Bar", "price": "35.4"},
|
||||
},
|
||||
{
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
@@ -55,26 +55,15 @@ def test_openapi_schema(client: TestClient):
|
||||
"title": "Item",
|
||||
"examples": [
|
||||
{
|
||||
"summary": "A normal example",
|
||||
"description": "A **normal** item works correctly.",
|
||||
"value": {
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
"price": 35.4,
|
||||
"tax": 3.2,
|
||||
},
|
||||
{"name": "Bar", "price": "35.4"},
|
||||
{
|
||||
"summary": "An example with converted data",
|
||||
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
|
||||
"value": {"name": "Bar", "price": "35.4"},
|
||||
},
|
||||
{
|
||||
"summary": "Invalid data is rejected with an error",
|
||||
"value": {
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
"name": "Baz",
|
||||
"price": "thirty five point four",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user