Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BelongsToReference.meta() sometimes not populated properly #7039

Closed
Herriau opened this issue Feb 11, 2020 · 2 comments · Fixed by #7534
Closed

BelongsToReference.meta() sometimes not populated properly #7039

Herriau opened this issue Feb 11, 2020 · 2 comments · Fixed by #7534
Assignees
Labels
🏷️ bug This PR primarily fixes a reported issue Needs Bug Verification

Comments

@Herriau
Copy link

Herriau commented Feb 11, 2020

Description

When an optional belongsTo relationship is loaded independently from the model it's defined on, e.g. when invoking await someRecord.belongsTo('someRel').load(), then the meta hash is only accessible on BelongsToReference object when Adapter.findBelongsTo() resolves to a hash that contains a non-null value on its data key.

Say I have a record loaded through store.findRecord('some-model', <some-id>), intially hydrated from the following JSONAPI document:

GET /some-model/<some-id>

{
  data: {
    "type": "some-model",
    "id": <some-id>,
    "attributes": { ... },
    "relationships": {
      "some-rel": {
        "links": {
          "related": "/some-model/<some-id>/some-rel"
        }
      }
    }
  }
}

Then let's say I load that one relationship with await someRecord.belongsTo('someRel').load(), If the request / response that resulted from that call look like:

GET /some-model/<some-id>/some-rel

{
  "data": {
    "type": "some-other-model",
    "id": <some-other-id>,
    "attributes": { ... },
    ...
  },
  "meta": {
    "foo": "bar",
  }
}

then someRecord.belongsTo('someRel').meta() will return { foo: "bar" }, which is correct. However if the request / response that resulted from that call look like:

GET /some-model/<some-id>/some-rel

{
  "data": null,
  "meta": {
    "foo": "bar",
  }
}

then someRecord.belongsTo('someRel').meta() will return null, when I would have expected it to return { foo: "bar" } as well.

Reproduction

models/car.ts

export default class Car extends Model {
  @belongsTo('navigation-system')
  navigationSystem: NavigationSystem | null;
}

models/navigation-system.ts

export default class NavigationSystem extends Model {}

adapters/car.ts

export default class CarAdapter extends ApplicationAdapter {
  async findAll(store: any, type: any): Promise<any> {
    return {
      data: [
        {
          type: 'cars',
          id: 1,
          attributes: {},
          relationships: {
            navigationSystem: {
              links: {
                related: '/car/1/navigation-system',
              }
            }
          }
        },
        {
          type: 'cars',
          id: 2,
          attributes: {},
          relationships: {
            navigationSystem: {
              links: {
                related: '/car/2/navigation-system',
              }
            }
          }
        }
      ]
    };
  }

  async findBelongsTo(store: any, snapshot: any): Promise<any> {
    switch (snapshot.id) {
      case 1:
        return {
          data: { type: 'navigation-systems', id: 10, attributes: {} },
          meta: { foo: 'bar' },
        };

      case 2:
        return {
          data: null,
          meta: { foo: 'bar' },
        };
    }
  }
}

test.ts

const [car1, car2] = (await store.findAll('car')).toArray();

car1.belongsTo('navigationSystem').meta(); // { foo: 'bar' }
car2.belongsTo('navigationSystem').meta(); // null, expected { foo: 'bar' }

Versions

└─ [email protected]
└─ [email protected]
├─ @ember-data/[email protected]
├─ @ember-data/[email protected]
├─ @ember-data/[email protected]
├─ @ember-data/[email protected]
├─ @ember-data/[email protected]
├─ @ember-data/[email protected]
├─ @ember-data/[email protected]
├─ @ember-data/[email protected]
├─ @ember-data/[email protected]
├─ @types/[email protected]
├─ [email protected]
└─ [email protected]
@Herriau
Copy link
Author

Herriau commented Feb 11, 2020

This originally came up when investigating a public way of dealing with this other issue: #5629

@runspired
Copy link
Contributor

A PR with a test for this would be awesome, I suspect the issue is either that our code which ensures that the response of findBelongsTo/findHasMany is a complete payload is insufficient or that due to the missing data/included property it never gets threaded through to the relationship. In either case the fix shouldn't be terribly hard and I'd be happy to pair on a fix.

sly7-7 added a commit to sly7-7/data that referenced this issue May 21, 2021
runspired added a commit that referenced this issue May 26, 2021
…empty or does not include the data key (#7534)

* add test for #7039

* cleanup test and fix bug

Co-authored-by: Chris Thoburn <[email protected]>
@runspired runspired added 🏷️ bug This PR primarily fixes a reported issue and removed Bug labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bug This PR primarily fixes a reported issue Needs Bug Verification
Projects
None yet
2 participants