Reserved name populated by snake-case-field
See original GitHub issueDescribe the bug
When a property name is Global
, and --snake-case-field
option is given, the generator creates a pydantic model named global
which fails, because global
is a reserved keyword.
To Reproduce
Below is a command to generator models from docker engine api.
The docker engine api has a schema(Mode
) that has Global
as property name.
Used commandline:
$ datamodel-codegen \
--url "https://converter.swagger.io/api/convert?url=https://docs.docker.com/engine/api/v1.30.yaml" \
--output ex.py \
--use-standard-collections \
--use-generic-container-types \
--reuse-model \
--enable-faux-immutability \
--strict-nullable \
--use-schema-description \
--snake-case-field \
--allow-population-by-field-name \
--target-python-version 3.7 \
--enum-field-as-literal all
Expected behavior
I expect that code generator would select another name rather than global
. such as global_
.
Solution Idea
We can add an underscore if the lowercased string is reserved.
parser.base.py:camel_to_snake
@lru_cache()
def camel_to_snake(string: str) -> str:
subbed = _UNDER_SCORE_1.sub(r'\1_\2', string)
subbed = _UNDER_SCORE_2.sub(r'\1_\2', subbed).lower() # +
if keyword.iskeyword(subbed): # +
subbed = subbed + "_" # +
return subbed # +
I’ve tested this out.
Version:
- OS: macOS 10.15.7
- Python version: 3.7.10
- datamodel-code-generator version: master branch (commit d5bc50cb9d934ae5d40e8a5dd003224bdc7527de)
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Application in Ember doesn't recognize the model so it ends ...
Encountered a resource object with type "series", but no model was found for model name "series" (resolved model name using 'my-app@serializer:-json-api:.
Read more >Reserved schema names and reserved words - IBM
In some cases, names are reserved, and cannot be used by application programs. In other cases, certain names are not recommended for use...
Read more >Service-Oriented Computing - Springer Link
All rights are reserved by the Publisher, whether the whole or part of the ... snake case Field Names. Of the non-camelCase field...
Read more >Mimalloc: Compact General Purpose Allocator with Great ...
This will name the shared library as libmimalloc-debug.so . ... Just like large OS pages, use with care as reserving contiguous physical memory...
Read more >datamodel-code-generator Changelog - pyup.io
Replace exponential in name for FieldnameResolver by PTank in ... Fix --snake-case-field breaks class_name by koxudaxi in ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No worries 😃 Thx for the fix
@yuyupopo I’m sorry for the late release. I pushed the new version as
0.10.0