Datamodel-code-generator: Generated intermediate models may shadow imports

Created on 15 Feb 2021  路  6Comments  路  Source: koxudaxi/datamodel-code-generator

Hey, great library, thank you!

Describe the bug

See Optional

To Reproduce

Example schema:

components:
  schemas:
    Config:
      properties:
        optional:
          type: object
          additionalProperties:
            type: array
            items:
              type: string

Used commandline:

$ datamodel-codegen --input-file-type openapi --strip-default-none --input test.yaml --output model.py

Produced:

from __future__ import annotations

from typing import Dict, List, Optional

from pydantic import BaseModel


class Optional(BaseModel):
    __root__: List[str]


class Config(BaseModel):
    optional: Optional[Dict[str, Optional]]

Version:

  • OS: Windows 10
  • Python version: 3.8
  • datamodel-code-generator version: 0.7.2
bug released

Most helpful comment

Awesome, thank you!

All 6 comments

@ofek
Thank you for creating this issue.
Oh, I don't expect the case.
We have two options.
a. Use an alias name to import duplicated class name like Optional.
b. Change the name of the model

a. affects only this file(python module)
b. impacts all modular files.

I feel b. is good.

What do you think about it?

b. sounds fine!

@ofek
Would you need to fix the problem early?
We must refactor change the relationship between a model and referenced model.

We may need another plan. 馃

@koxudaxi Take your time! Do the correct approach

@ofek
I have released a new version 0.9.0
This version change duplicated name with Model as suffix.

# generated by datamodel-codegen:
#   filename:  a.yaml
#   timestamp: 2021-03-04T16:42:27+00:00

from __future__ import annotations

from typing import Dict, List, Optional

from pydantic import BaseModel


class OptionalModel(BaseModel):
    __root__: List[str]


class Config(BaseModel):
    optional: Optional[Dict[str, OptionalModel]] = None

Awesome, thank you!

Was this page helpful?
0 / 5 - 0 ratings