Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit b6b1ae8

Browse files
committed
fix
1 parent 8575a1d commit b6b1ae8

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

lib/index.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ function serialize(resource, schema, options = {}) {
3434
}
3535

3636
function findObjectDef(propertyDef) {
37-
const { type, properties } = propertyDef;
37+
const { type, anyOf } = propertyDef;
3838

39-
if (type === 'object' && properties) {
39+
if (type === 'object') {
4040
return propertyDef;
4141
}
4242

4343
if (type === 'array') {
4444
return findObjectDef(propertyDef.items);
4545
}
4646

47+
if (anyOf && isNullableType(propertyDef)) {
48+
return anyOf.map(findObjectDef).find(def => def && def.type === 'object');
49+
}
50+
4751
return null;
4852
}
4953

@@ -58,4 +62,13 @@ function toJSON(value) {
5862
return value && typeof value.toJSON === 'function' ? value.toJSON() : value;
5963
}
6064

65+
function isNullType(schema = {}) {
66+
return schema.type === 'null';
67+
}
68+
69+
function isNullableType(schema = {}) {
70+
const { anyOf = [] } = schema;
71+
return Boolean(anyOf.length === 2 && anyOf.find(isNullType) && anyOf.find(item => !isNullType(item)));
72+
}
73+
6174
module.exports = serialize;

tests/all.test.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,25 @@ describe('Serializers', () => {
6969
type: 'string',
7070
},
7171
modelsB: {
72-
type: 'array',
73-
items: {
74-
type: 'object',
75-
properties: {
76-
b: {
77-
type: 'integer',
78-
},
79-
c: {
80-
type: 'boolean',
72+
anyOf: [
73+
{
74+
type: 'array',
75+
items: {
76+
type: 'object',
77+
properties: {
78+
b: {
79+
type: 'integer',
80+
},
81+
c: {
82+
type: 'boolean',
83+
},
84+
},
8185
},
8286
},
83-
},
87+
{
88+
type: 'null',
89+
},
90+
],
8491
},
8592
},
8693
};

0 commit comments

Comments
 (0)