Skip to content

Commit 9ba18fa

Browse files
committed
fix: don't set attributes if it's partial
1 parent 4db73e0 commit 9ba18fa

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

dist/index.cjs.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class Parser {
119119
}
120120
const loadedElement = Parser.load(element, included);
121121
const model = $registeredModels.find(e => e.type === loadedElement.type);
122-
const instance = this.wrapWhenPartial(model, loadedElement);
122+
const instance = this.wrapWhenPartial(new (model?.klass || Model)(), loadedElement);
123123
this.resolved[uniqueKey] = instance;
124124
if (model && model.createFn) {
125125
return model.createFn(instance, loadedElement, relation => this.parse(relation, included));
@@ -128,15 +128,17 @@ class Parser {
128128
const relsData = $registeredRelationships.find(e => e.klass === model?.klass);
129129
instance.id = loadedElement.id;
130130
instance._type = loadedElement.type;
131+
if ('$_partial' in loadedElement) {
132+
return instance;
133+
}
131134
this.parseAttributes(instance, loadedElement, attrData);
132135
this.parseRelationships(instance, loadedElement, relsData, included);
133136
return instance;
134137
}
135-
wrapWhenPartial(model, loadedElement) {
138+
wrapWhenPartial(instance, loadedElement) {
136139
if (!loadedElement.$_partial) {
137-
return new (model?.klass || Model)();
140+
return instance;
138141
}
139-
const instance = new Model();
140142
return new Proxy(instance, {
141143
get: function (target, prop) {
142144
if (prop === "$_partial") {

dist/index.esm.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Parser {
115115
}
116116
const loadedElement = Parser.load(element, included);
117117
const model = $registeredModels.find(e => e.type === loadedElement.type);
118-
const instance = this.wrapWhenPartial(model, loadedElement);
118+
const instance = this.wrapWhenPartial(new (model?.klass || Model)(), loadedElement);
119119
this.resolved[uniqueKey] = instance;
120120
if (model && model.createFn) {
121121
return model.createFn(instance, loadedElement, relation => this.parse(relation, included));
@@ -124,15 +124,17 @@ class Parser {
124124
const relsData = $registeredRelationships.find(e => e.klass === model?.klass);
125125
instance.id = loadedElement.id;
126126
instance._type = loadedElement.type;
127+
if ('$_partial' in loadedElement) {
128+
return instance;
129+
}
127130
this.parseAttributes(instance, loadedElement, attrData);
128131
this.parseRelationships(instance, loadedElement, relsData, included);
129132
return instance;
130133
}
131-
wrapWhenPartial(model, loadedElement) {
134+
wrapWhenPartial(instance, loadedElement) {
132135
if (!loadedElement.$_partial) {
133-
return new (model?.klass || Model)();
136+
return instance;
134137
}
135-
const instance = new Model();
136138
return new Proxy(instance, {
137139
get: function (target, prop) {
138140
if (prop === "$_partial") {

dist/types/Parser.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { JSONData } from "./interfaces/JSONData";
22
import { JSONModel } from "./interfaces/JSONModel";
33
import { Model } from "./Model";
4-
import { RegisteredModel } from "./interfaces/RegisteredModel";
54
export declare class Parser {
65
private data;
76
private included;
@@ -11,7 +10,7 @@ export declare class Parser {
1110
private parse;
1211
parseList(list: JSONModel[], included: JSONModel[]): unknown[];
1312
parseElement<T>(element: JSONModel, included: JSONModel[]): T;
14-
wrapWhenPartial(model: RegisteredModel | undefined, loadedElement: JSONModel & {
13+
wrapWhenPartial(instance: Model, loadedElement: JSONModel & {
1514
$_partial?: boolean;
1615
}): Model;
1716
private parseRelationships;

src/Parser.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { RegisteredProperty } from "./interfaces/RegisteredProperty";
55
import { Model } from "./Model";
66
import { $registeredAttributes, $registeredModels, $registeredRelationships } from "./data";
77
import { debug } from "./utils";
8-
import { RegisteredModel } from "./interfaces/RegisteredModel";
98

109
export class Parser {
1110
readonly resolved: Record<string, Model> = {};
@@ -63,7 +62,7 @@ export class Parser {
6362
(e) => e.type === loadedElement.type
6463
);
6564

66-
const instance = this.wrapWhenPartial(model, loadedElement);
65+
const instance = this.wrapWhenPartial(new (model?.klass || Model)(), loadedElement);
6766
this.resolved[uniqueKey] = instance;
6867

6968
if (model && model.createFn) {
@@ -80,17 +79,19 @@ export class Parser {
8079
instance.id = loadedElement.id;
8180
instance._type = loadedElement.type;
8281

82+
if ('$_partial' in loadedElement) {
83+
return instance as T;
84+
}
8385
this.parseAttributes(instance, loadedElement, attrData);
8486
this.parseRelationships(instance, loadedElement, relsData, included);
8587

8688
return instance as T;
8789
}
8890

89-
wrapWhenPartial(model: RegisteredModel | undefined, loadedElement: JSONModel & { $_partial?: boolean }) {
91+
wrapWhenPartial(instance: Model, loadedElement: JSONModel & { $_partial?: boolean }) {
9092
if (!loadedElement.$_partial) {
91-
return new (model?.klass || Model)();
93+
return instance;
9294
}
93-
const instance = new Model();
9495
return new Proxy(
9596
instance,
9697
{

test/basic.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ test("logs undeclared attributes", () => {
115115
test("toJSON has a max depth", () => {
116116
const post = parsed[0];
117117
expect(JSON.stringify(post.toJSON(1))).toBe(`
118-
{"id":"2","name":"My post","ciao":null,"content":"ciao","createdAt":"2020-10-10T10:32:00.000Z","enabled":true,"author":{"id":"3","firstName":"Gino","lastName":"Pino","createdAt":"2020-10-15T10:32:00.000Z"},"reviewer":null,"relatedPost":{"id":"10","enabled":true}}
118+
{"id":"2","name":"My post","ciao":null,"content":"ciao","createdAt":"2020-10-10T10:32:00.000Z","enabled":true,"author":{"id":"3","firstName":"Gino","lastName":"Pino","createdAt":"2020-10-15T10:32:00.000Z"},"reviewer":null,"relatedPost":{"id":"10"}}
119119
`.trim());
120120
});
121121

0 commit comments

Comments
 (0)