Skip to content

Commit 3246f91

Browse files
fix(schemas): don't use schema to attribute mapping on singular array schemas (paularmstrong#387)
Co-authored-by: Paul Armstrong <[email protected]>
1 parent 5992d34 commit 3246f91

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/schemas/Polymorphic.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default class PolymorphicSchema {
4545
if (!this.isSingleSchema && !schemaKey) {
4646
return value;
4747
}
48-
const id = isImmutable(value) ? value.get('id') : value.id;
48+
const id = this.isSingleSchema ? undefined : isImmutable(value) ? value.get('id') : value.id;
4949
const schema = this.isSingleSchema ? this.schema : this.schema[schemaKey];
5050
return unvisit(id || value, schema);
5151
}

src/schemas/__tests__/Array.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,17 @@ describe(`${schema.Array.name} denormalization`, () => {
192192
expect(denormalize('123', taco, entities)).toMatchSnapshot();
193193
expect(denormalize('123', taco, fromJS(entities))).toMatchSnapshot();
194194
});
195+
196+
test('does not assume mapping of schema to attribute values when schemaAttribute is not set', () => {
197+
const cats = new schema.Entity('cats');
198+
const catRecord = new schema.Object({
199+
cat: cats
200+
});
201+
const catList = new schema.Array(catRecord);
202+
const input = [{ cat: { id: 1 }, id: 5 }, { cat: { id: 2 }, id: 6 }];
203+
const output = normalize(input, catList);
204+
expect(output).toMatchSnapshot();
205+
expect(denormalize(output.result, catList, output.entities)).toEqual(input);
206+
});
195207
});
196208
});

src/schemas/__tests__/__snapshots__/Array.test.js.snap

+25
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ Array [
6666
]
6767
`;
6868

69+
exports[`ArraySchema denormalization Class does not assume mapping of schema to attribute values when schemaAttribute is not set 1`] = `
70+
Object {
71+
"entities": Object {
72+
"cats": Object {
73+
"1": Object {
74+
"id": 1,
75+
},
76+
"2": Object {
77+
"id": 2,
78+
},
79+
},
80+
},
81+
"result": Array [
82+
Object {
83+
"cat": 1,
84+
"id": 5,
85+
},
86+
Object {
87+
"cat": 2,
88+
"id": 6,
89+
},
90+
],
91+
}
92+
`;
93+
6994
exports[`ArraySchema denormalization Class returns the input value if is not an array 1`] = `
7095
Object {
7196
"fillings": Object {},

0 commit comments

Comments
 (0)