Skip to content

Commit d1efdaf

Browse files
fix: return serialized null value as string (#435)
1 parent 5eeaeb7 commit d1efdaf

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Serializer {
9494
}
9595

9696
asIntegerNullable (i) {
97-
return i === null ? null : this.asInteger(i)
97+
return i === null ? 'null' : this.asInteger(i)
9898
}
9999

100100
asNumber (i) {
@@ -107,15 +107,15 @@ class Serializer {
107107
}
108108

109109
asNumberNullable (i) {
110-
return i === null ? null : this.asNumber(i)
110+
return i === null ? 'null' : this.asNumber(i)
111111
}
112112

113113
asBoolean (bool) {
114114
return bool && 'true' || 'false' // eslint-disable-line
115115
}
116116

117117
asBooleanNullable (bool) {
118-
return bool === null ? null : this.asBoolean(bool)
118+
return bool === null ? 'null' : this.asBoolean(bool)
119119
}
120120

121121
asDatetime (date, skipQuotes) {
@@ -176,7 +176,7 @@ class Serializer {
176176
}
177177

178178
asStringNullable (str) {
179-
return str === null ? null : this.asString(str)
179+
return str === null ? 'null' : this.asString(str)
180180
}
181181

182182
// magically escape strings for json

test/nullable.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,67 @@ Object.keys(testSet).forEach(key => {
116116
t.same(JSON.parse(result), expected)
117117
})
118118
})
119+
120+
test('handle nullable number correctly', (t) => {
121+
t.plan(2)
122+
123+
const schema = {
124+
type: 'number',
125+
nullable: true
126+
}
127+
const stringify = build(schema)
128+
129+
const data = null
130+
const result = stringify(data)
131+
132+
t.same(result, JSON.stringify(data))
133+
t.same(JSON.parse(result), data)
134+
})
135+
136+
test('handle nullable integer correctly', (t) => {
137+
t.plan(2)
138+
139+
const schema = {
140+
type: 'integer',
141+
nullable: true
142+
}
143+
const stringify = build(schema)
144+
145+
const data = null
146+
const result = stringify(data)
147+
148+
t.same(result, JSON.stringify(data))
149+
t.same(JSON.parse(result), data)
150+
})
151+
152+
test('handle nullable boolean correctly', (t) => {
153+
t.plan(2)
154+
155+
const schema = {
156+
type: 'boolean',
157+
nullable: true
158+
}
159+
const stringify = build(schema)
160+
161+
const data = null
162+
const result = stringify(data)
163+
164+
t.same(result, JSON.stringify(data))
165+
t.same(JSON.parse(result), data)
166+
})
167+
168+
test('handle nullable string correctly', (t) => {
169+
t.plan(2)
170+
171+
const schema = {
172+
type: 'string',
173+
nullable: true
174+
}
175+
const stringify = build(schema)
176+
177+
const data = null
178+
const result = stringify(data)
179+
180+
t.same(result, JSON.stringify(data))
181+
t.same(JSON.parse(result), data)
182+
})

0 commit comments

Comments
 (0)