Skip to content

Commit 46a6d1f

Browse files
authored
fix: getSchemaSerializer contentType check (fastify#4531)
1 parent a4c5f5f commit 46a6d1f

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lib/error-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function fallbackErrorHandler (error, reply, cb) {
9696
const statusCode = reply.statusCode
9797
let payload
9898
try {
99-
const serializerFn = getSchemaSerializer(reply[kRouteContext], statusCode)
99+
const serializerFn = getSchemaSerializer(reply[kRouteContext], statusCode, reply[kReplyHeaders]['content-type'])
100100
payload = (serializerFn === false)
101101
? serializeError({
102102
error: statusCodes[statusCode + ''],

lib/schemas.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function getSchemaSerializer (context, statusCode, contentType) {
160160
return false
161161
}
162162
if (responseSchemaDef[statusCode]) {
163-
if (responseSchemaDef[statusCode].constructor === Object) {
163+
if (responseSchemaDef[statusCode].constructor === Object && contentType) {
164164
const mediaName = contentType.split(';')[0]
165165
if (responseSchemaDef[statusCode][mediaName]) {
166166
return responseSchemaDef[statusCode][mediaName]
@@ -172,7 +172,7 @@ function getSchemaSerializer (context, statusCode, contentType) {
172172
}
173173
const fallbackStatusCode = (statusCode + '')[0] + 'xx'
174174
if (responseSchemaDef[fallbackStatusCode]) {
175-
if (responseSchemaDef[fallbackStatusCode].constructor === Object) {
175+
if (responseSchemaDef[fallbackStatusCode].constructor === Object && contentType) {
176176
const mediaName = contentType.split(';')[0]
177177
if (responseSchemaDef[fallbackStatusCode][mediaName]) {
178178
return responseSchemaDef[fallbackStatusCode][mediaName]
@@ -184,7 +184,7 @@ function getSchemaSerializer (context, statusCode, contentType) {
184184
return responseSchemaDef[fallbackStatusCode]
185185
}
186186
if (responseSchemaDef.default) {
187-
if (responseSchemaDef.default.constructor === Object) {
187+
if (responseSchemaDef.default.constructor === Object && contentType) {
188188
const mediaName = contentType.split(';')[0]
189189
if (responseSchemaDef.default[mediaName]) {
190190
return responseSchemaDef.default[mediaName]

test/hooks.test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const fs = require('fs')
1010
const split = require('split2')
1111
const symbols = require('../lib/symbols.js')
1212
const payload = { hello: 'world' }
13+
const proxyquire = require('proxyquire')
1314

1415
process.removeAllListeners('warning')
1516

@@ -1269,7 +1270,14 @@ test('clear payload', t => {
12691270
})
12701271

12711272
test('onSend hook throws', t => {
1272-
t.plan(9)
1273+
t.plan(11)
1274+
const Fastify = proxyquire('..', {
1275+
'./lib/schemas.js': {
1276+
getSchemaSerializer: (param1, param2, param3) => {
1277+
t.equal(param3, 'application/json; charset=utf-8', 'param3 should be "application/json; charset=utf-8"')
1278+
}
1279+
}
1280+
})
12731281
const fastify = Fastify()
12741282
fastify.addHook('onSend', function (request, reply, payload, done) {
12751283
if (request.raw.method === 'DELETE') {
@@ -1281,13 +1289,37 @@ test('onSend hook throws', t => {
12811289
throw new Error('some error')
12821290
}
12831291

1292+
if (request.raw.method === 'POST') {
1293+
throw new Error('some error')
1294+
}
1295+
12841296
done()
12851297
})
12861298

12871299
fastify.get('/', (req, reply) => {
12881300
reply.send({ hello: 'world' })
12891301
})
12901302

1303+
fastify.post('/', {
1304+
schema: {
1305+
response: {
1306+
200: {
1307+
content: {
1308+
'application/json': {
1309+
schema: {
1310+
name: { type: 'string' },
1311+
image: { type: 'string' },
1312+
address: { type: 'string' }
1313+
}
1314+
}
1315+
}
1316+
}
1317+
}
1318+
}
1319+
}, (req, reply) => {
1320+
reply.send({ hello: 'world' })
1321+
})
1322+
12911323
fastify.delete('/', (req, reply) => {
12921324
reply.send({ hello: 'world' })
12931325
})
@@ -1309,6 +1341,13 @@ test('onSend hook throws', t => {
13091341
t.equal(response.headers['content-length'], '' + body.length)
13101342
t.same(JSON.parse(body), { hello: 'world' })
13111343
})
1344+
sget({
1345+
method: 'POST',
1346+
url: 'http://localhost:' + fastify.server.address().port
1347+
}, (err, response, body) => {
1348+
t.error(err)
1349+
t.equal(response.statusCode, 500)
1350+
})
13121351
sget({
13131352
method: 'DELETE',
13141353
url: 'http://localhost:' + fastify.server.address().port

0 commit comments

Comments
 (0)