forked from tiangolo/fastapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fix JSON Schema accepting bools as valid JSON Schemas, e.g. `additi…
…onalProperties: false` (tiangolo#9781) * 🐛 Fix JSON Schema accepting bools as valid JSON Schemas, e.g. additionalProperties: false * ✅ Add test to ensure additionalProperties can be false * ♻️ Tweak OpenAPI models to support Pydantic v1's JSON Schema for tuples
- Loading branch information
Showing
2 changed files
with
142 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
}, | ||
} | ||
}, | ||
} |