Skip to content

Commit 9814fc3

Browse files
authored
Merge pull request #1122 from mtardy/pr/mtardy/embedded-alias
🐛 pkg/crd: fix alias type parsing for struct type alias
2 parents c71f903 + cf0446d commit 9814fc3

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

pkg/crd/schema.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
268268
// This reproduces the behavior we had pre gotypesalias=1 (needed if this
269269
// project is compiled with default settings and Go >= 1.23).
270270
if aliasInfo, isAlias := typeInfo.(*types.Alias); isAlias {
271-
typeInfo = aliasInfo.Underlying()
271+
typeInfo = aliasInfo.Rhs()
272272
}
273273
if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic {
274274
typ, fmt, err := builtinToType(basicInfo, ctx.allowDangerousTypes)

pkg/crd/testdata/cronjob_types.go

+11
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,17 @@ type CronJobSpec struct {
374374

375375
// This tests that selectable field.
376376
SelectableFieldString string `json:"selectableFieldString,omitempty"`
377+
378+
// This tests that embedded struct, which is an alias type, is handled correctly.
379+
InlineAlias `json:",inline"`
380+
}
381+
382+
type InlineAlias = EmbeddedStruct
383+
384+
// EmbeddedStruct is for testing that embedded struct is handled correctly when it is used through an alias type.
385+
type EmbeddedStruct struct {
386+
// FromEmbedded is a field from the embedded struct that was used through an alias type.
387+
FromEmbedded string `json:"fromEmbedded,omitempty"`
377388
}
378389

379390
type StringAlias = string

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

+16-10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ spec:
4141
spec:
4242
description: CronJobSpec defines the desired state of CronJob
4343
properties:
44+
aliasFromPackage:
45+
description: |-
46+
This tests that alias imported from a package is handled correctly. The
47+
corev1.IPFamilyPolicyType is just reused since it's available from
48+
imported package. We can create our own in a separate package if needed.
49+
type: string
4450
array:
4551
description: Checks that fixed-length arrays work
4652
items:
@@ -184,13 +190,13 @@ spec:
184190
x-kubernetes-preserve-unknown-fields: true
185191
enumSlice:
186192
description: This tests slice item validation with enum
187-
type: array
188193
items:
189-
type: integer
190194
enum:
191195
- 0
192196
- 1
193197
- 3
198+
type: integer
199+
type: array
194200
explicitlyOptionalKubebuilder:
195201
description: This tests explicitly optional kubebuilder fields
196202
type: string
@@ -228,6 +234,10 @@ spec:
228234
Test that we can add a forbidden field using XValidation Reason and FieldPath.
229235
The validation is applied to the spec struct itself and not the field.
230236
type: integer
237+
fromEmbedded:
238+
description: FromEmbedded is a field from the embedded struct that
239+
was used through an alias type.
240+
type: string
231241
hosts:
232242
description: This tests string slice item validation.
233243
items:
@@ -8984,22 +8994,18 @@ spec:
89848994
time for any reason. Missed jobs executions will be counted as failed ones.
89858995
format: int64
89868996
type: integer
8987-
aliasFromPackage:
8988-
description: |-
8989-
This tests that alias imported from a package is handled correctly. The
8990-
corev1.IPFamilyPolicyType is just reused since it's available from
8991-
imported package. We can create our own in a separate package if needed.
8992-
type: string
89938997
stringAlias:
89948998
description: This tests that string alias is handled correctly.
89958999
type: string
89969000
stringAliasAddedValidation:
8997-
description: This tests that validation on a string alias type is handled correctly.
9001+
description: This tests that validation on a string alias type is
9002+
handled correctly.
89989003
maxLength: 255
89999004
minLength: 1
90009005
type: string
90019006
stringAliasAlreadyValidated:
9002-
description: This tests that validation on a the string alias type itself is handled correctly.
9007+
description: This tests that validation on a the string alias type
9008+
itself is handled correctly.
90039009
maxLength: 255
90049010
minLength: 1
90059011
type: string

0 commit comments

Comments
 (0)