Skip to content

Commit b34003a

Browse files
committed
Allow a one-to-one relationship to be an ID (string) or an object (which has ID in it) - fix Equipment Model test
1 parent 67725a6 commit b34003a

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

src/utils/JsonSerializer.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,22 @@ export class JsonApiSerializer {
7070
} else {
7171
const value = (model as any)[relationship.name];
7272
if (value) {
73-
payload.data.relationships![relationship.name] = {
74-
data: {
75-
type: relationship.modelType,
76-
id: value.id,
77-
},
78-
};
73+
// value could be a string (ID) or an object
74+
if (typeof value === 'string') {
75+
payload.data.relationships![relationship.name] = {
76+
data: {
77+
type: relationship.modelType,
78+
id: value,
79+
},
80+
};
81+
} else {
82+
payload.data.relationships![relationship.name] = {
83+
data: {
84+
type: relationship.modelType,
85+
id: value.id,
86+
},
87+
};
88+
}
7989
}
8090
}
8191
});
@@ -135,12 +145,22 @@ export class JsonApiSerializer {
135145
} else {
136146
const value = (model as any)[relationship.name];
137147
if (value) {
138-
payload.data.relationships![relationship.name] = {
139-
data: {
140-
type: relationship.modelType,
141-
id: value.id,
142-
},
143-
};
148+
// value could be a string (ID) or an object
149+
if (typeof value === 'string') {
150+
payload.data.relationships![relationship.name] = {
151+
data: {
152+
type: relationship.modelType,
153+
id: value,
154+
},
155+
};
156+
} else {
157+
payload.data.relationships![relationship.name] = {
158+
data: {
159+
type: relationship.modelType,
160+
id: value.id,
161+
},
162+
};
163+
}
144164
}
145165
}
146166
});
@@ -190,4 +210,4 @@ export class JsonApiSerializer {
190210
},
191211
};
192212
}
193-
}
213+
}

tests/utils/JsonSerializer.test.ts renamed to tests/utils/JsonSerializer/EquipmentAndEquipmentModel.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { describe, it, expect } from "bun:test";
22
import { JsonApiSerializer } from "@utils/JsonSerializer";
33
import { Equipment } from "@models/Equipment";
4-
import { FormCategory } from '../../src';
5-
import { Hydrator } from '../../src/utils/Hydrator';
4+
import { FormCategory } from '@models/FormCategory';
5+
import { Hydrator } from '@utils/Hydrator'
66

7-
describe('JsonApiSerializer', () => {
7+
describe('JsonApiSerializer for Equipment and Model', () => {
88

99
const hydrator = new Hydrator();
1010

0 commit comments

Comments
 (0)