question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reserved name populated by snake-case-field

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
yuyupopocommented, Mar 28, 2021

No worries 😃 Thx for the fix

1reaction
koxudaxicommented, Mar 27, 2021

@yuyupopo I’m sorry for the late release. I pushed the new version as 0.10.0

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found