Created by: RoryDungan
I noticed that the Python-FastAPI generator converts all model field names to snake_case to match Python convention. While this is desireable from a code-style perspective, it breaks any API using a different convention for field names since it also affects the names of fields that get serialised to JSON.
FastAPI uses Pydantic for its model classes, which supports setting an alias value in order to deserialise a different name to what is used for the Python class, so I modified the generator to use this. Be default it will always be used during deserialisation but only during serialisation if the by_alias=True
option is specified. Luckily, FastAPI includes an option we can set on endpoints that will use this for data returned by the endpoint, response_model_by_alias=True
.
I've enabled this everywhere because there's no harm in using it if the alias is the same as the name and it seemed like adding logic to specifically detect camelCase field names would overcomplicate things, plus this could potentially be useful if you have an OpenAPI spec that includes a field name that is a reserved word in Python, although I haven't tested it in that case.
Here's an example of some code before and after the change:
# before:
class RenderClip(BaseModel):
"""NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
Do not edit the class manually.
RenderClip - a model defined in OpenAPI
focused_player_actor_id: The focused_player_actor_id of this RenderClip.
time_range: The time_range of this RenderClip.
ingredients: The ingredients of this RenderClip.
output_length: The output_length of this RenderClip [Optional].
"""
focused_player_actor_id: str
time_range: GameTimeRange
ingredients: List[Ingredient]
output_length: Optional[float] = None
# after:
class RenderClip(BaseModel):
"""NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
Do not edit the class manually.
RenderClip - a model defined in OpenAPI
focused_player_actor_id: The focused_player_actor_id of this RenderClip.
time_range: The time_range of this RenderClip.
ingredients: The ingredients of this RenderClip.
output_length: The output_length of this RenderClip [Optional].
"""
focused_player_actor_id: str = Field(alias="focusedPlayerActorId")
time_range: GameTimeRange = Field(alias="timeRange")
ingredients: List[Ingredient] = Field(alias="ingredients")
output_length: Optional[float] = Field(alias="outputLength", default=None)
This may also fix #11604 (closed) since it allows serializing/deserializing models with names in camelCase without requiring any additional options in the generator.
I've made this change on top of 5.4.x because that's what our team is using so if this could be included in a patch release of 5.4.x that would be ideal, although I have also tested on master
and it works so if you think it would be more appropriate to commit there I'm happy to rebase.
PR checklist
- [✓] Read the contribution guidelines.
- [✓] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- [✓] Run the following to build the project and update samples:
./mvnw clean package ./bin/generate-samples.sh ./bin/utils/export_docs_generators.sh
./bin/generate-samples.sh bin/configs/java*
. For Windows users, please run the script in Git BASH. - [✓] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request. This change applies to the Python-FastAPI server generator @taxpon @frol @mbohlool @cbornet @kenjones-cisco @tomplus @arun-nalla @spacether