Skip to content

crossplane project build generates broken Python schemas for OpenAPI fields named int/bool #63

Description

@negz

What happened?

When an XRD has a property literally named int or bool (e.g. modeling DRA's DeviceAttribute, whose wire fields are int/bool/string/version), crossplane project build emits Python models that reference undefined type aliases int_aliased/bool_aliased. These appear across many generated files — including the shared io/k8s/apimachinery/pkg/apis/meta/v1.py — with no definition, making the models unimportable:

PydanticUserError: `ObjectMeta` is not fully defined; you should define `int_aliased`, then call `ObjectMeta.model_rebuild()`.

Root cause: schema generation runs the pinned image docker.io/koxudaxi/datamodel-code-generator:0.31.2 (internal/schemas/generator/python.go). The CLI's fixAliasedTypesInFile post-processing (which rewrites int_aliasedint) isn't applied to all generated files, so the XRD-derived models and meta/v1.py keep the broken references.

The underlying codegen bug is fixed upstream in koxudaxi/datamodel-code-generator#2968 (fixes #2964), first released in 0.54.0, which sanitizes builtin-conflicting field names (trailing underscore + preserved alias). Verified with the pinned image and the CLI's exact flags:

  • 0.31.2 / 0.32.0 / 0.45.0: broken int_aliased (no definition)
  • 0.50.0 / 0.53.0: int: int | None = None (imports, but shadows the builtin)
  • >= 0.54.0: correct — int_: int | None = Field(None, alias='int') (wire name preserved, non-shadowing attribute)

Bumping the pinned image to >= 0.54.0 fixes it.

How can we reproduce it?

XRD property:

attributes:
  type: object
  additionalProperties:
    type: object
    properties:
      string: {type: string}
      bool: {type: boolean}
      int: {type: integer, format: int64}

crossplane project build, then inspect the generated models — int_aliased/bool_aliased appear without definitions; importing/validating any model that transitively uses ObjectMeta fails.

Minimal standalone repro with the pinned image:

docker run --rm -v "$PWD:/work" -w /work \
  docker.io/koxudaxi/datamodel-code-generator:0.31.2 \
  --input attr.yaml --input-file-type openapi --disable-timestamp \
  --output-model-type pydantic_v2.BaseModel --target-python-version 3.12 \
  --use-field-description --enum-field-as-literal all --use-one-literal-as-default \
  --output out.py
# out.py: bool: Optional[bool_aliased] = None / int: Optional[int_aliased] = None

What environment did it happen in?

  • Crossplane CLI version: v0.0.0-1779477155-5208019 (negz/cli@5208019, the diy branch / crossplane/cli#24)
  • Platform (e.g., linux/amd64): linux/arm64
  • Crossplane version (if applicable): n/a (schema generation)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions