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

allOf ignores the $ref option if it contains enum

Open lexihill1 opened this issue 1 year ago • 3 comments

When using allOf that one of the options contains $ref with enum, the pydantic model is generated without the ref

Example schema:


 components:
  schemas:
    AdminUser:
      type: object
      properties:
        userType:
          type: string
          enum:
            - admin
        username:
          type: string
        details:
          type: object
          required:
            - phoneNumber
          properties:
            phoneNumber:
              type: string
              minLength: 9
              maxLength: 15
              pattern: '^\+[0-9]{9,15}$'
      required:
        - userType
        - username
        - details

    RegularUser:
      type: object
      properties:
        userType:
          type: string
          enum:
            - regular
        username:
          type: string
        details:
          type: object
          required:
            - address
          properties:
            address:
              type: string
      required:
        - userType
        - username
        - details
     
    User:
        oneOf:
            - $ref: '#/components/schemas/AdminUser'
            - $ref: '#/components/schemas/RegularUser'
         discriminator:
              propertyName: userType
   
paths:
  /user:
      post:
          summary: Add user
          requestBody:
            content:
              application/json:
                schema:
                  allOf:
                    - type: object
                      properties:
                        userId:
                          type: string
                      required:
                        - userId
                  - $ref: '#/components/schemas/User'
      responses:
        '200':
          description: OK




Output:

class UserPostRequest(BaseModel):
    userId: str

Version:

  • Python version: 3.10.12
  • datamodel-code-generator version: 0.25.1

lexihill1 avatar Dec 07 '23 16:12 lexihill1

@denisart @koxudaxi

lexihill1 avatar Dec 12 '23 09:12 lexihill1

I'm sorry for my response. I understand your issue. But, your schema's allOf has discriminator . So, It's a little complicated

Do you expect the code?


class AdminUser(BaseModel):
    userType: Literal['AdminUser']
    username: str
    details: Details

class AdminUser1(AdminUser):
    userId: str

class RegularUser(BaseModel):
    userType: Literal['RegularUser']
    username: str
    details: Details1

class RegularUser1(RegularUser):
    userId: str
    
class User(BaseModel):
    __root__: Union[AdminUser, RegularUser] = Field(..., discriminator='userType')


class UserPostRequest(BaseModel):
    __root__: Union[AdminUser1, RegularUser1] = Field(..., discriminator='userType')


koxudaxi avatar Dec 22 '23 15:12 koxudaxi

Yes that the code im expecting but not the one im currently getting. This is the output im seeing:

class UserPostRequest(BaseModel):
    userId: str

@koxudaxi

lexihill1 avatar Dec 31 '23 12:12 lexihill1