datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

No relation between classes generated from nested objects

Open gk12277 opened this issue 3 years ago • 2 comments

Describe the bug

Not sure if its a bug or intended case. I have a nested objects to which i have given a reference. When the classes are generated shouldn't the outermost class be related to the inner class in some way

To Reproduce

Example schema:


type: object
properties:
  User:
    type: object
    properties:
      name: 
        type: string    
      userProp:
        $ref:  "#/definitions/components/UserProperties"
      homeProp:
        $ref:  "#/definitions/components/homeProperties"
definitions:
  components:
    UserProperties:
      type: object
      properties:
        experience:
          type: string
    homeProperties:
      type: object
      properties:
        type:
          type: string

Used commandline:

$ datamodel-codegen --input file_name --output output_file_name

Expected behavior

Generated file

class Components(BaseModel):
    __root__: Any


class UserProperties(BaseModel):
    experience: Optional[str] = None


class HomeProperties(BaseModel):
    type: Optional[str] = None


class User(BaseModel):
    name: Optional[str] = None
    userProp: Optional[UserProperties] = None
    homeProp: Optional[HomeProperties] = None


class Model(BaseModel):
    User: Optional[User] = None

Here shouldn't UserProperties and HomeProperties be related to Components class in some way according to the schema.

Should it be more like

class Components(BaseModel):
    __root__: Any


class UserProperties(Components):
    experience: Optional[str] = None


class HomeProperties(Components):
    type: Optional[str] = None

Version:

  • OS: Mac
  • Python version: 3.9
  • datamodel-code-generator version: 0.11.6

gk12277 avatar Aug 03 '21 04:08 gk12277

@gk12277 Thank you for creating this issue. Yes, I expected the behavior.

We don't need Components in the generated file. I can change to ignore Components. But, UserProperties and HomeProperties should be removed? I guess no problem that users can use the classes.

koxudaxi avatar Aug 09 '21 17:08 koxudaxi

@koxudaxi Sorry for late reply Yes I think users can use UserProperties and HomeProperties

gk12277 avatar Sep 16 '21 09:09 gk12277