Skip to content

fix: CRD generation fails for schema property named "group" (unknown type Group) - #48

Closed
braghettos wants to merge 1 commit into
krateoplatformops:mainfrom
krateo-platformops:fix/group-property-type
Closed

fix: CRD generation fails for schema property named "group" (unknown type Group)#48
braghettos wants to merge 1 commit into
krateoplatformops:mainfrom
krateo-platformops:fix/group-property-type

Conversation

@braghettos

Copy link
Copy Markdown
Member

The bug

When an OpenAPI/JSON schema has a property literally named group, crdgen fails to generate the CRD. controller-gen aborts with:

types.go:19:9: unknown type Group
types.go:19:2: invalid field type: invalid type
Error: not all generators ran successfully

This happens whether the sub-schema is $ref'd or inlined — the literal property name group is the trigger. Other envelope names (project, user, role, domain, ...) work fine. The concrete case is the OpenStack Keystone Group resource, whose request body is wrapped in a top-level group envelope (spec.group.*).

Root cause

The generated Go type for the group property and the generated apis package share the same package scope:

  • groupversion_info.go emits a package-level constant const Group = "<api group>".
  • types.go emits type Group struct { ... } for the group envelope property, in the same package.

Two declarations of Group in one package is a redeclaration; the field Group *Group then resolves Group to the constant rather than a type, so controller-gen reports unknown type Group. The same latent collision exists for any property named after an identifier crdgen emits at package scope (Group, Version, SchemeGroupVersion, SchemeBuilder, AddToScheme, AddToSchemes).

The fix

In the transpiler, sanitize the generated struct type name (not the field name or JSON tag) so it never equals one of those reserved package-level identifiers. Colliding names get an Envelope suffix (Group -> GroupEnvelope), chosen to also avoid clashing with the <Kind>Spec / <Kind>Status / <Kind>List types crdgen generates. Because both the struct definition and the field's type reference derive from the same name inside processObject, they stay consistent.

The field name and JSON tag are untouched, so the CRD still exposes spec.group.* exactly as before — only the internal Go type is renamed.

Verification

Before: CRD generation fails with unknown type Group.
After: the Keystone group envelope generates a valid CRD with spec.group.{name,domain_id,description} and the correct required markers.

Added a deterministic regression test (TestReservedTypeNameCollision) covering group and version (which must be renamed) alongside project/domain/user/role (which must not), asserting the field type references a defined struct and the JSON name is preserved.

Note: the pre-existing TestDuplicateStructGeneration test is flaky on main independently of this change (it relies on rand.Intn plus Go's randomized map iteration order in the duplicate-name collision path). It is unrelated to this fix and left as-is.

🤖 Generated with Claude Code

A schema property named after an identifier that crdgen emits at package
scope (most notably "group", which becomes the "Group" constant in
groupversion_info.go) produced a struct type of the same name in the same
package. The resulting redeclaration made the field reference resolve to
the constant instead of the type, so CRD generation failed with
"unknown type Group".

Rename such generated struct types (e.g. Group -> GroupEnvelope) while
keeping the field name and JSON tag unchanged, and add a regression test
covering group/version plus previously-working names.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@braghettos

Copy link
Copy Markdown
Member Author

Moving this to a PR within the braghettos fork instead of upstream.

@braghettos braghettos closed this Jul 1, 2026
@braghettos
braghettos deleted the fix/group-property-type branch August 13, 2026 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant