Skip to content

bug: validation of a slice of non-empty interface against an array schema should pass #751

@andrewyang96

Description

@andrewyang96

Context

An array schema validates only against a slice of empty interface: []interface{}{...}. If a slice of non-empty interface (e.g. []string{...}) is attempted to validate against that schema, this error occurs: unhandled value of type []string

Reproduce

package main

import (
	"fmt"

	"github.com/getkin/kin-openapi/openapi3"
)

func main() {
	schema := &openapi3.Schema{
		Type:        "array",
		UniqueItems: true,
		Items:       openapi3.NewStringSchema().NewRef(),
	}
	data := []string{"foo", "bar"}
	err := schema.VisitJSON(data)
	fmt.Println(err)
}

Expected

In this case, a nil error should be printed because VisitJSON would recognize that data is a slice.

There is a bug in VisitJSON where the []interface{} case does not cover all slices:

kin-openapi/openapi3/schema.go

Lines 1108 to 1109 in 9f99fee

case []interface{}:
return schema.visitJSONArray(settings, value)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions