Skip to content

Commit

Permalink
fix(schemas): don't use schema to attribute mapping on singular array…
Browse files Browse the repository at this point in the history
… schemas (paularmstrong#387)

Co-authored-by: Paul Armstrong <[email protected]>
  • Loading branch information
fpipita and paularmstrong committed Jan 23, 2020
1 parent 5992d34 commit 3246f91
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/schemas/Polymorphic.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class PolymorphicSchema {
if (!this.isSingleSchema && !schemaKey) {
return value;
}
const id = isImmutable(value) ? value.get('id') : value.id;
const id = this.isSingleSchema ? undefined : isImmutable(value) ? value.get('id') : value.id;
const schema = this.isSingleSchema ? this.schema : this.schema[schemaKey];
return unvisit(id || value, schema);
}
Expand Down
12 changes: 12 additions & 0 deletions src/schemas/__tests__/Array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,17 @@ describe(`${schema.Array.name} denormalization`, () => {
expect(denormalize('123', taco, entities)).toMatchSnapshot();
expect(denormalize('123', taco, fromJS(entities))).toMatchSnapshot();
});

test('does not assume mapping of schema to attribute values when schemaAttribute is not set', () => {
const cats = new schema.Entity('cats');
const catRecord = new schema.Object({
cat: cats
});
const catList = new schema.Array(catRecord);
const input = [{ cat: { id: 1 }, id: 5 }, { cat: { id: 2 }, id: 6 }];
const output = normalize(input, catList);
expect(output).toMatchSnapshot();
expect(denormalize(output.result, catList, output.entities)).toEqual(input);
});
});
});
25 changes: 25 additions & 0 deletions src/schemas/__tests__/__snapshots__/Array.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ Array [
]
`;

exports[`ArraySchema denormalization Class does not assume mapping of schema to attribute values when schemaAttribute is not set 1`] = `
Object {
"entities": Object {
"cats": Object {
"1": Object {
"id": 1,
},
"2": Object {
"id": 2,
},
},
},
"result": Array [
Object {
"cat": 1,
"id": 5,
},
Object {
"cat": 2,
"id": 6,
},
],
}
`;

exports[`ArraySchema denormalization Class returns the input value if is not an array 1`] = `
Object {
"fillings": Object {},
Expand Down

0 comments on commit 3246f91

Please sign in to comment.