Browse Source

add: option to generate api specification by field name instead of alias

pull/10621/head
Yasin Tatar 1 year ago
parent
commit
f507a77a43
  1. 1
      docs/en/docs/how-to/extending-openapi.md
  2. 5
      fastapi/openapi/utils.py

1
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
The parameter `summary` is available in OpenAPI 3.1.0 and above, supported by FastAPI 0.99.0 and above.

5
fastapi/openapi/utils.py

@ -450,6 +450,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:
@ -471,7 +472,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,

Loading…
Cancel
Save