Skip to content

Commit 39a9e27

Browse files
author
Michael Dostál
committed
fix: sending null values when attributes were not provided from backend
fix: jsonmodelconverter providing model instead of array
1 parent e4b33d8 commit 39a9e27

File tree

7 files changed

+46
-16
lines changed

7 files changed

+46
-16
lines changed

package-lock.json

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular2-jsonapi",
3-
"version": "6.0.5-beta",
3+
"version": "6.0.6-beta",
44
"description": "A lightweight Angular 2 adapter for JSON API",
55
"scripts": {
66
"build": "rimraf dist src/compiled && tslint src/**/*.ts && ngc",

src/converters/json-model/json-model.converter.spec.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('JsonModel converter', () => {
88

99
describe('ArrayModel - simple values', () => {
1010
beforeEach(() => {
11-
converter = new JsonModelConverter(Array);
11+
converter = new JsonModelConverter(Array, { hasMany: true });
1212
});
1313

1414
it('should return empty array when empty input', () => {
@@ -34,10 +34,23 @@ describe('JsonModel converter', () => {
3434
});
3535
});
3636

37+
describe('ArrayModel - simple values - nullable', () => {
38+
beforeEach(() => {
39+
converter = new JsonModelConverter(Array, { hasMany: true, nullValue: true });
40+
});
41+
42+
it('should throw error when provided empty array when empty input', () => {
43+
const VALUE = null;
44+
expect(() => {
45+
converter.mask(VALUE);
46+
}).toThrow(new Error('ERROR: JsonModelConverter: Expected array but got ' + typeof VALUE + '.'));
47+
});
48+
});
49+
3750
describe('Array model -> object values', () => {
3851

3952
beforeEach(() => {
40-
converter = new JsonModelConverter(School);
53+
converter = new JsonModelConverter(School, { hasMany: true });
4154
});
4255

4356
it('should return array of Schools from provided data', () => {
@@ -83,7 +96,7 @@ describe('JsonModel converter', () => {
8396

8497
describe('ObjectModel - nullable', () => {
8598
beforeEach(() => {
86-
converter = new JsonModelConverter(School, false);
99+
converter = new JsonModelConverter(School, { nullValue: false });
87100
});
88101

89102
it('should return null when null', () => {
@@ -96,7 +109,7 @@ describe('JsonModel converter', () => {
96109
describe('unmask method', () => {
97110
describe('ArrayModel - simple values', () => {
98111
beforeEach(() => {
99-
converter = new JsonModelConverter(Array);
112+
converter = new JsonModelConverter(Array, { hasMany: true });
100113
});
101114

102115
it('should return serialized output when provided null', () => {
@@ -120,7 +133,7 @@ describe('JsonModel converter', () => {
120133
describe('Array model -> object values', () => {
121134

122135
beforeEach(() => {
123-
converter = new JsonModelConverter(School);
136+
converter = new JsonModelConverter(School, { hasMany: true });
124137
});
125138

126139
it('should return serialized Schools from provided Array of Schools', () => {
@@ -129,7 +142,7 @@ describe('JsonModel converter', () => {
129142
{ name: 'Charles University', students: 51438, foundation: '1348-04-07' },
130143
];
131144
const schools: Array<School> = [new School(DATA[0]), new School(DATA[1])];
132-
const result:Array<any> = converter.unmask(schools);
145+
const result: Array<any> = converter.unmask(schools);
133146
expect(result.length).toBe(2);
134147
for (const key in result) {
135148
expect(result[key].name).toBe(DATA[key].name);
@@ -141,7 +154,7 @@ describe('JsonModel converter', () => {
141154

142155
describe('ObjectModel - nullable', () => {
143156
beforeEach(() => {
144-
converter = new JsonModelConverter(School, false);
157+
converter = new JsonModelConverter(School, { nullValue: false });
145158
});
146159

147160
it('should return null when null', () => {

src/converters/json-model/json-model.converter.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
import { JsonApiNestedModel, PropertyConverter } from '../..';
2+
import { JsonModelConverterConfig } from '../../interfaces/json-model-converter-config.interface';
3+
4+
export const DEFAULT_OPTIONS: JsonModelConverterConfig = {
5+
nullValue: false,
6+
hasMany: false
7+
};
28

39
export class JsonModelConverter<T> implements PropertyConverter {
410
private modelType: any; // ModelType<T>
11+
private options: JsonModelConverterConfig;
512

6-
constructor(model: T, public nullValue: boolean = true) {
13+
constructor(model: T, options: JsonModelConverterConfig = {}) {
714
this.modelType = model; // <ModelType<T>>model
15+
this.options = { ...DEFAULT_OPTIONS, ...options };
816
}
917

10-
mask(value: any): T {
11-
if (!value && !this.nullValue) {
18+
mask(value: any): T | Array<T> {
19+
if (!value && !this.options.nullValue) {
20+
if (this.options.hasMany) {
21+
return [];
22+
}
1223
return new this.modelType();
1324
}
1425

1526
let result = null;
16-
if (Array.isArray(value)) {
27+
if (this.options.hasMany) {
28+
if (!Array.isArray(value)) {
29+
throw new Error('ERROR: JsonModelConverter: Expected array but got ' + typeof value + '.');
30+
}
1731
result = [];
1832
for (const item of value) {
1933
if (item === null) {

src/decorators/attribute.decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function Attribute(options: AttributeDecoratorOptions = {}): PropertyDeco
7272
const setter = function (newVal: any) {
7373
const targetType = Reflect.getMetadata('design:type', target, propertyName);
7474
const convertedValue = converter(targetType, newVal);
75-
let oldValue = undefined;
75+
let oldValue = null;
7676
if (this.isModelInitialization() && this.id) {
7777
oldValue = converter(targetType, newVal);
7878
} else {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export * from './models/error-response.model';
1616
export * from './models/json-api-query-data';
1717

1818
export * from './interfaces/overrides.interface';
19+
export * from './interfaces/json-model-converter-config.interface';
1920
export * from './interfaces/datastore-config.interface';
2021
export * from './interfaces/model-config.interface';
2122
export * from './interfaces/attribute-decorator-options.interface';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface JsonModelConverterConfig {
2+
nullValue?: boolean;
3+
hasMany?: boolean;
4+
}

0 commit comments

Comments
 (0)