gudwls215 3 days ago
committed by GitHub
parent
commit
1c71531f73
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      fastapi/_compat.py
  2. 26
      fastapi/openapi/utils.py
  3. 5
      fastapi/routing.py

2
fastapi/_compat.py

@ -200,7 +200,7 @@ if PYDANTIC_V2:
# This expects that GenerateJsonSchema was already used to generate the definitions # This expects that GenerateJsonSchema was already used to generate the definitions
json_schema = field_mapping[(field, override_mode or field.mode)] json_schema = field_mapping[(field, override_mode or field.mode)]
if "$ref" not in json_schema: if "$ref" not in json_schema:
# TODO remove when deprecating Pydantic v1 # Set title for field when not using reference
# Ref: https://github.com/pydantic/pydantic/blob/d61792cc42c80b13b23e3ffa74bc37ec7c77f7d1/pydantic/schema.py#L207 # Ref: https://github.com/pydantic/pydantic/blob/d61792cc42c80b13b23e3ffa74bc37ec7c77f7d1/pydantic/schema.py#L207
json_schema["title"] = ( json_schema["title"] = (
field.field_info.title or field.alias.title().replace("_", " ") field.field_info.title or field.alias.title().replace("_", " ")

26
fastapi/openapi/utils.py

@ -338,16 +338,22 @@ def get_openapi_path(
if route.status_code is not None: if route.status_code is not None:
status_code = str(route.status_code) status_code = str(route.status_code)
else: else:
# It would probably make more sense for all response classes to have an # Use cached attribute if available, otherwise fall back to inspection
# explicit default status_code, and to extract it from them, instead of # This improves performance by avoiding repeated signature inspection
# doing this inspection tricks, that would probably be in the future if hasattr(current_response_class, "default_status_code"):
# TODO: probably make status_code a default class attribute for all status_code = str(current_response_class.default_status_code)
# responses in Starlette else:
response_signature = inspect.signature(current_response_class.__init__) response_signature = inspect.signature(
status_code_param = response_signature.parameters.get("status_code") current_response_class.__init__
if status_code_param is not None: )
if isinstance(status_code_param.default, int): status_code_param = response_signature.parameters.get("status_code")
status_code = str(status_code_param.default) if status_code_param is not None:
if isinstance(status_code_param.default, int):
status_code = str(status_code_param.default)
else:
status_code = "200"
else:
status_code = "200"
operation.setdefault("responses", {}).setdefault(status_code, {})[ operation.setdefault("responses", {}).setdefault(status_code, {})[
"description" "description"
] = route.response_description ] = route.response_description

5
fastapi/routing.py

@ -365,10 +365,6 @@ def get_websocket_app(
) -> Callable[[WebSocket], Coroutine[Any, Any, Any]]: ) -> Callable[[WebSocket], Coroutine[Any, Any, Any]]:
async def app(websocket: WebSocket) -> None: async def app(websocket: WebSocket) -> None:
async with AsyncExitStack() as async_exit_stack: async with AsyncExitStack() as async_exit_stack:
# TODO: remove this scope later, after a few releases
# This scope fastapi_astack is no longer used by FastAPI, kept for
# compatibility, just in case
websocket.scope["fastapi_astack"] = async_exit_stack
solved_result = await solve_dependencies( solved_result = await solve_dependencies(
request=websocket, request=websocket,
dependant=dependant, dependant=dependant,
@ -521,7 +517,6 @@ class APIRoute(routing.Route):
# would pass the validation and be returned as is. # would pass the validation and be returned as is.
# By being a new field, no inheritance will be passed as is. A new model # By being a new field, no inheritance will be passed as is. A new model
# will always be created. # will always be created.
# TODO: remove when deprecating Pydantic v1
self.secure_cloned_response_field: Optional[ModelField] = ( self.secure_cloned_response_field: Optional[ModelField] = (
create_cloned_field(self.response_field) create_cloned_field(self.response_field)
) )

Loading…
Cancel
Save