diff --git a/docs/en/docs/how-to/extending-openapi.md b/docs/en/docs/how-to/extending-openapi.md index 26c742c20..feecaeb2f 100644 --- a/docs/en/docs/how-to/extending-openapi.md +++ b/docs/en/docs/how-to/extending-openapi.md @@ -26,6 +26,7 @@ And that function `get_openapi()` receives as parameters: * `summary`: A short summary of the API. * `description`: The description of your API, this can include markdown and will be shown in the docs. * `routes`: A list of routes, these are each of the registered *path operations*. They are taken from `app.routes`. +* `field_names_by_alias`: A boolean indicating whether to use the field names or the alias names for the models. /// info diff --git a/fastapi/_compat.py b/fastapi/_compat.py index c07e4a3b0..384bf1614 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -363,6 +363,7 @@ else: @dataclass class GenerateJsonSchema: # type: ignore[no-redef] ref_template: str + by_alias: bool = True class PydanticSchemaGenerationError(Exception): # type: ignore[no-redef] pass diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 808646cc2..f6a94227e 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -489,6 +489,7 @@ def get_openapi( contact: Optional[Dict[str, Union[str, Any]]] = None, license_info: Optional[Dict[str, Union[str, Any]]] = None, separate_input_output_schemas: bool = True, + field_names_by_alias: bool = True, ) -> Dict[str, Any]: info: Dict[str, Any] = {"title": title, "version": version} if summary: @@ -510,7 +511,9 @@ def get_openapi( operation_ids: Set[str] = set() all_fields = get_fields_from_routes(list(routes or []) + list(webhooks or [])) model_name_map = get_compat_model_name_map(all_fields) - schema_generator = GenerateJsonSchema(ref_template=REF_TEMPLATE) + schema_generator = GenerateJsonSchema( + ref_template=REF_TEMPLATE, by_alias=field_names_by_alias + ) field_mapping, definitions = get_definitions( fields=all_fields, schema_generator=schema_generator,