Stop stripping valid formats for number/integer types

This commit is contained in:
yongruilin
2026-01-27 21:43:14 +00:00
parent 65b579a036
commit eac0206446
2 changed files with 43 additions and 9 deletions

View File

@@ -96,8 +96,26 @@ func StripUnsupportedFormatsPostProcessorForVersion(compatibilityVersion *versio
return nil
}
normalized := strings.ReplaceAll(s.Format, "-", "") // go-openapi default format name normalization
if !supportedFormatsAtVersion(compatibilityVersion).supported.Has(normalized) {
schemaType := ""
if len(s.Type) == 1 {
schemaType = s.Type[0]
}
switch schemaType {
case "", "string":
normalized := strings.ReplaceAll(s.Format, "-", "") // go-openapi default format name normalization
if !supportedFormatsAtVersion(compatibilityVersion).supported.Has(normalized) {
s.Format = ""
}
case "integer":
if s.Format != "int32" && s.Format != "int64" {
s.Format = ""
}
case "number":
if s.Format != "float" && s.Format != "double" {
s.Format = ""
}
default:
// Format not supported on other types
s.Format = ""
}
@@ -113,11 +131,27 @@ func GetUnrecognizedFormats(schema *spec.Schema, compatibilityVersion *version.V
return unrecognizedFormats
}
if len(schema.Type) == 1 && schema.Type[0] == "string" {
schemaType := ""
if len(schema.Type) == 1 {
schemaType = schema.Type[0]
}
switch schemaType {
case "", "string":
normalized := strings.ReplaceAll(schema.Format, "-", "") // go-openapi default format name normalization
if !supportedFormatsAtVersion(compatibilityVersion).supported.Has(normalized) {
unrecognizedFormats = append(unrecognizedFormats, schema.Format)
}
case "integer":
if schema.Format != "int32" && schema.Format != "int64" {
unrecognizedFormats = append(unrecognizedFormats, schema.Format)
}
case "number":
if schema.Format != "float" && schema.Format != "double" {
unrecognizedFormats = append(unrecognizedFormats, schema.Format)
}
default:
// Format not supported on other types
unrecognizedFormats = append(unrecognizedFormats, schema.Format)
}
return unrecognizedFormats

View File

@@ -201,22 +201,22 @@ func TestGetUnrecognizedFormats(t *testing.T) {
expectedFormats: []string{},
},
{
name: "unrecognized format for integer type is not reported",
name: "unrecognized format for integer type is reported",
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"integer"}}},
compatibilityVersion: version.MajorMinor(1, 0),
expectedFormats: []string{},
expectedFormats: []string{"unknown-format"},
},
{
name: "unrecognized format for string,null type is not reported",
name: "unrecognized format for string,null type is reported",
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"string", "null"}}},
compatibilityVersion: version.MajorMinor(1, 0),
expectedFormats: []string{},
expectedFormats: []string{"unknown-format"},
},
{
name: "unrecognized format for no type is not reported",
name: "unrecognized format for no type is reported",
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format"}},
compatibilityVersion: version.MajorMinor(1, 0),
expectedFormats: []string{},
expectedFormats: []string{"unknown-format"},
},
}