Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Process only unprocessed tags in recursion #103

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fixtures/array_handling.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"ArrayHandler": {
"properties": {
"min_len": {
"default": [
"qwerty"
],
"items": {
"type": "string",
"minLength": 2
Expand Down
25 changes: 19 additions & 6 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func (t *Schema) structKeywordsFromTags(f reflect.StructField, parent *Schema, p
t.Description = f.Tag.Get("jsonschema_description")

tags := splitOnUnescapedCommas(f.Tag.Get("jsonschema"))
t.genericKeywords(tags, parent, propertyName)
tags = t.genericKeywords(tags, parent, propertyName)

switch t.Type {
case "string":
Expand All @@ -677,7 +677,8 @@ func (t *Schema) structKeywordsFromTags(f reflect.StructField, parent *Schema, p
}

// read struct tags for generic keywords
func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName string) { //nolint:gocyclo
func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName string) []string { //nolint:gocyclo
unprocessed := make([]string, 0, len(tags))
for _, tag := range tags {
nameValue := strings.Split(tag, "=")
if len(nameValue) == 2 {
Expand Down Expand Up @@ -773,9 +774,12 @@ func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName str
Type: ty,
})
}
default:
unprocessed = append(unprocessed, tag)
}
}
}
return unprocessed
}

// read struct tags for boolean type keywords
Expand Down Expand Up @@ -886,6 +890,8 @@ func (t *Schema) numericalKeywords(tags []string) {
// read struct tags for array type keywords
func (t *Schema) arrayKeywords(tags []string) {
var defaultValues []any

unprocessed := make([]string, 0, len(tags))
for _, tag := range tags {
nameValue := strings.Split(tag, "=")
if len(nameValue) == 2 {
Expand All @@ -905,24 +911,31 @@ func (t *Schema) arrayKeywords(tags []string) {
t.Items.Format = val
case "pattern":
t.Items.Pattern = val
default:
unprocessed = append(unprocessed, tag) // left for further processing by underlying type
}
}
}
if len(defaultValues) > 0 {
t.Default = defaultValues
}

if len(unprocessed) == 0 {
// we don't have anything else to process
return
}

switch t.Items.Type {
case "string":
t.Items.stringKeywords(tags)
t.Items.stringKeywords(unprocessed)
case "number":
t.Items.numericalKeywords(tags)
t.Items.numericalKeywords(unprocessed)
case "integer":
t.Items.numericalKeywords(tags)
t.Items.numericalKeywords(unprocessed)
case "array":
// explicitly don't support traversal for the [][]..., as it's unclear where the array tags belong
case "boolean":
t.Items.booleanKeywords(tags)
t.Items.booleanKeywords(unprocessed)
}
}

Expand Down
2 changes: 1 addition & 1 deletion reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func TestNumberHandling(t *testing.T) {

func TestArrayHandling(t *testing.T) {
type ArrayHandler struct {
MinLen []string `json:"min_len" jsonschema:"minLength=2"`
MinLen []string `json:"min_len" jsonschema:"minLength=2,default=qwerty"`
MinVal []float64 `json:"min_val" jsonschema:"minimum=2.5"`
}

Expand Down
Loading