mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-31 18:20:45 -05:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4976568fc7 | ||
|
|
fb30cc2f50 | ||
|
|
f95a174288 | ||
|
|
5126e099bd | ||
|
|
dcf0299195 |
@@ -443,6 +443,14 @@ The docs UI will interact with the server that you select.
|
||||
|
||||
///
|
||||
|
||||
/// note | Technical Details
|
||||
|
||||
The `servers` property in the OpenAPI specification is optional.
|
||||
|
||||
If you don't specify the `servers` parameter and `root_path` is equal to `/`, the `servers` property in the generated OpenAPI schema will be omitted entirely by default, which is the equivalent of a single server with a `url` value of `/`.
|
||||
|
||||
///
|
||||
|
||||
### Disable automatic server from `root_path` { #disable-automatic-server-from-root-path }
|
||||
|
||||
If you don't want **FastAPI** to include an automatic server using the `root_path`, you can use the parameter `root_path_in_servers=False`:
|
||||
|
||||
@@ -7,6 +7,16 @@ hide:
|
||||
|
||||
## Latest Changes
|
||||
|
||||
## 0.123.4
|
||||
|
||||
### Fixes
|
||||
|
||||
* 🐛 Fix OpenAPI schema support for computed fields when using `separate_input_output_schemas=False`. PR [#13207](https://github.com/fastapi/fastapi/pull/13207) by [@vgrafe](https://github.com/vgrafe).
|
||||
|
||||
### Docs
|
||||
|
||||
* 📝 Fix docstring of `servers` parameter. PR [#14405](https://github.com/fastapi/fastapi/pull/14405) by [@YuriiMotov](https://github.com/YuriiMotov).
|
||||
|
||||
## 0.123.3
|
||||
|
||||
### Fixes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""FastAPI framework, high performance, easy to learn, fast to code, ready for production"""
|
||||
|
||||
__version__ = "0.123.3"
|
||||
__version__ = "0.123.4"
|
||||
|
||||
from starlette import status as status
|
||||
|
||||
|
||||
@@ -180,8 +180,13 @@ def get_schema_from_model_field(
|
||||
],
|
||||
separate_input_output_schemas: bool = True,
|
||||
) -> Dict[str, Any]:
|
||||
computed_fields = field._type_adapter.core_schema.get("schema", {}).get(
|
||||
"computed_fields", []
|
||||
)
|
||||
override_mode: Union[Literal["validation"], None] = (
|
||||
None if separate_input_output_schemas else "validation"
|
||||
None
|
||||
if (separate_input_output_schemas or len(computed_fields) > 0)
|
||||
else "validation"
|
||||
)
|
||||
# This expects that GenerateJsonSchema was already used to generate the definitions
|
||||
json_schema = field_mapping[(field, override_mode or field.mode)]
|
||||
@@ -203,9 +208,14 @@ def get_definitions(
|
||||
Dict[Tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue],
|
||||
Dict[str, Dict[str, Any]],
|
||||
]:
|
||||
has_computed_fields: bool = any(
|
||||
field._type_adapter.core_schema.get("schema", {}).get("computed_fields", [])
|
||||
for field in fields
|
||||
)
|
||||
|
||||
schema_generator = GenerateJsonSchema(ref_template=REF_TEMPLATE)
|
||||
override_mode: Union[Literal["validation"], None] = (
|
||||
None if separate_input_output_schemas else "validation"
|
||||
None if (separate_input_output_schemas or has_computed_fields) else "validation"
|
||||
)
|
||||
validation_fields = [field for field in fields if field.mode == "validation"]
|
||||
serialization_fields = [field for field in fields if field.mode == "serialization"]
|
||||
|
||||
@@ -301,7 +301,12 @@ class FastAPI(Starlette):
|
||||
browser tabs open). Or if you want to leave fixed the possible URLs.
|
||||
|
||||
If the servers `list` is not provided, or is an empty `list`, the
|
||||
default value would be a `dict` with a `url` value of `/`.
|
||||
`servers` property in the generated OpenAPI will be:
|
||||
|
||||
* a `dict` with a `url` value of the application's mounting point
|
||||
(`root_path`) if it's different from `/`.
|
||||
* otherwise, the `servers` property will be omitted from the OpenAPI
|
||||
schema.
|
||||
|
||||
Each item in the `list` is a `dict` containing:
|
||||
|
||||
|
||||
@@ -6,8 +6,9 @@ from .utils import needs_pydanticv2
|
||||
|
||||
|
||||
@pytest.fixture(name="client")
|
||||
def get_client():
|
||||
app = FastAPI()
|
||||
def get_client(request):
|
||||
separate_input_output_schemas = request.param
|
||||
app = FastAPI(separate_input_output_schemas=separate_input_output_schemas)
|
||||
|
||||
from pydantic import BaseModel, computed_field
|
||||
|
||||
@@ -32,6 +33,7 @@ def get_client():
|
||||
return client
|
||||
|
||||
|
||||
@pytest.mark.parametrize("client", [True, False], indirect=True)
|
||||
@pytest.mark.parametrize("path", ["/", "/responses"])
|
||||
@needs_pydanticv2
|
||||
def test_get(client: TestClient, path: str):
|
||||
@@ -40,6 +42,7 @@ def test_get(client: TestClient, path: str):
|
||||
assert response.json() == {"width": 3, "length": 4, "area": 12}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("client", [True, False], indirect=True)
|
||||
@needs_pydanticv2
|
||||
def test_openapi_schema(client: TestClient):
|
||||
response = client.get("/openapi.json")
|
||||
|
||||
Reference in New Issue
Block a user