Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/crd/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func flattenAllOfInto(dst *apiextensionsv1.JSONSchemaProps, src apiextensionsv1.
case "Title", "Description", "Example", "ExternalDocs":
// don't merge because we pre-merge to properly preserve field docs
continue
case "Enum":
// Enum from field markers should be preserved even if the type schema doesn't have it
// This is important for types like IntOrString where field-level enum validation
// needs to be preserved during flattening
if len(src.Enum) > 0 && len(dst.Enum) == 0 {
dst.Enum = make([]apiextensionsv1.JSON, len(src.Enum))
copy(dst.Enum, src.Enum)
}
continue
}
srcField := srcVal.Field(i)
fldTyp := srcField.Type()
Expand Down
6 changes: 6 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ type CronJobSpec struct {
// +kubebuilder:validation:Pattern="^((100|[0-9]{1,2})%|[0-9]+)$"
IntOrStringWithAPattern *intstr.IntOrString `json:"intOrStringWithAPattern,omitempty"`

// This tests that an IntOrString can have enum validation.
// The XIntOrString marker is required for applying enum to IntOrString.
// +kubebuilder:validation:XIntOrString
// +kubebuilder:validation:Enum=1;2;"foo";"bar"
IntOrStringWithEnum *intstr.IntOrString `json:"intOrStringWithEnum,omitempty"`

// Checks that nested maps work
NestedMap map[string]map[string]string `json:"nestedMap,omitempty"`

Expand Down
13 changes: 13 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ spec:
minLength: 2
pattern: ^((100|[0-9]{1,2})%|[0-9]+)$
x-kubernetes-int-or-string: true
intOrStringWithEnum:
anyOf:
- type: integer
- type: string
description: |-
This tests that an IntOrString can have enum validation.
The XIntOrString marker is required for applying enum to IntOrString.
enum:
- 1
- 2
- foo
- bar
x-kubernetes-int-or-string: true
intWithValidations:
maximum: 2
minimum: -2
Expand Down
Loading