Skip to content

Commit 9233196

Browse files
authored
fix(attributes): updated toJSON to use getRawAttributes (#198)
## Continuous Integration * ci: added git identification configuration ## Fix * fix(attributes): updated `toJSON` to use `getRawAttributes`
1 parent fa29d20 commit 9233196

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

.github/workflows/deploy-api-docs.yml

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jobs:
2020
run: npm run docs:api
2121
- name: Update gh-pages branch
2222
run: |
23+
git config --global user.email "nandor.kraszlan@gmail.com"
24+
git config --global user.name "Nandor Kraszlan"
2325
cp -r ./api-docs/* ./
2426
git add -f assets
2527
git add -f classes

src/Calliope/Concerns/HasAttributes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ export default class HasAttributes extends GuardsAttributes implements Jsonable,
686686
* @inheritDoc
687687
*/
688688
public toJSON<T extends ReturnType<typeof JSON.parse> = RawAttributes<this>>(): T {
689-
const json = this.getAttributes() as SimpleAttributes;
689+
const json = this.getRawAttributes() as SimpleAttributes;
690690

691691
const relations = (this as unknown as HasRelations).getRelations();
692692

@@ -696,7 +696,7 @@ export default class HasAttributes extends GuardsAttributes implements Jsonable,
696696
return;
697697
}
698698

699-
json[relation] = (relations[relation]! as ModelCollection<Model>).map(model => model.toJSON()).toArray();
699+
json[relation] = (relations[relation] as ModelCollection<Model>).map(model => model.toJSON()).toArray();
700700
});
701701

702702
return json as unknown as T;

tests/Calliope/Concerns/HasAttributes.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ModelCollection from '../../../src/Calliope/ModelCollection';
44
import { isEqual } from 'lodash';
55
import Contract from '../../mock/Models/Contract';
66
import Team from '../../mock/Models/Team';
7+
import type { RawAttributes } from '../../../src';
78
import { Collection } from '../../../src';
89

910
let hasAttributes: User;
@@ -748,6 +749,20 @@ describe('HasAttributes', () => {
748749
]
749750
});
750751
});
752+
753+
it('should take attributes in their raw form', () => {
754+
// raw because json isn't something that is displayed to the user
755+
const shift = Shift.create({ attr: 1 });
756+
757+
shift.getAttrAttribute = (value: number) => {
758+
return value + 1;
759+
};
760+
761+
hasAttributes.addRelation('shifts', shift);
762+
expect((hasAttributes.toJSON().shifts as [RawAttributes<Shift>])[0].attr).toBe(1);
763+
764+
delete shift.getAttrAttribute;
765+
});
751766
});
752767

753768
describe('toString()', () => {

tests/mock/Models/Shift.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ export default class Shift extends Model {
99
public factory(): ShiftFactory {
1010
return new ShiftFactory;
1111
}
12+
13+
public get fillable(): string[] {
14+
return ['*'];
15+
}
1216
}

0 commit comments

Comments
 (0)