{"action":"created","issue":{"url":"https://api.github.com/repos/paularmstrong/normalizr/issues/261","repository_url":"https://api.github.com/repos/paularmstrong/normalizr","labels_url":"https://api.github.com/repos/paularmstrong/normalizr/issues/261/labels{/name}","comments_url":"https://api.github.com/repos/paularmstrong/normalizr/issues/261/comments","events_url":"https://api.github.com/repos/paularmstrong/normalizr/issues/261/events","html_url":"https://github.com/paularmstrong/normalizr/pull/261","id":228472751,"number":261,"title":"Use processedEntity to get idAttribute","user":{"login":"Stenerson","id":1191049,"avatar_url":"https://avatars0.githubusercontent.com/u/1191049?v=3","gravatar_id":"","url":"https://api.github.com/users/Stenerson","html_url":"https://github.com/Stenerson","followers_url":"https://api.github.com/users/Stenerson/followers","following_url":"https://api.github.com/users/Stenerson/following{/other_user}","gists_url":"https://api.github.com/users/Stenerson/gists{/gist_id}","starred_url":"https://api.github.com/users/Stenerson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Stenerson/subscriptions","organizations_url":"https://api.github.com/users/Stenerson/orgs","repos_url":"https://api.github.com/users/Stenerson/repos","events_url":"https://api.github.com/users/Stenerson/events{/privacy}","received_events_url":"https://api.github.com/users/Stenerson/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2017-05-13T13:16:46Z","updated_at":"2017-06-15T14:28:43Z","closed_at":"2017-05-17T14:53:51Z","pull_request":{"url":"https://api.github.com/repos/paularmstrong/normalizr/pulls/261","html_url":"https://github.com/paularmstrong/normalizr/pull/261","diff_url":"https://github.com/paularmstrong/normalizr/pull/261.diff","patch_url":"https://github.com/paularmstrong/normalizr/pull/261.patch"},"body":"# Problem\r\n\r\nChanges made to the object in `processStrategy` are not available to the `idAttribute` unless the value is mutated in `processStrategy`.\r\n\r\nSee discussion in #258 \r\n\r\n# Solution\r\n\r\nChange source to use `processedEntity` to get the ID, rather than the non-normalized value. This allows us to get the ID using a string rather than a function without mutating the input value.\r\n\r\n## Alternative Solution\r\n\r\nDon't mutate the input value and pass a function to `idAttirbute` that returns the computed ID **value**.\r\n\r\n# TODO\r\n\r\n- [x] Add & update tests\r\n- [ ] Ensure CI is passing (lint, tests, flow) **Requires discussion**\r\n- [x] Update relevant documentation **none?**\r\n\r\n# Issues\r\nThere is [one test](https://github.com/paularmstrong/normalizr/blob/master/src/__tests__/index.test.js#L108) that's failing due to this change. Since **this is a breaking change**, I thought we could discuss the possible solutions here; before I changed the intent of the library/tests.\r\n\r\n```js\r\n  it('uses the non-normalized input when getting the ID for an entity', () => {\r\n    const userEntity = new schema.Entity('users');\r\n    const idAttributeFn = jest.fn((nonNormalized, parent, key) => nonNormalized.user.id);\r\n    const recommendation = new schema.Entity('recommendations', { user: userEntity }, {\r\n      idAttribute: idAttributeFn\r\n    });\r\n    expect(normalize({ user: { id: '456' } }, recommendation)).toMatchSnapshot();\r\n    expect(idAttributeFn.mock.calls).toMatchSnapshot();\r\n    expect(recommendation.idAttribute).toBe(idAttributeFn);\r\n  });\r\n```\r\n\r\nThis change goes pretty much against the intent of this test since it's clearly now passing the partially normalized output to the `getId` function:\r\n\r\n```js\r\n  // Entity.js:51\r\n  normalize(input, parent, key, visit, addEntity) {\r\n    const processedEntity = this._processStrategy(input, parent, key);\r\n    Object.keys(this.schema).forEach((key) => {\r\n      if (processedEntity.hasOwnProperty(key) && typeof processedEntity[key] === 'object') {\r\n        const schema = this.schema[key];\r\n        // HERE, we're normalizing child schemas before passing to getId\r\n        processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity);\r\n      }\r\n    });\r\n\r\n    addEntity(this, processedEntity, input, parent, key);\r\n    // Here's the only change I made, passing processedEntity, vs input\r\n    return this.getId(processedEntity, parent, key);\r\n  }\r\n```\r\n\r\nI see a couple options to move forward. Please let me know which one you'd prefer @paularmstrong\r\n- If you want to move forward with this, we can discuss options en route to a breaking change (also [see additional discussion here](https://github.com/paularmstrong/normalizr/issues/258#issuecomment-300592753))\r\n- Close this, in favor of simply updating the doc --> #260 \r\n"},"comment":{"url":"https://api.github.com/repos/paularmstrong/normalizr/issues/comments/308750563","html_url":"https://github.com/paularmstrong/normalizr/pull/261#issuecomment-308750563","issue_url":"https://api.github.com/repos/paularmstrong/normalizr/issues/261","id":308750563,"user":{"login":"Stenerson","id":1191049,"avatar_url":"https://avatars0.githubusercontent.com/u/1191049?v=3","gravatar_id":"","url":"https://api.github.com/users/Stenerson","html_url":"https://github.com/Stenerson","followers_url":"https://api.github.com/users/Stenerson/followers","following_url":"https://api.github.com/users/Stenerson/following{/other_user}","gists_url":"https://api.github.com/users/Stenerson/gists{/gist_id}","starred_url":"https://api.github.com/users/Stenerson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Stenerson/subscriptions","organizations_url":"https://api.github.com/users/Stenerson/orgs","repos_url":"https://api.github.com/users/Stenerson/repos","events_url":"https://api.github.com/users/Stenerson/events{/privacy}","received_events_url":"https://api.github.com/users/Stenerson/received_events","type":"User","site_admin":false},"created_at":"2017-06-15T14:28:43Z","updated_at":"2017-06-15T14:28:43Z","body":"I could see this being an issue if your client-generated ID is not derived in any way from the data. Is it possible to derive the ID from the data? **If you can** use the data to generate your ID, [this solution](https://github.com/paularmstrong/normalizr/issues/258#issuecomment-300268601) may work for you."}}
{"actor":{"display_login":"Stenerson"}}