Skip to content

Commit 59a61f0

Browse files
committed
Revert "Stop setting invalid formats int32/int64 for integer types"
This reverts commit 02ea9af.
1 parent d55abc7 commit 59a61f0

File tree

3 files changed

+144
-8
lines changed

3 files changed

+144
-8
lines changed

pkg/crd/schema.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
271271
typeInfo = aliasInfo.Rhs()
272272
}
273273
if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic {
274-
typ, err := builtinToType(basicInfo, ctx.allowDangerousTypes)
274+
typ, fmt, err := builtinToType(basicInfo, ctx.allowDangerousTypes)
275275
if err != nil {
276276
ctx.pkg.AddError(loader.ErrFromNode(err, ident))
277277
}
@@ -284,12 +284,14 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
284284
ctx.requestSchema("", ident.Name)
285285
link := TypeRefLink("", ident.Name)
286286
return &apiext.JSONSchemaProps{
287-
Type: typ,
288-
Ref: &link,
287+
Type: typ,
288+
Format: fmt,
289+
Ref: &link,
289290
}
290291
}
291292
return &apiext.JSONSchemaProps{
292-
Type: typ,
293+
Type: typ,
294+
Format: fmt,
293295
}
294296
}
295297
// NB(directxman12): if there are dot imports, this might be an external reference,
@@ -552,7 +554,7 @@ func validateOneOfValues(fields ...string) error {
552554
// builtinToType converts builtin basic types to their equivalent JSON schema form.
553555
// It *only* handles types allowed by the kubernetes API standards. Floats are not
554556
// allowed unless allowDangerousTypes is true
555-
func builtinToType(basic *types.Basic, allowDangerousTypes bool) (typ string, err error) {
557+
func builtinToType(basic *types.Basic, allowDangerousTypes bool) (typ string, format string, err error) {
556558
// NB(directxman12): formats from OpenAPI v3 are slightly different than those defined
557559
// in JSONSchema. This'll use the OpenAPI v3 ones, since they're useful for bounding our
558560
// non-string types.
@@ -568,13 +570,20 @@ func builtinToType(basic *types.Basic, allowDangerousTypes bool) (typ string, er
568570
if allowDangerousTypes {
569571
typ = "number"
570572
} else {
571-
return "", errors.New("found float, the usage of which is highly discouraged, as support for them varies across languages. Please consider serializing your float as string instead. If you are really sure you want to use them, re-run with crd:allowDangerousTypes=true")
573+
return "", "", errors.New("found float, the usage of which is highly discouraged, as support for them varies across languages. Please consider serializing your float as string instead. If you are really sure you want to use them, re-run with crd:allowDangerousTypes=true")
572574
}
573575
default:
574-
return "", fmt.Errorf("unsupported type %q", basic.String())
576+
return "", "", fmt.Errorf("unsupported type %q", basic.String())
575577
}
576578

577-
return typ, nil
579+
switch basic.Kind() {
580+
case types.Int32, types.Uint32:
581+
format = "int32"
582+
case types.Int64, types.Uint64:
583+
format = "int64"
584+
}
585+
586+
return typ, format, nil
578587
}
579588

580589
// Open coded go/types representation of encoding/json.Marshaller

0 commit comments

Comments
 (0)