From 4a457d97f4bcd0cf66ee1f4257674a457ac02505 Mon Sep 17 00:00:00 2001 From: yongruilin Date: Thu, 15 Jan 2026 06:43:07 +0000 Subject: [PATCH] Add tests for invalid numeric formats --- .../apiserver/validation/validation_test.go | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go index df2c6dd8d5a..586551af7bb 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go @@ -670,6 +670,62 @@ func TestValidateCustomResource(t *testing.T) { }, }, }, + {name: "numeric formats invalid", + schema: apiextensions.JSONSchemaProps{ + Type: "object", + Properties: map[string]apiextensions.JSONSchemaProps{ + "intThirtyTwo": {Type: "integer", Format: "int32"}, + "intSixtyFour": {Type: "integer", Format: "int64"}, + "floatThreeTwo": {Type: "number", Format: "float"}, + "floatSixFour": {Type: "number", Format: "double"}, + }, + }, + failingObjects: []failingObject{ + { + object: map[string]interface{}{"intThirtyTwo": int64(math.MaxInt32 + 1)}, + expectErrs: []string{ + `: Invalid value: "": Checked value must be of type integer with format int32 in intThirtyTwo`, + }, + }, + { + object: map[string]interface{}{"intThirtyTwo": int64(math.MinInt32 - 1)}, + expectErrs: []string{ + `: Invalid value: "": Checked value must be of type integer with format int32 in intThirtyTwo`, + }, + }, + // int64 overflow is not possible with int64 input, but we can test it with float64 + { + object: map[string]interface{}{"intSixtyFour": float64(math.MaxInt64) * 1.1}, + expectErrs: []string{ + `intSixtyFour: Invalid value: "float64": intSixtyFour in body must be of type int64: "float64"`, + `: Invalid value: "": Checked value must be of type integer with format int64 in intSixtyFour`, + }, + }, + { + object: map[string]interface{}{"intSixtyFour": float64(math.MinInt64) * 1.1}, + expectErrs: []string{ + `intSixtyFour: Invalid value: "float64": intSixtyFour in body must be of type int64: "float64"`, + `: Invalid value: "": Checked value must be of type integer with format int64 in intSixtyFour`, + }, + }, + { + object: map[string]interface{}{"floatThreeTwo": float64(math.MaxFloat32 * 1.1)}, + expectErrs: []string{ + `: Invalid value: "": Checked value must be of type number with format float in floatThreeTwo`, + }, + }, + { + object: map[string]interface{}{"floatThreeTwo": float64(-math.MaxFloat32 * 1.1)}, + expectErrs: []string{ + `: Invalid value: "": Checked value must be of type number with format float in floatThreeTwo`, + }, + }, + // double overflow (float64) is handled by JSON parsing, but we can try to pass a value that might trigger it if not parsed as such. + // However, standard JSON unmarshalling usually caps at float64 limits or errors out. + // The validator itself checks ranges. Since Go's float64 matches double, true overflow is hard to represent without a custom numeric type. + // We will skip explicit double overflow tests here as they often result in +Inf/-Inf which might be handled differently or parse errors. + }, + }, } for _, tt := range tests { compatVersion := tt.compatVersion