Skip to content

Commit

Permalink
set min/maxItems and min/maxProperties when generating openapi
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Kalinin <[email protected]>
  • Loading branch information
Dmitriy Kalinin committed Apr 14, 2024
1 parent eacb7bc commit 43dee5f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
21 changes: 21 additions & 0 deletions pkg/cmd/template/schema_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,13 @@ foo:
#@schema/validation min_len=1, max_len=10
string_key: ""
#@schema/validation min_len=3, max_len=4
array_key:
- ""
#@schema/validation min_len=2, max_len=5
map_key: {}
#@schema/validation one_of=[1,2,3]
one_of_integers: 1
Expand Down Expand Up @@ -616,6 +623,20 @@ components:
default: ""
minLength: 1
maxLength: 10
array_key:
type: array
items:
type: string
default: ""
default: []
minItems: 3
maxItems: 4
map_key:
type: object
additionalProperties: false
properties: {}
minProperties: 2
maxProperties: 5
one_of_integers:
type: integer
default: 1
Expand Down
36 changes: 32 additions & 4 deletions pkg/schema/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ const (
defaultProp = "default"
minProp = "minimum"
maxProp = "maximum"
minLenProp = "minLength"
minLenProp = "minLength" // for strings
maxLenProp = "maxLength"
minItemsProp = "minItems" // for arrays
maxItemsProp = "maxItems"
minPropertiesProp = "minProperties" // for objects
maxPropertiesProp = "maxProperties"
enumProp = "enum"
)

Expand All @@ -48,7 +52,11 @@ var propOrder = map[string]int{
maxProp: 13,
minLenProp: 14,
maxLenProp: 15,
enumProp: 16,
minItemsProp: 16,
maxItemsProp: 17,
minPropertiesProp: 18,
maxPropertiesProp: 19,
enumProp: 20,
}

type openAPIKeys []*yamlmeta.MapItem
Expand Down Expand Up @@ -206,10 +214,30 @@ func convertValidations(schemaVal Type) []*yamlmeta.MapItem {
var items []*yamlmeta.MapItem

if value, found := validation.HasSimpleMinLength(); found {
items = append(items, &yamlmeta.MapItem{Key: minLenProp, Value: value})
containedValue := schemaVal.GetValueType()
switch containedValue.(type) {
case *ArrayType:
items = append(items, &yamlmeta.MapItem{Key: minItemsProp, Value: value})
case *MapType:
items = append(items, &yamlmeta.MapItem{Key: minPropertiesProp, Value: value})
default:
if containedValue.String() == "string" {
items = append(items, &yamlmeta.MapItem{Key: minLenProp, Value: value})
}
}
}
if value, found := validation.HasSimpleMaxLength(); found {
items = append(items, &yamlmeta.MapItem{Key: maxLenProp, Value: value})
containedValue := schemaVal.GetValueType()
switch containedValue.(type) {
case *ArrayType:
items = append(items, &yamlmeta.MapItem{Key: maxItemsProp, Value: value})
case *MapType:
items = append(items, &yamlmeta.MapItem{Key: maxPropertiesProp, Value: value})
default:
if containedValue.String() == "string" {
items = append(items, &yamlmeta.MapItem{Key: maxLenProp, Value: value})
}
}
}
if value, found := validation.HasSimpleMin(); found {
items = append(items, &yamlmeta.MapItem{Key: minProp, Value: value})
Expand Down

0 comments on commit 43dee5f

Please sign in to comment.