Skip to content

Commit

Permalink
pkg/crd: add a test for embedded/inline struct type alias
Browse files Browse the repository at this point in the history
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
#1088.

Co-authored-by: Tero Saarni <[email protected]>
Signed-off-by: Mahe Tardy <[email protected]>
  • Loading branch information
mtardy and tsaarni committed Jan 6, 2025
1 parent a1c5e9e commit 470bfea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 470bfea

Please sign in to comment.