Skip to content

✨Support XValidation on type aliases to map/slice/pointer types - #1463

Open
dongjiang1989 wants to merge 1 commit into
kubernetes-sigs:mainfrom
dongjiang1989:object-alias-XValidation
Open

✨Support XValidation on type aliases to map/slice/pointer types#1463
dongjiang1989 wants to merge 1 commit into
kubernetes-sigs:mainfrom
dongjiang1989:object-alias-XValidation

Conversation

@dongjiang1989

@dongjiang1989 dongjiang1989 commented Aug 5, 2026

Copy link
Copy Markdown
Member

What does this PR do, and why do we need it?

Fixes #1462

controller-gen did not generate x-kubernetes-validations for type aliases to complex types (e.g. type X = map[string]string). The alias was unwrapped to the underlying type in localNamedToSchema, which hit the default case and produced an error:

unsupported type *types.Map for identifier X

Changes

For user-defined type aliases (type X = Y) whose RHS is a non-basic, non-named type (map, slice, pointer, etc.), emit a $ref to the alias type instead of unwrapping it. The flattener then resolves the $ref via infoToSchema, which correctly applies markers on the alias declaration (including +kubebuilder:validation:XValidation).

Behavior for other alias types is preserved:

  • Basic type aliases (type X = string): unwrap, emit inline type/format + $ref (preserves field-level markers like MinLength/MaxLength)
  • Named type aliases (type X = SomeStruct): unwrap, emit $ref to the underlying named type (preserves inline/embedded struct alias behavior and applyconfiguration generator compatibility)
  • Built-in aliases (any): unwrap (no $ref, since any is not a user-defined type)

Example

// +kubebuilder:validation:XValidation:rule=`self.all(key, size(key) > 0)`,message="keys must be non-empty"
type Labels = map[string]string

type MySpec struct {
    Labels Labels `json:"labels,omitempty"`
}

Now correctly generates:

labels:
  additionalProperties:
    type: string
  type: object
  x-kubernetes-validations:
  - message: keys must be non-empty
    rule: self.all(key, size(key) > 0)

@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dongjiang1989
Once this PR has been reviewed and has the lgtm label, please assign sbueringer for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 5, 2026
@dongjiang1989 dongjiang1989 changed the title 🐛 support XValidation on type aliases to map/slice/pointer types ✨support XValidation on type aliases to map/slice/pointer types Aug 5, 2026
controller-gen did not generate x-kubernetes-validations for type aliases
to complex types (e.g. type X = map[string]string). The alias was unwrapped
to the underlying type, which hit the default case in localNamedToSchema
and produced an error ("unsupported type *types.Map for identifier X").

For user-defined aliases to non-basic, non-named types, emit a $ref to
the alias type so the flattener resolves it via infoToSchema, applying
markers on the alias declaration. Basic and named type aliases keep
their existing behavior.

Fixes: kubernetes-sigs#1462
Signed-off-by: dongjiang <[email protected]>
@dongjiang1989
dongjiang1989 force-pushed the object-alias-XValidation branch from 43921d4 to 9f9967c Compare August 5, 2026 09:22
@dongjiang1989 dongjiang1989 changed the title ✨support XValidation on type aliases to map/slice/pointer types ✨Support XValidation on type aliases to map/slice/pointer types Aug 5, 2026
@dongjiang1989

Copy link
Copy Markdown
Member Author

cc @sbueringer PTAL, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

controller-gen does not support XValidation on type aliases

1 participant