Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 1f0801f

Browse files
committed
resolves #241
1 parent 57ccb02 commit 1f0801f

File tree

4 files changed

+158
-65
lines changed

4 files changed

+158
-65
lines changed

src/core/@model/json.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe('@Json', () => {
2525
@Json('anp') @Json('anotherProperty')
2626
anotherProperty: string;
2727

28+
@Json()
2829
aThirdProperty: number = 777;
2930

3031
aFourthProperty: string;

src/core/@model/model-operators/to-json.spec.ts

Lines changed: 115 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -63,38 +63,36 @@ describe('Model.toJson', () => {
6363
it('transforms a defined property to the designated fieldName in the output of toJson', () => {
6464

6565
class Address {
66-
@Db('st')
66+
@Db('st') @Json()
6767
street = '1600 Pennsylvania Ave NW';
6868

6969
@Db('c')
7070
@Json('cy')
7171
city = 'Washington';
7272

73+
@Json()
7374
state = 'DC';
7475

7576
@Json('z')
7677
zipCode = '20500';
7778
}
7879

7980
class Contact {
80-
@Db('ph')
81+
@Db('ph') @Json()
8182
phone = '123-123-1234';
8283

83-
@Db({field: 'a', model: Address})
84-
@Json('addr')
84+
@Db({field: 'a', model: Address}) @Json('addr')
8585
address = new Address();
8686
}
8787

8888
@Model({})
8989
class User extends SapiModelMixin() {
90-
@Db('fn')
91-
@Json('fn')
90+
@Db('fn') @Json('fn')
9291
firstName = 'George';
93-
@Db('ln')
94-
@Json('ln')
92+
@Db('ln') @Json('ln')
9593
lastName = 'Washington';
9694

97-
@Db({field: 'c', model: Contact})
95+
@Db({field: 'c', model: Contact}) @Json()
9896
contact = new Contact();
9997
}
10098

@@ -110,11 +108,11 @@ describe('Model.toJson', () => {
110108
};
111109

112110
const user = User.fromDb(db);
113-
const json = (user.toJson() as any);
111+
const json = user.toJson();
114112

115113
expect(json.fn).toBe(db.fn);
116114
expect(json.ln).toBe(db.ln);
117-
expect(json.contact).toBeDefined('A property not decorated with @Json should still be marshalled to Json');
115+
expect(json.contact).toBeDefined();
118116
expect(json.contact.phone).toBe(db.c.ph);
119117
expect(json.contact.addr).toBeDefined('A deeply nested property should be marshalled to Json');
120118
expect(json.contact.addr.street).toBe(db.c.a.st);
@@ -123,18 +121,18 @@ describe('Model.toJson', () => {
123121
expect(json.contact.addr.z).toBe(user.contact.address.zipCode);
124122
});
125123

126-
it('properties are marshalled when not decorated with @Json properties', () => {
124+
it('properties are NOT marshalled when not decorated with @Json properties', () => {
127125

128126
class Contact {
129127
static test() {
130-
// methods should be marshalled to the resulting json object
128+
// methods should not be marshalled to the resulting json object
131129
}
132130

133131
phone = 123;
134132
address = '123 Main St.';
135133

136134
test() {
137-
// methods should be marshalled to the resulting json object
135+
// methods should not be marshalled to the resulting json object
138136
}
139137
}
140138

@@ -146,16 +144,11 @@ describe('Model.toJson', () => {
146144
}
147145

148146
const user = new User();
149-
const json = (user.toJson() as any);
150-
151-
expect(json.firstName).toBe(user.firstName);
152-
expect(json.lastname).toBeUndefined('properties without assigned values and no default values do not actually' +
153-
' exist in the resulting transpiled js output, so they cannot be marshalled to json');
154-
expect(json.contact).toBeDefined('A property defining a child object should be included in the resulting json');
155-
expect(json.contact.phone).toBe(user.contact.phone);
156-
expect(json.contact.address).toBe(user.contact.address);
147+
const json = user.toJson();
157148

158-
expect(json.contact.test).toBeUndefined('instance methods should not be included in the resulting json');
149+
expect(json.firstName).toBeUndefined();
150+
expect(json.lastname).toBeUndefined();
151+
expect(json.contact).toBeUndefined();
159152
});
160153

161154
it('does not return _id', () => {
@@ -353,14 +346,12 @@ describe('Model.toJson', () => {
353346
@Db({private: true})
354347
state = 'DC';
355348

356-
@Db({private: true})
357-
@Json('t')
349+
@Db({private: true}) @Json('t')
358350
test = 'test';
359351
}
360352

361353
class Contact {
362-
@Db({field: 'a', model: Address})
363-
@Json('addr')
354+
@Db({field: 'a', model: Address}) @Json('addr')
364355
address = new Address();
365356
}
366357

@@ -373,7 +364,7 @@ describe('Model.toJson', () => {
373364
@Json('ln')
374365
lastName = 'Washington';
375366

376-
@Db({field: 'c', model: Contact})
367+
@Db({field: 'c', model: Contact}) @Json()
377368
contact = new Contact();
378369

379370
@Db({private: true, model: Contact})
@@ -442,7 +433,7 @@ describe('Model.toJson', () => {
442433

443434
});
444435

445-
it('falls back to using property names (no context) when an invalid context is passed in', () => {
436+
it('leaves out fields when an invalid context is passed in', () => {
446437
@Model()
447438
class TestContext extends SapiModelMixin() {
448439
@Json('fn')
@@ -455,8 +446,8 @@ describe('Model.toJson', () => {
455446
const testContext = new TestContext();
456447
const result = testContext.toJson('non-existent');
457448

458-
expect(result.firstName).toBe(testContext.firstName);
459-
expect(result.lastName).toBe(testContext.lastName);
449+
expect(result.firstName).toBeUndefined();
450+
expect(result.lastName).toBeUndefined();
460451
});
461452

462453
it('supports multiple contexts', () => {
@@ -752,12 +743,12 @@ describe('Model.toJson', () => {
752743

753744
const result1 = tcc1.toJson({test: true});
754745
expect(result1.aField.test).toBeTruthy();
755-
expect(result1.bField).toBe('');
746+
expect(result1.bField).toBeUndefined();
756747

757748
const tcc2 = TestComplexContext.fromJson({});
758749

759750
const result2 = tcc2.toJson({context: 'B', data: 'isB'});
760-
expect(result2.aField).toBe('');
751+
expect(result2.aField).toBeUndefined();
761752
expect(result2.bField).toBe('isB');
762753

763754
});
@@ -780,8 +771,11 @@ describe('Model.toJson', () => {
780771
@Id() @Json({type: 'id'})
781772
id: ObjectID;
782773

774+
@Json()
783775
fieldA = 'a';
776+
@Json()
784777
fieldB = 'b';
778+
@Json()
785779
field1 = 1;
786780

787781
}
@@ -843,8 +837,11 @@ describe('Model.toJson', () => {
843837
@Model()
844838
class TestSubDoc {
845839

840+
@Json()
846841
subA = 'a';
842+
@Json()
847843
subB = 'b';
844+
@Json()
848845
field2 = 2;
849846

850847
}
@@ -855,11 +852,14 @@ describe('Model.toJson', () => {
855852
@Id() @Json({type: 'id'})
856853
id: ObjectID;
857854

855+
@Json()
858856
fieldA = 'a';
857+
@Json()
859858
fieldB = 'b';
859+
@Json()
860860
field1 = 1;
861861

862-
@Db({model: TestSubDoc})
862+
@Db({model: TestSubDoc}) @Json()
863863
subDoc: TestSubDoc = new TestSubDoc();
864864

865865
}
@@ -966,10 +966,13 @@ describe('Model.toJson', () => {
966966
@Model()
967967
class TestSubDoc {
968968

969+
@Json()
969970
subA = 'a';
971+
@Json()
970972
subB = 'b';
973+
@Json()
971974
field2 = 2;
972-
975+
@Json()
973976
test = ['1', '2'];
974977

975978
}
@@ -980,11 +983,14 @@ describe('Model.toJson', () => {
980983
@Id() @Json({type: 'id'})
981984
id: ObjectID;
982985

986+
@Json()
983987
fieldA = 'a';
988+
@Json()
984989
fieldB = 'b';
990+
@Json()
985991
field1 = 1;
986992

987-
@Db({model: TestSubDoc})
993+
@Db({model: TestSubDoc}) @Json()
988994
subDoc: TestSubDoc[] = [new TestSubDoc(), new TestSubDoc()];
989995

990996
}
@@ -1096,6 +1102,79 @@ describe('Model.toJson', () => {
10961102
});
10971103
});
10981104
});
1105+
1106+
it('works with context issue #241', () => {
1107+
1108+
@Model()
1109+
class TestProjection1 extends SapiModelMixin() {
1110+
@Id() @Json({type: 'id', context: '*'})
1111+
id: ObjectID = new ObjectID();
1112+
1113+
@Json({context: 'test'})
1114+
fieldA = 'a';
1115+
1116+
@Json({context: 'test2'})
1117+
fieldB = 'b';
1118+
}
1119+
1120+
const model = new TestProjection1();
1121+
const json = model.toJson({
1122+
context: 'test',
1123+
projection: {
1124+
fieldA: 1,
1125+
id: 1
1126+
}
1127+
});
1128+
1129+
expect(json.id).toEqual(model.id);
1130+
expect(json.fieldA).toBe(model.fieldA);
1131+
expect(json.fieldB).toBeUndefined();
1132+
1133+
const json2 = model.toJson({
1134+
context: 'test2',
1135+
projection: {
1136+
fieldB: 1,
1137+
id: 1
1138+
}
1139+
});
1140+
1141+
expect(json2.id).toEqual(model.id);
1142+
expect(json2.fieldA).toBeUndefined();
1143+
expect(json2.fieldB).toBe(model.fieldB);
1144+
1145+
});
1146+
1147+
it('projection does not override context', () => {
1148+
1149+
@Model()
1150+
class TestProjection1 extends SapiModelMixin() {
1151+
@Id() @Json({type: 'id', context: '*'})
1152+
id: ObjectID = new ObjectID();
1153+
1154+
@Json({context: 'test'})
1155+
fieldA = 'a';
1156+
1157+
@Json({context: 'test2'})
1158+
fieldB = 'b';
1159+
1160+
fieldC = 'c';
1161+
}
1162+
1163+
const model = new TestProjection1();
1164+
const json2 = model.toJson({
1165+
context: 'test2',
1166+
projection: {
1167+
fieldB: 1,
1168+
id: 1
1169+
}
1170+
});
1171+
1172+
expect(json2.id).toEqual(model.id);
1173+
expect(json2.fieldA).toBeUndefined();
1174+
expect(json2.fieldB).toBe(model.fieldB);
1175+
expect(json2.fieldC).toBeUndefined();
1176+
1177+
});
10991178
});
11001179

11011180
describe('hierarchical toJson calls', () => {

0 commit comments

Comments
 (0)