Skip to content

Commit

Permalink
Testing embedded alias fix
Browse files Browse the repository at this point in the history
Signed-off-by: Tero Saarni <tero.saarni@est.tech>
  • Loading branch information
tsaarni committed Nov 9, 2024
1 parent ab4fa74 commit 50fddb6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
Format: fmt,
}
}
// Handle embedded structs, they do not have a type name.
if _, isNamed := typeInfo.(*types.Named); !isNamed {
// TODO: Following uses deprecated "ident.Obj" so alternative solution is needed.
typeSpec, ok := ident.Obj.Decl.(*ast.TypeSpec)
if !ok {
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("unknown error"), ident))
return &apiext.JSONSchemaProps{}
}
return typeToSchema(ctx.ForInfo(&markers.TypeInfo{}), typeSpec.Type)
}

// NB(directxman12): if there are dot imports, this might be an external reference,
// so use typechecking info to get the actual object
typeNameInfo := typeInfo.(interface{ Obj() *types.TypeName }).Obj()
Expand Down
11 changes: 11 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ 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 StringAlias = string
Expand All @@ -380,6 +383,14 @@ type StringAlias = string
// +kubebuilder:validation:MaxLength=255
type StringAliasWithValidation = string

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 ContainsNestedMap struct {
InnerMap map[string]string `json:"innerMap,omitempty"`
}
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 50fddb6

Please sign in to comment.