Skip to content

bug: minProperties constraint is ignored in generated Pydantic models #49

Description

@damaz91

The minProperties constraint defined in JSON schemas is currently ignored during the SDK generation process.

Example

In description.json (see description.json), we have:

{
  "properties": {
    "plain": { "type": "string" },
    "html": { "type": "string" },
    "markdown": { "type": "string" }
  },
  "minProperties": 1
}

This is intended to ensure that at least one of plain, html, or markdown is provided.

However, the generated Pydantic model Description in ucp_sdk/models/schemas/shopping/types/description.py defines all these fields as optional and does not enforce that at least one is present:

class Description(BaseModel):
    model_config = ConfigDict(extra="allow")
    plain: str | None = None
    html: str | None = None
    markdown: str | None = None

Instantiating Description() with no fields passes validation, which violates the schema constraint.

Proposed Solution

We need to handle minProperties (and potentially maxProperties) in preprocess_schemas.py or via post-processing of the generated models.
One option is to translate minProperties into an anyOf structure that datamodel-code-generator can interpret, or post-process the generated files to inject a Pydantic model_validator (mode='after') to enforce the property count.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions