From 470bfea382f4dacd7d1c407934d301c7da862c29 Mon Sep 17 00:00:00 2001 From: Mahe Tardy Date: Mon, 6 Jan 2025 11:55:19 +0100 Subject: [PATCH] pkg/crd: add a test for embedded/inline struct type alias User tsaarni bumped into a panic when using embedded/inline field with a type alias and that Alias types are enabled in Go. They thankfully included a reproducer that is shipped in this patch from the issue kubernetes-sigs/controller-tools#1088. Co-authored-by: Tero Saarni Signed-off-by: Mahe Tardy --- pkg/crd/testdata/cronjob_types.go | 11 +++++++++++ .../testdata/testdata.kubebuilder.io_cronjobs.yaml | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/pkg/crd/testdata/cronjob_types.go b/pkg/crd/testdata/cronjob_types.go index 4569d2f7d..4cf20fe12 100644 --- a/pkg/crd/testdata/cronjob_types.go +++ b/pkg/crd/testdata/cronjob_types.go @@ -374,6 +374,17 @@ type CronJobSpec struct { // This tests that selectable field. SelectableFieldString string `json:"selectableFieldString,omitempty"` + + // This tests that embedded struct, which is an alias type, is handled correctly. + InlineAlias `json:",inline"` +} + +type InlineAlias = EmbeddedStruct + +// EmbeddedStruct is for testing that embedded struct is handled correctly when it is used through an alias type. +type EmbeddedStruct struct { + // FromEmbedded is a field from the embedded struct that was used through an alias type. + FromEmbedded string `json:"fromEmbedded,omitempty"` } type StringAlias = string diff --git a/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml b/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml index a3bcc2f89..bca4851ef 100644 --- a/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml +++ b/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml @@ -234,6 +234,10 @@ spec: Test that we can add a forbidden field using XValidation Reason and FieldPath. The validation is applied to the spec struct itself and not the field. type: integer + fromEmbedded: + description: FromEmbedded is a field from the embedded struct that + was used through an alias type. + type: string hosts: description: This tests string slice item validation. items: