mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-02-22 07:03:28 +00:00
Merge pull request #133896 from yongruilin/fix-format-warning
fix: Only warn for unrecognized formats on type=string
This commit is contained in:
@@ -113,9 +113,11 @@ func GetUnrecognizedFormats(schema *spec.Schema, compatibilityVersion *version.V
|
||||
return unrecognizedFormats
|
||||
}
|
||||
|
||||
normalized := strings.ReplaceAll(schema.Format, "-", "") // go-openapi default format name normalization
|
||||
if !supportedFormatsAtVersion(compatibilityVersion).supported.Has(normalized) {
|
||||
unrecognizedFormats = append(unrecognizedFormats, schema.Format)
|
||||
if len(schema.Type) == 1 && schema.Type[0] == "string" {
|
||||
normalized := strings.ReplaceAll(schema.Format, "-", "") // go-openapi default format name normalization
|
||||
if !supportedFormatsAtVersion(compatibilityVersion).supported.Has(normalized) {
|
||||
unrecognizedFormats = append(unrecognizedFormats, schema.Format)
|
||||
}
|
||||
}
|
||||
|
||||
return unrecognizedFormats
|
||||
|
||||
@@ -148,58 +148,76 @@ func TestGetUnrecognizedFormats(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "empty format",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: ""}},
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "", Type: []string{"string"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 0),
|
||||
expectedFormats: []string{},
|
||||
},
|
||||
{
|
||||
name: "recognized format at version 1.0",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email"}},
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email", Type: []string{"string"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 0),
|
||||
expectedFormats: []string{},
|
||||
},
|
||||
{
|
||||
name: "unrecognized format at version 1.0",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format"}},
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"string"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 0),
|
||||
expectedFormats: []string{"unknown-format"},
|
||||
},
|
||||
{
|
||||
name: "recognized format with normalization",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name"}},
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name", Type: []string{"string"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 34),
|
||||
expectedFormats: []string{},
|
||||
},
|
||||
{
|
||||
name: "unrecognized format with normalization",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-long-name"}},
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-long-name", Type: []string{"string"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 33),
|
||||
expectedFormats: []string{"k8s-long-name"},
|
||||
},
|
||||
{
|
||||
name: "format introduced in later version",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name"}},
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name", Type: []string{"string"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 0),
|
||||
expectedFormats: []string{"k8s-short-name"},
|
||||
},
|
||||
{
|
||||
name: "format with dash normalization",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8sshortname"}},
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8sshortname", Type: []string{"string"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 34),
|
||||
expectedFormats: []string{},
|
||||
},
|
||||
{
|
||||
name: "recognized format at exact version",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "uuid"}},
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "uuid", Type: []string{"string"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 0),
|
||||
expectedFormats: []string{},
|
||||
},
|
||||
{
|
||||
name: "recognized format at higher version",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email"}},
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email", Type: []string{"string"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 35),
|
||||
expectedFormats: []string{},
|
||||
},
|
||||
{
|
||||
name: "unrecognized format for integer type is not reported",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"integer"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 0),
|
||||
expectedFormats: []string{},
|
||||
},
|
||||
{
|
||||
name: "unrecognized format for string,null type is not reported",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"string", "null"}}},
|
||||
compatibilityVersion: version.MajorMinor(1, 0),
|
||||
expectedFormats: []string{},
|
||||
},
|
||||
{
|
||||
name: "unrecognized format for no type is not reported",
|
||||
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format"}},
|
||||
compatibilityVersion: version.MajorMinor(1, 0),
|
||||
expectedFormats: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
|
||||
@@ -223,7 +223,7 @@ func getUnrecognizedFormatsInSchema(schema *apiextensions.JSONSchemaProps, compa
|
||||
validation.SchemaHas(schema, func(s *apiextensions.JSONSchemaProps) bool {
|
||||
if len(s.Format) > 0 {
|
||||
// Convert to spec.Schema for format validation
|
||||
specSchema := &spec.Schema{SchemaProps: spec.SchemaProps{Format: s.Format}}
|
||||
specSchema := &spec.Schema{SchemaProps: spec.SchemaProps{Format: s.Format, Type: []string{s.Type}}}
|
||||
if formats := apiservervalidation.GetUnrecognizedFormats(specSchema, compatibilityVersion); len(formats) > 0 {
|
||||
unrecognizedFormats = append(unrecognizedFormats, formats...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user