Skip to content

Commit a3a19f0

Browse files
authored
openapi3: fix validation of non-empty interface slice value against array schema (#752)
Resolves #751
1 parent 9f99fee commit a3a19f0

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

openapi3/schema.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,17 @@ func (schema *Schema) visitJSON(settings *schemaValidationSettings, value interf
11201120
return schema.visitJSONObject(settings, values)
11211121
}
11221122
}
1123+
1124+
// Catch slice of non-empty interface type
1125+
if reflect.TypeOf(value).Kind() == reflect.Slice {
1126+
valueR := reflect.ValueOf(value)
1127+
newValue := make([]interface{}, valueR.Len())
1128+
for i := 0; i < valueR.Len(); i++ {
1129+
newValue[i] = valueR.Index(i).Interface()
1130+
}
1131+
return schema.visitJSONArray(settings, newValue)
1132+
}
1133+
11231134
return &SchemaError{
11241135
Value: value,
11251136
Schema: schema,

openapi3/schema_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,3 +1354,15 @@ enum:
13541354
err = schema.VisitJSON(map[string]interface{}{"d": "e"})
13551355
require.Error(t, err)
13561356
}
1357+
1358+
func TestIssue751(t *testing.T) {
1359+
schema := &Schema{
1360+
Type: "array",
1361+
UniqueItems: true,
1362+
Items: NewStringSchema().NewRef(),
1363+
}
1364+
validData := []string{"foo", "bar"}
1365+
invalidData := []string{"foo", "foo"}
1366+
require.NoError(t, schema.VisitJSON(validData))
1367+
require.ErrorContains(t, schema.VisitJSON(invalidData), "duplicate items found")
1368+
}

0 commit comments

Comments
 (0)