Skip to content

hoistData Won't Hoist With Additional Properties #1131

@thenhawke

Description

@thenhawke

If there is an additional property in the data, the hoist doesn't seem to work.

This works from the example:

const result = kc.deserialise({
    data: [
        {
            id: '1',
            type: 'users',
            attributes: {
                name: 'Josh'
            },
            relationships: {
                waifu: {
                    data: {
                        id: '1',
                        type: 'characters'
                    }
                }
            }
        }
    ],
    included: [
        {
            id: '1',
            type: 'characters',
            attributes: {
                name: 'Genkai'
            }
        }
    ],
}, { hoistData: true })

Outputs:

[
  {
    "id": "1",
    "type": "users",
    "waifu": {
      "id": "1",
      "type": "characters",
      "name": "Genkai"
    },
    "name": "Josh"
  }
]

However, if there are any other properties on the object, it doesn't hoist. For example, my API framework puts "jsonapi": { "version": "1.0" } in the results. This prevents the hoist. Also empty "meta" and "links" objects will prevent the hoist as well, keeping the nested "data" objects:

const result = kc.deserialise({
    data: [
        {
            id: '1',
            type: 'users',
            attributes: {
                name: 'Josh'
            },
            relationships: {
                waifu: {
                    data: {
                        id: '1',
                        type: 'characters'
                    },
                    meta: {},
                    links: {}
                }
            }
        }
    ],
    included: [
        {
            id: '1',
            type: 'characters',
            attributes: {
                name: 'Genkai'
            }
        }
    ],
    "jsonapi": {
        "version": "1.0"
    }
}, { hoistData: true })

Outputs:

{
  "data": [
    {
      "id": "1",
      "type": "users",
      "waifu": {
        "data": {
          "id": "1",
          "type": "characters",
          "name": "Genkai"
        },
        "links": {},
        "meta": {}
      },
      "name": "Josh"
    }
  ],
  "jsonapi": {
    "version": "1.0"
  }
}

The docs say that if hoistData is "enabled, the contents of the data property will be hoisted to the parent. This provides a flatter response object, see examples below for transformation examples. Links and Meta properties will be merged into the parent object if they exist." However this doesn't always seem to be the case.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions