Skip to content

Commit

Permalink
openapi3: fix validation of non-empty interface slice value against a…
Browse files Browse the repository at this point in the history
…rray schema (#752)

Resolves #751
  • Loading branch information
andrewyang96 committed Jan 25, 2023
1 parent 9f99fee commit a3a19f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions openapi3/schema.go
Expand Up @@ -1120,6 +1120,17 @@ func (schema *Schema) visitJSON(settings *schemaValidationSettings, value interf
return schema.visitJSONObject(settings, values)
}
}

// Catch slice of non-empty interface type
if reflect.TypeOf(value).Kind() == reflect.Slice {
valueR := reflect.ValueOf(value)
newValue := make([]interface{}, valueR.Len())
for i := 0; i < valueR.Len(); i++ {
newValue[i] = valueR.Index(i).Interface()
}
return schema.visitJSONArray(settings, newValue)
}

return &SchemaError{
Value: value,
Schema: schema,
Expand Down
12 changes: 12 additions & 0 deletions openapi3/schema_test.go
Expand Up @@ -1354,3 +1354,15 @@ enum:
err = schema.VisitJSON(map[string]interface{}{"d": "e"})
require.Error(t, err)
}

func TestIssue751(t *testing.T) {
schema := &Schema{
Type: "array",
UniqueItems: true,
Items: NewStringSchema().NewRef(),
}
validData := []string{"foo", "bar"}
invalidData := []string{"foo", "foo"}
require.NoError(t, schema.VisitJSON(validData))
require.ErrorContains(t, schema.VisitJSON(invalidData), "duplicate items found")
}

0 comments on commit a3a19f0

Please sign in to comment.