Skip to content

Commit 49d7045

Browse files
committed
Apply type and format from underlying named types
1 parent fe5d201 commit 49d7045

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

pkg/crd/schema.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type applyFirstMarker interface {
6060
// schemaRequester knows how to marker that another schema (e.g. via an external reference) is necessary.
6161
type schemaRequester interface {
6262
NeedSchemaFor(typ TypeIdent)
63+
LookupType(pkg *loader.Package, name string) *markers.TypeInfo
6364
}
6465

6566
// schemaContext stores and provides information across a hierarchy of schema generation.
@@ -304,8 +305,26 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
304305
}
305306
ctx.requestSchema(pkgPath, typeNameInfo.Name())
306307
link := TypeRefLink(pkgPath, typeNameInfo.Name())
308+
309+
// In cases where we have a named type, apply the type and format from the named schema
310+
// to allow markers that need this information to apply correctly.
311+
var typ, fmt string
312+
if namedInfo, isNamed := typeInfo.(*types.Named); isNamed {
313+
// We don't want/need to do this for structs, maps, or arrays.
314+
// These are already handled in infoToSchema if they have custom marshalling.
315+
if _, isBasic := namedInfo.Underlying().(*types.Basic); isBasic {
316+
namedTypeInfo := ctx.schemaRequester.LookupType(ctx.pkg, namedInfo.Obj().Name())
317+
318+
namedSchema := infoToSchema(ctx.ForInfo(namedTypeInfo))
319+
typ = namedSchema.Type
320+
fmt = namedSchema.Format
321+
}
322+
}
323+
307324
return &apiext.JSONSchemaProps{
308-
Ref: &link,
325+
Type: typ,
326+
Format: fmt,
327+
Ref: &link,
309328
}
310329
}
311330

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,23 @@ spec:
226226
format: int32
227227
type: integer
228228
fieldLevelAliasOverride:
229+
allOf:
230+
- maxLength: 255
231+
minLength: 1
232+
- maxLength: 10
233+
minLength: 10
229234
description: |-
230235
This tests that field-level overrides are handled correctly
231236
for local type aliases.
232-
maxLength: 10
233-
minLength: 10
234237
type: string
235238
fieldLevelLocalDeclarationOverride:
239+
allOf:
240+
- minLength: 4
241+
- minLength: 10
236242
description: |-
237243
This tests that field-level overrides are handled correctly
238244
for local type declarations.
239245
maxLength: 10
240-
minLength: 10
241246
type: string
242247
float64WithValidations:
243248
maximum: 1.5
@@ -9098,10 +9103,10 @@ spec:
90989103
and type.
90999104
type: string
91009105
x-kubernetes-validations:
9101-
- message: must have even length
9102-
rule: self.size() % 2 == 0
91039106
- message: must have good prefix
91049107
rule: self.startsWith('good-')
9108+
- message: must have even length
9109+
rule: self.size() % 2 == 0
91059110
stringWithEvenLengthAndMessageExpression:
91069111
description: Test of the expression-based validation with messageExpression
91079112
marker.

0 commit comments

Comments
 (0)