@@ -68,25 +68,29 @@ type schemaContext struct {
68
68
69
69
schemaRequester schemaRequester
70
70
PackageMarkers markers.MarkerValues
71
+
72
+ allowDangerousTypes bool
71
73
}
72
74
73
75
// newSchemaContext constructs a new schemaContext for the given package and schema requester.
74
76
// It must have type info added before use via ForInfo.
75
- func newSchemaContext (pkg * loader.Package , req schemaRequester ) * schemaContext {
77
+ func newSchemaContext (pkg * loader.Package , req schemaRequester , allowDangerousTypes bool ) * schemaContext {
76
78
pkg .NeedTypesInfo ()
77
79
return & schemaContext {
78
- pkg : pkg ,
79
- schemaRequester : req ,
80
+ pkg : pkg ,
81
+ schemaRequester : req ,
82
+ allowDangerousTypes : allowDangerousTypes ,
80
83
}
81
84
}
82
85
83
86
// ForInfo produces a new schemaContext with containing the same information
84
87
// as this one, except with the given type information.
85
88
func (c * schemaContext ) ForInfo (info * markers.TypeInfo ) * schemaContext {
86
89
return & schemaContext {
87
- pkg : c .pkg ,
88
- info : info ,
89
- schemaRequester : c .schemaRequester ,
90
+ pkg : c .pkg ,
91
+ info : info ,
92
+ schemaRequester : c .schemaRequester ,
93
+ allowDangerousTypes : c .allowDangerousTypes ,
90
94
}
91
95
}
92
96
@@ -206,7 +210,7 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
206
210
return & apiext.JSONSchemaProps {}
207
211
}
208
212
if basicInfo , isBasic := typeInfo .(* types.Basic ); isBasic {
209
- typ , fmt , err := builtinToType (basicInfo )
213
+ typ , fmt , err := builtinToType (basicInfo , ctx . allowDangerousTypes )
210
214
if err != nil {
211
215
ctx .pkg .AddError (loader .ErrFromNode (err , ident ))
212
216
}
@@ -398,8 +402,8 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON
398
402
399
403
// builtinToType converts builtin basic types to their equivalent JSON schema form.
400
404
// It *only* handles types allowed by the kubernetes API standards. Floats are not
401
- // allowed.
402
- func builtinToType (basic * types.Basic ) (typ string , format string , err error ) {
405
+ // allowed unless allowDangerousTypes is true
406
+ func builtinToType (basic * types.Basic , allowDangerousTypes bool ) (typ string , format string , err error ) {
403
407
// NB(directxman12): formats from OpenAPI v3 are slightly different than those defined
404
408
// in JSONSchema. This'll use the OpenAPI v3 ones, since they're useful for bounding our
405
409
// non-string types.
@@ -411,6 +415,8 @@ func builtinToType(basic *types.Basic) (typ string, format string, err error) {
411
415
typ = "string"
412
416
case basicInfo & types .IsInteger != 0 :
413
417
typ = "integer"
418
+ case basicInfo & types .IsFloat != 0 && allowDangerousTypes :
419
+ typ = "number"
414
420
default :
415
421
// NB(directxman12): floats are *NOT* allowed in kubernetes APIs
416
422
return "" , "" , fmt .Errorf ("unsupported type %q" , basic .String ())
0 commit comments