Skip to content

Filtering on fields of type dictionary does not work #139

@torie

Description

@torie

I am trying to filter responses by a specific key-value combination for a dictionary field, which seems to not be working.

Steps to reproduce:

Define a resource with a schema.Field with the following specification:

"foobar": {
        Description: "A set of key values.",
        Validator: &schema.Dict{
	        KeysValidator:   &schema.String{MinLen: 1},
		ValuesValidator: &schema.String{MinLen: 1},
	},
	Filterable: true,
},

Make a request to get resources which has a specific key-value pair in the foobar dictionary.

http://myapi.com/myresource?filter={"foobar":{"foo": "bar"}}

The following error occurs.

"Invalid `filter` parameter: foobar: invalid query expression: not a dict"

I've done some digging, and the error occurs in schema/dict.go when the func (v Dict) Validate(...) function attempts to check if the incoming value is of type map[string]interface{}. At this point the value has been parsed into map[string]query.Value which fails the test.

// Validate implements FieldValidator interface.
func (v Dict) Validate(value interface{}) (interface{}, error) {
	dict, ok := value.(map[string]interface{}) // <- value is of type map[string]query.Value here
	if !ok {
		return nil, errors.New("not a dict")
	}
        ...

Metadata

Metadata

Assignees

Labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions