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

mixing additionalProperties and required properties doesn't work with pydantic

Open Panaetius opened this issue 1 year ago • 1 comments

with version 0.25.1 having a schema like

    RCloneConfig:
      type: object
      description: Dictionary of rclone key:value pairs (based on schema from '/storage_schema')
      properties:
        type:
          type: string
      required:
        - type
      additionalProperties:
        oneOf:
        - type: number
        - type: string
          nullable: true
        - type: boolean
        - type: object

gives python code like

class RCloneConfig(BaseAPISpec):
    type: str

which causes pydantic to ignore anything except type.

without the required property no class is generated and parent classes refer directly to Optional[Dict[str, Union[float, Optional[str], bool, Dict[str, Any]]]]

I'm not even sure this can be properly modeled in pydantic, with both validating the required property and checking that the arbitrary types conform to the correct value types (Extra.allow wouldn't check the value types). But thought I'd report it anyways just in case.

Panaetius avatar Dec 01 '23 17:12 Panaetius

@Panaetius I'm sorry for my late reply.

This is an issue that should be addressed. Just give me a few days as the implementation is a bit complicated.

koxudaxi avatar Dec 22 '23 14:12 koxudaxi