stas/jsonapi.rb

Sparse fields don't work with non-single-word types

kitsuneyo opened this issue · 3 comments

Expected Behavior

Requests for a sparse fieldset to a resource with a type that includes two words, e.g. personAlias, should only return the specified fields.

Example request:

GET /person-aliases?fields[personAlias]=myField

Expected response:

{
    "data": [
        {
            "id": "1",
            "type": "personAlias",
            "attributes": {
                "myField": "foo"
            }
       }
    ]
}

Note that:

  • I'm using jsonapi.rb alongside jsonapi-serializer
  • My serializers have the option set_key_transform :camel_lower
  • I've manually set the the type in my serializers e.g. set_type :person_alias
  • A request with the type given in snake_case, e.g. GET /person-aliases?fields[person_alias]=myField or GET /person-aliases?fields[person_alias]=my_field, does not work either

If I manually set a one-word type, e.g. set_type :foo, things work as expected.

Actual Behavior

The response includes all fields, not the sparse fieldset requested, for example:

{
    "data": [
        {
            "id": "1",
            "type": "personAlias",
            "attributes": {
                "myField": "foo",
                "myOtherField": "foo",
                "anotherOne": "foo",
            }
       }
    ]
}

Steps to Reproduce the Problem

  1. In the serializer, add options set_type :foo_bar and set_key_transform :camel_lower
  2. In the controller, include JSONAPI::Fetching etc. and render with render jsonapi: result, status: :ok

Specifications

  • Version: 1.7.0
  • Ruby version: 2.7.1
stas commented

Hey @kitsuneyo

Before we dig in to this, the set_key_transform :camel_lower doesn't affect the values, just the keys!

Another thing here is that this doesn't look like a jsonapi.rb issue, since the library barely calls the jsonapi-serializer based on the params it extracts from the request. In which case, I suggest you file an issue at https://github.com/jsonapi-serializer/jsonapi-serializer (here's the related test-case if you want to give it a try: https://github.com/jsonapi-serializer/jsonapi-serializer/blob/master/spec/integration/attributes_fields_spec.rb#L43 )

Second, the request in your example doesn't seem correct.

GET /person-aliases?fields[personAlias]=myField
                               ^- this should be the value you set with `set_type`, eg. `person_alias`
                                       

Consider this updated example:

GET /person-aliases?fields[person_alias]=myField

Alternatively, as the library doesn't have a lot of tests with custom key transformation support, I suggest to give it a try without changing the key transformation method and see if it works.

Let me know how did it go. Thanks!

Thanks for the quick reply.

Before we dig in to this, the set_key_transform :camel_lower doesn't affect the values, just the keys!

In the case of type, I promise you it does! If I comment out set_key_transform :camel_lower, then my type value personAlias becomes person_alias. But that is a jsonapi-serializer thing.

Consider this updated example: GET /person-aliases?fields[person_alias]=myField

It doesn't work, I had already tried it :(

Alternatively, as the library doesn't have a lot of tests with custom key transformation support, I suggest to give it a try without changing the key transformation method and see if it works.

It does work! I'll investigate more, thanks for the hints.

I think this is actually a duplicate of jsonapi-serializer/jsonapi-serializer#119.