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

Commit

Permalink
resolves #177
Browse files Browse the repository at this point in the history
  • Loading branch information
etsuo committed May 14, 2018
1 parent 8dcb801 commit f2b8a51
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 26 deletions.
42 changes: 41 additions & 1 deletion src/core/@model/from-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Json } from './json';
import { Model } from './model';
import { SapiModelMixin } from './sapi-model-mixin';

describe('@FormatFromJson', () => {
describe('@FromJson', () => {

it('passes through model', () => {

Expand Down Expand Up @@ -148,4 +148,44 @@ describe('@FormatFromJson', () => {
expect(result1.prop1).toBe('1');
expect(result1.prop2).toBe('2');
});

it('is called on the document and sub documents', () => {
pending('see issue #194');

let parentToJsonCalled = false;
let childToJsonCalled = false;

@Model()
class TestChild extends SapiModelMixin() {
@Json()
child = 'child';

@FromJson()
childToJson() {
childToJsonCalled = true;
}
}

@Model()
class TestParent extends SapiModelMixin() {

@Json()
parent = 'parent';

@Json({model: TestChild})
child = new TestChild();

@FromJson()
parentToJson() {
parentToJsonCalled = true;
}

}

TestParent.fromJson({});

expect(parentToJsonCalled).toBeTruthy('parent @ToJson not called');
expect(childToJsonCalled).toBeTruthy('child @ToJson not called');

});
});
12 changes: 3 additions & 9 deletions src/core/@model/model-operators/from-json.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { ObjectID } from 'mongodb';
import { shouldRecurse } from '../../lib';
import {
dbSymbols,
IDbOptions
} from '../db';
import { dbSymbols, IDbOptions } from '../db';
import { formatFromJsonSymbols, FromJsonHandler } from '../from-json';
import {
IJsonOptions,
jsonSymbols
} from '../json';
import { IJsonOptions, jsonSymbols } from '../json';
import { debug } from './index';

/**
Expand Down Expand Up @@ -117,7 +111,7 @@ export function fromJson<T = any>(json: T, context = 'default'): any {
}
}

// @json({formatFromJson})
// @json({fromJson})
if (meta.formatFromJson) {
value = meta.formatFromJson(value, key);
}
Expand Down
30 changes: 14 additions & 16 deletions src/core/@model/model-operators/to-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,12 @@ export function toJson(context: string | IContext = 'default'): { [key: string]:
const modelName = (this.constructor || {} as any).name;
debug.normal(`.toJson called, target '${modelName}'`);

let jsonObj = mapModelToJson(ctx, this, ctx.projection);

// @ToJson
const formatToJson = Reflect.getMetadata(formatToJsonSymbols.functionMap, this);
if (formatToJson) {
const formatters: ToJsonHandler[] = [
...formatToJson.get(ctx.context) || [],
...formatToJson.get('*') || []
];
for (const formatter of formatters) {
jsonObj = formatter.call(this, jsonObj, this, ctx);
}
}

return jsonObj;
return mapModelToJson(ctx, this, ctx.projection);
}

function mapModelToJson(ctx: IContext, source, projection: IProjection) {

const jsonObj = {};
let jsonObj = {};

if (!source) {
return source;
Expand Down Expand Up @@ -148,6 +134,18 @@ function mapModelToJson(ctx: IContext, source, projection: IProjection) {

}

// @ToJson
const formatToJson = Reflect.getMetadata(formatToJsonSymbols.functionMap, source);
if (formatToJson) {
const formatters: ToJsonHandler[] = [
...formatToJson.get(ctx.context) || [],
...formatToJson.get('*') || []
];
for (const formatter of formatters) {
jsonObj = formatter.call(source, jsonObj, source, ctx);
}
}

return jsonObj;
}

Expand Down
39 changes: 39 additions & 0 deletions src/core/@model/to-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,43 @@ describe('@ToJson', () => {
SomeModel.fromJson({}).toJson();
expect(thisVal instanceof SomeModel).toBeTruthy();
});

it('is called on the document and sub documents', () => {

let parentToJsonCalled = false;
let childToJsonCalled = false;

@Model()
class TestChild extends SapiModelMixin() {
@Json()
child = 'child';

@ToJson()
childToJson() {
childToJsonCalled = true;
}
}

@Model()
class TestParent extends SapiModelMixin() {

@Json()
parent = 'parent';

@Json({model: TestChild})
child = new TestChild();

@ToJson()
parentToJson() {
parentToJsonCalled = true;
}

}

(new TestParent()).toJson();

expect(parentToJsonCalled).toBeTruthy('parent @ToJson not called');
expect(childToJsonCalled).toBeTruthy('child @ToJson not called');

});
});

0 comments on commit f2b8a51

Please sign in to comment.