From b1674d27645763b31e2db3ae1d49d445bd0ed9d3 Mon Sep 17 00:00:00 2001 From: Nic Cope Date: Tue, 2 Jun 2026 22:18:50 -0700 Subject: [PATCH] Fix Python schema generation for fields named int or bool crossplane project build generates broken Python models when an XRD has a property literally named int or bool. The generated models reference undefined type aliases int_aliased and bool_aliased, which makes the models unimportable: PydanticUserError: `ObjectMeta` is not fully defined; you should define `int_aliased`, then call `ObjectMeta.model_rebuild()`. The undefined aliases are emitted by the pinned code generator image docker.io/koxudaxi/datamodel-code-generator:0.31.2. The CLI worked around this with fixAliasedTypesInFile, which text-replaced the broken aliases, but it only ran in the OpenAPI generation path - not the XRD/CRD path that project build uses - so XRD-derived models and the shared meta/v1.py kept the broken references. The underlying code generator bug is fixed upstream in 0.54.0, which sanitizes builtin-conflicting field names by appending a trailing underscore and preserving the wire name via a Pydantic alias. Fields named int now generate as `int_: int | None = Field(None, alias='int')`. This commit bumps the pinned image to 0.59.0, which fixes the broken output at its source. With the fix in place fixAliasedTypesInFile no longer matches anything, so this commit removes it along with the postProcessFile wrapper, leaving both generation paths to call adjustImportsInFile directly. Fixes #63. Signed-off-by: Nic Cope --- internal/schemas/generator/python.go | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/internal/schemas/generator/python.go b/internal/schemas/generator/python.go index c2698be..cf7a043 100644 --- a/internal/schemas/generator/python.go +++ b/internal/schemas/generator/python.go @@ -47,7 +47,7 @@ const ( pythonPackageRoot = "models" pythonAdoptModelsStructure = "sorted" pythonGeneratedFolder = "models/workdir" - pythonImage = "docker.io/koxudaxi/datamodel-code-generator:0.31.2" + pythonImage = "docker.io/koxudaxi/datamodel-code-generator:0.59.0" ) var importRE = regexp.MustCompile(`^(from\s+)(\.*)([^\s]+)(.*)`) @@ -542,19 +542,6 @@ func processOpenAPIContent(doc *openapi3.T) *openapi3.T { //nolint:gocognit // s return doc } -func fixAliasedTypesInFile(fs afero.Fs, filePath string) error { - fileContent, err := afero.ReadFile(fs, filePath) - if err != nil { - return errors.Wrapf(err, "reading file %s", filePath) - } - - content := string(fileContent) - content = strings.ReplaceAll(content, "bool_aliased", "bool") - content = strings.ReplaceAll(content, "int_aliased", "int") - - return afero.WriteFile(fs, filePath, []byte(content), os.ModePerm) -} - func postTransformOpenAPI(fs afero.Fs, sourceDir, targetDir string) error { createdInitDirs := make(map[string]bool) @@ -587,8 +574,8 @@ func postTransformOpenAPI(fs afero.Fs, sourceDir, targetDir string) error { return err } - if err := postProcessFile(fs, destPath); err != nil { - return err + if err := adjustImportsInFile(fs, destPath); err != nil { + return errors.Wrapf(err, "adjusting imports") } return transformMetaImportsInFile(fs, destPath) @@ -679,13 +666,6 @@ func copyFileWithInit(fs afero.Fs, srcPath, destPath, destDir string, created ma return nil } -func postProcessFile(fs afero.Fs, path string) error { - if err := adjustImportsInFile(fs, path); err != nil { - return errors.Wrapf(err, "adjusting imports") - } - return errors.Wrapf(fixAliasedTypesInFile(fs, path), "fixing aliased types") -} - func isMetaV1File(path string) bool { return strings.HasSuffix(filepath.ToSlash(path), "apis/meta/v1.py") }