|
|
@ -249,7 +249,264 @@ def no_response_model_annotation_json_response_class() -> JSONResponse: |
|
|
|
return JSONResponse(content={"foo": "bar"}) |
|
|
|
|
|
|
|
|
|
|
|
openapi_schema = { |
|
|
|
client = TestClient(app) |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_no_annotation_return_model(): |
|
|
|
response = client.get("/no_response_model-no_annotation-return_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_no_annotation_return_dict(): |
|
|
|
response = client.get("/no_response_model-no_annotation-return_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_same_model(): |
|
|
|
response = client.get("/response_model-no_annotation-return_same_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_exact_dict(): |
|
|
|
response = client.get("/response_model-no_annotation-return_exact_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_invalid_dict(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/response_model-no_annotation-return_invalid_dict") |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_invalid_model(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/response_model-no_annotation-return_invalid_model") |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_dict_with_extra_data(): |
|
|
|
response = client.get("/response_model-no_annotation-return_dict_with_extra_data") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_submodel_with_extra_data(): |
|
|
|
response = client.get( |
|
|
|
"/response_model-no_annotation-return_submodel_with_extra_data" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_same_model(): |
|
|
|
response = client.get("/no_response_model-annotation-return_same_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_exact_dict(): |
|
|
|
response = client.get("/no_response_model-annotation-return_exact_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_invalid_dict(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/no_response_model-annotation-return_invalid_dict") |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_invalid_model(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/no_response_model-annotation-return_invalid_model") |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_dict_with_extra_data(): |
|
|
|
response = client.get("/no_response_model-annotation-return_dict_with_extra_data") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_submodel_with_extra_data(): |
|
|
|
response = client.get( |
|
|
|
"/no_response_model-annotation-return_submodel_with_extra_data" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_same_model(): |
|
|
|
response = client.get("/response_model_none-annotation-return_same_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_exact_dict(): |
|
|
|
response = client.get("/response_model_none-annotation-return_exact_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_invalid_dict(): |
|
|
|
response = client.get("/response_model_none-annotation-return_invalid_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_invalid_model(): |
|
|
|
response = client.get("/response_model_none-annotation-return_invalid_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "Foo", "price": 42.0} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_dict_with_extra_data(): |
|
|
|
response = client.get("/response_model_none-annotation-return_dict_with_extra_data") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == { |
|
|
|
"name": "John", |
|
|
|
"surname": "Doe", |
|
|
|
"password_hash": "secret", |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_submodel_with_extra_data(): |
|
|
|
response = client.get( |
|
|
|
"/response_model_none-annotation-return_submodel_with_extra_data" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == { |
|
|
|
"name": "John", |
|
|
|
"surname": "Doe", |
|
|
|
"password_hash": "secret", |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_same_model(): |
|
|
|
response = client.get("/response_model_model1-annotation_model2-return_same_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_exact_dict(): |
|
|
|
response = client.get("/response_model_model1-annotation_model2-return_exact_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_invalid_dict(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/response_model_model1-annotation_model2-return_invalid_dict") |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_invalid_model(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/response_model_model1-annotation_model2-return_invalid_model") |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_dict_with_extra_data(): |
|
|
|
response = client.get( |
|
|
|
"/response_model_model1-annotation_model2-return_dict_with_extra_data" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_submodel_with_extra_data(): |
|
|
|
response = client.get( |
|
|
|
"/response_model_model1-annotation_model2-return_submodel_with_extra_data" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_filtering_model_annotation_submodel_return_submodel(): |
|
|
|
response = client.get( |
|
|
|
"/response_model_filtering_model-annotation_submodel-return_submodel" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_list_of_model_no_annotation(): |
|
|
|
response = client.get("/response_model_list_of_model-no_annotation") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == [ |
|
|
|
{"name": "John", "surname": "Doe"}, |
|
|
|
{"name": "Jane", "surname": "Does"}, |
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_list_of_model(): |
|
|
|
response = client.get("/no_response_model-annotation_list_of_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == [ |
|
|
|
{"name": "John", "surname": "Doe"}, |
|
|
|
{"name": "Jane", "surname": "Does"}, |
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_forward_ref_list_of_model(): |
|
|
|
response = client.get("/no_response_model-annotation_forward_ref_list_of_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == [ |
|
|
|
{"name": "John", "surname": "Doe"}, |
|
|
|
{"name": "Jane", "surname": "Does"}, |
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_union_no_annotation_return_model1(): |
|
|
|
response = client.get("/response_model_union-no_annotation-return_model1") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_union_no_annotation_return_model2(): |
|
|
|
response = client.get("/response_model_union-no_annotation-return_model2") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "Foo", "price": 42.0} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_union_return_model1(): |
|
|
|
response = client.get("/no_response_model-annotation_union-return_model1") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_union_return_model2(): |
|
|
|
response = client.get("/no_response_model-annotation_union-return_model2") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "Foo", "price": 42.0} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_class(): |
|
|
|
response = client.get("/no_response_model-annotation_response_class") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.text == "Foo" |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_json_response_class(): |
|
|
|
response = client.get("/no_response_model-annotation_json_response_class") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"foo": "bar"} |
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_response_model_field(): |
|
|
|
app = FastAPI() |
|
|
|
with pytest.raises(FastAPIError) as e: |
|
|
|
|
|
|
|
@app.get("/") |
|
|
|
def read_root() -> Union[Response, None]: |
|
|
|
return Response(content="Foo") # pragma: no cover |
|
|
|
|
|
|
|
assert "valid Pydantic field type" in e.value.args[0] |
|
|
|
assert "parameter response_model=None" in e.value.args[0] |
|
|
|
|
|
|
|
|
|
|
|
def test_openapi_schema(): |
|
|
|
response = client.get("/openapi.json") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == { |
|
|
|
"openapi": "3.0.2", |
|
|
|
"info": {"title": "FastAPI", "version": "0.1.0"}, |
|
|
|
"paths": { |
|
|
@ -723,389 +980,129 @@ openapi_schema = { |
|
|
|
"content": { |
|
|
|
"application/json": { |
|
|
|
"schema": { |
|
|
|
"title": "Response Response Model Union No Annotation Return Model1 Response Model Union No Annotation Return Model1 Get", |
|
|
|
"anyOf": [ |
|
|
|
{"$ref": "#/components/schemas/User"}, |
|
|
|
{"$ref": "#/components/schemas/Item"}, |
|
|
|
], |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
"/response_model_union-no_annotation-return_model2": { |
|
|
|
"get": { |
|
|
|
"summary": "Response Model Union No Annotation Return Model2", |
|
|
|
"operationId": "response_model_union_no_annotation_return_model2_response_model_union_no_annotation_return_model2_get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": { |
|
|
|
"application/json": { |
|
|
|
"schema": { |
|
|
|
"title": "Response Response Model Union No Annotation Return Model2 Response Model Union No Annotation Return Model2 Get", |
|
|
|
"anyOf": [ |
|
|
|
{"$ref": "#/components/schemas/User"}, |
|
|
|
{"$ref": "#/components/schemas/Item"}, |
|
|
|
], |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
"/no_response_model-annotation_union-return_model1": { |
|
|
|
"get": { |
|
|
|
"summary": "No Response Model Annotation Union Return Model1", |
|
|
|
"operationId": "no_response_model_annotation_union_return_model1_no_response_model_annotation_union_return_model1_get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": { |
|
|
|
"application/json": { |
|
|
|
"schema": { |
|
|
|
"title": "Response No Response Model Annotation Union Return Model1 No Response Model Annotation Union Return Model1 Get", |
|
|
|
"anyOf": [ |
|
|
|
{"$ref": "#/components/schemas/User"}, |
|
|
|
{"$ref": "#/components/schemas/Item"}, |
|
|
|
], |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
"/no_response_model-annotation_union-return_model2": { |
|
|
|
"get": { |
|
|
|
"summary": "No Response Model Annotation Union Return Model2", |
|
|
|
"operationId": "no_response_model_annotation_union_return_model2_no_response_model_annotation_union_return_model2_get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": { |
|
|
|
"application/json": { |
|
|
|
"schema": { |
|
|
|
"title": "Response No Response Model Annotation Union Return Model2 No Response Model Annotation Union Return Model2 Get", |
|
|
|
"anyOf": [ |
|
|
|
{"$ref": "#/components/schemas/User"}, |
|
|
|
{"$ref": "#/components/schemas/Item"}, |
|
|
|
], |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
"/no_response_model-annotation_response_class": { |
|
|
|
"get": { |
|
|
|
"summary": "No Response Model Annotation Response Class", |
|
|
|
"operationId": "no_response_model_annotation_response_class_no_response_model_annotation_response_class_get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": {"application/json": {"schema": {}}}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
"/no_response_model-annotation_json_response_class": { |
|
|
|
"get": { |
|
|
|
"summary": "No Response Model Annotation Json Response Class", |
|
|
|
"operationId": "no_response_model_annotation_json_response_class_no_response_model_annotation_json_response_class_get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": {"application/json": {"schema": {}}}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
"components": { |
|
|
|
"schemas": { |
|
|
|
"Item": { |
|
|
|
"title": "Item", |
|
|
|
"required": ["name", "price"], |
|
|
|
"type": "object", |
|
|
|
"properties": { |
|
|
|
"name": {"title": "Name", "type": "string"}, |
|
|
|
"price": {"title": "Price", "type": "number"}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
"User": { |
|
|
|
"title": "User", |
|
|
|
"required": ["name", "surname"], |
|
|
|
"type": "object", |
|
|
|
"properties": { |
|
|
|
"name": {"title": "Name", "type": "string"}, |
|
|
|
"surname": {"title": "Surname", "type": "string"}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
client = TestClient(app) |
|
|
|
|
|
|
|
|
|
|
|
def test_openapi_schema(): |
|
|
|
response = client.get("/openapi.json") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == openapi_schema |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_no_annotation_return_model(): |
|
|
|
response = client.get("/no_response_model-no_annotation-return_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_no_annotation_return_dict(): |
|
|
|
response = client.get("/no_response_model-no_annotation-return_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_same_model(): |
|
|
|
response = client.get("/response_model-no_annotation-return_same_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_exact_dict(): |
|
|
|
response = client.get("/response_model-no_annotation-return_exact_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_invalid_dict(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/response_model-no_annotation-return_invalid_dict") |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_invalid_model(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/response_model-no_annotation-return_invalid_model") |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_dict_with_extra_data(): |
|
|
|
response = client.get("/response_model-no_annotation-return_dict_with_extra_data") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_no_annotation_return_submodel_with_extra_data(): |
|
|
|
response = client.get( |
|
|
|
"/response_model-no_annotation-return_submodel_with_extra_data" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_same_model(): |
|
|
|
response = client.get("/no_response_model-annotation-return_same_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_exact_dict(): |
|
|
|
response = client.get("/no_response_model-annotation-return_exact_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_invalid_dict(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/no_response_model-annotation-return_invalid_dict") |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_invalid_model(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/no_response_model-annotation-return_invalid_model") |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_dict_with_extra_data(): |
|
|
|
response = client.get("/no_response_model-annotation-return_dict_with_extra_data") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_submodel_with_extra_data(): |
|
|
|
response = client.get( |
|
|
|
"/no_response_model-annotation-return_submodel_with_extra_data" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_same_model(): |
|
|
|
response = client.get("/response_model_none-annotation-return_same_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_exact_dict(): |
|
|
|
response = client.get("/response_model_none-annotation-return_exact_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_invalid_dict(): |
|
|
|
response = client.get("/response_model_none-annotation-return_invalid_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_invalid_model(): |
|
|
|
response = client.get("/response_model_none-annotation-return_invalid_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "Foo", "price": 42.0} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_dict_with_extra_data(): |
|
|
|
response = client.get("/response_model_none-annotation-return_dict_with_extra_data") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == { |
|
|
|
"name": "John", |
|
|
|
"surname": "Doe", |
|
|
|
"password_hash": "secret", |
|
|
|
"title": "Response Response Model Union No Annotation Return Model1 Response Model Union No Annotation Return Model1 Get", |
|
|
|
"anyOf": [ |
|
|
|
{"$ref": "#/components/schemas/User"}, |
|
|
|
{"$ref": "#/components/schemas/Item"}, |
|
|
|
], |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_none_annotation_return_submodel_with_extra_data(): |
|
|
|
response = client.get( |
|
|
|
"/response_model_none-annotation-return_submodel_with_extra_data" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == { |
|
|
|
"name": "John", |
|
|
|
"surname": "Doe", |
|
|
|
"password_hash": "secret", |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_same_model(): |
|
|
|
response = client.get("/response_model_model1-annotation_model2-return_same_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_exact_dict(): |
|
|
|
response = client.get("/response_model_model1-annotation_model2-return_exact_dict") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_invalid_dict(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/response_model_model1-annotation_model2-return_invalid_dict") |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_invalid_model(): |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
client.get("/response_model_model1-annotation_model2-return_invalid_model") |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_dict_with_extra_data(): |
|
|
|
response = client.get( |
|
|
|
"/response_model_model1-annotation_model2-return_dict_with_extra_data" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_model1_annotation_model2_return_submodel_with_extra_data(): |
|
|
|
response = client.get( |
|
|
|
"/response_model_model1-annotation_model2-return_submodel_with_extra_data" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_filtering_model_annotation_submodel_return_submodel(): |
|
|
|
response = client.get( |
|
|
|
"/response_model_filtering_model-annotation_submodel-return_submodel" |
|
|
|
) |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_list_of_model_no_annotation(): |
|
|
|
response = client.get("/response_model_list_of_model-no_annotation") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == [ |
|
|
|
{"name": "John", "surname": "Doe"}, |
|
|
|
{"name": "Jane", "surname": "Does"}, |
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_list_of_model(): |
|
|
|
response = client.get("/no_response_model-annotation_list_of_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == [ |
|
|
|
{"name": "John", "surname": "Doe"}, |
|
|
|
{"name": "Jane", "surname": "Does"}, |
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_forward_ref_list_of_model(): |
|
|
|
response = client.get("/no_response_model-annotation_forward_ref_list_of_model") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == [ |
|
|
|
{"name": "John", "surname": "Doe"}, |
|
|
|
{"name": "Jane", "surname": "Does"}, |
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_union_no_annotation_return_model1(): |
|
|
|
response = client.get("/response_model_union-no_annotation-return_model1") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_union_no_annotation_return_model2(): |
|
|
|
response = client.get("/response_model_union-no_annotation-return_model2") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "Foo", "price": 42.0} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_union_return_model1(): |
|
|
|
response = client.get("/no_response_model-annotation_union-return_model1") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "John", "surname": "Doe"} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_union_return_model2(): |
|
|
|
response = client.get("/no_response_model-annotation_union-return_model2") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"name": "Foo", "price": 42.0} |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_return_class(): |
|
|
|
response = client.get("/no_response_model-annotation_response_class") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.text == "Foo" |
|
|
|
|
|
|
|
|
|
|
|
def test_no_response_model_annotation_json_response_class(): |
|
|
|
response = client.get("/no_response_model-annotation_json_response_class") |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"foo": "bar"} |
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_response_model_field(): |
|
|
|
app = FastAPI() |
|
|
|
with pytest.raises(FastAPIError) as e: |
|
|
|
|
|
|
|
@app.get("/") |
|
|
|
def read_root() -> Union[Response, None]: |
|
|
|
return Response(content="Foo") # pragma: no cover |
|
|
|
|
|
|
|
assert "valid Pydantic field type" in e.value.args[0] |
|
|
|
assert "parameter response_model=None" in e.value.args[0] |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
"/response_model_union-no_annotation-return_model2": { |
|
|
|
"get": { |
|
|
|
"summary": "Response Model Union No Annotation Return Model2", |
|
|
|
"operationId": "response_model_union_no_annotation_return_model2_response_model_union_no_annotation_return_model2_get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": { |
|
|
|
"application/json": { |
|
|
|
"schema": { |
|
|
|
"title": "Response Response Model Union No Annotation Return Model2 Response Model Union No Annotation Return Model2 Get", |
|
|
|
"anyOf": [ |
|
|
|
{"$ref": "#/components/schemas/User"}, |
|
|
|
{"$ref": "#/components/schemas/Item"}, |
|
|
|
], |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
"/no_response_model-annotation_union-return_model1": { |
|
|
|
"get": { |
|
|
|
"summary": "No Response Model Annotation Union Return Model1", |
|
|
|
"operationId": "no_response_model_annotation_union_return_model1_no_response_model_annotation_union_return_model1_get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": { |
|
|
|
"application/json": { |
|
|
|
"schema": { |
|
|
|
"title": "Response No Response Model Annotation Union Return Model1 No Response Model Annotation Union Return Model1 Get", |
|
|
|
"anyOf": [ |
|
|
|
{"$ref": "#/components/schemas/User"}, |
|
|
|
{"$ref": "#/components/schemas/Item"}, |
|
|
|
], |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
"/no_response_model-annotation_union-return_model2": { |
|
|
|
"get": { |
|
|
|
"summary": "No Response Model Annotation Union Return Model2", |
|
|
|
"operationId": "no_response_model_annotation_union_return_model2_no_response_model_annotation_union_return_model2_get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": { |
|
|
|
"application/json": { |
|
|
|
"schema": { |
|
|
|
"title": "Response No Response Model Annotation Union Return Model2 No Response Model Annotation Union Return Model2 Get", |
|
|
|
"anyOf": [ |
|
|
|
{"$ref": "#/components/schemas/User"}, |
|
|
|
{"$ref": "#/components/schemas/Item"}, |
|
|
|
], |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
"/no_response_model-annotation_response_class": { |
|
|
|
"get": { |
|
|
|
"summary": "No Response Model Annotation Response Class", |
|
|
|
"operationId": "no_response_model_annotation_response_class_no_response_model_annotation_response_class_get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": {"application/json": {"schema": {}}}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
"/no_response_model-annotation_json_response_class": { |
|
|
|
"get": { |
|
|
|
"summary": "No Response Model Annotation Json Response Class", |
|
|
|
"operationId": "no_response_model_annotation_json_response_class_no_response_model_annotation_json_response_class_get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": {"application/json": {"schema": {}}}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
"components": { |
|
|
|
"schemas": { |
|
|
|
"Item": { |
|
|
|
"title": "Item", |
|
|
|
"required": ["name", "price"], |
|
|
|
"type": "object", |
|
|
|
"properties": { |
|
|
|
"name": {"title": "Name", "type": "string"}, |
|
|
|
"price": {"title": "Price", "type": "number"}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
"User": { |
|
|
|
"title": "User", |
|
|
|
"required": ["name", "surname"], |
|
|
|
"type": "object", |
|
|
|
"properties": { |
|
|
|
"name": {"title": "Name", "type": "string"}, |
|
|
|
"surname": {"title": "Surname", "type": "string"}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|