Fix issue with fastapi generator converting all fields to snake_case #12261
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
This may also fix #11604 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
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
This change applies to the Python-FastAPI server generator @taxpon @frol @mbohlool @cbornet @kenjones-cisco @tomplus @arun-nalla @spacether