Skip to content

Commit f9a2799

Browse files
authored
fix: prevent mutation of response headers schema (#871)
1 parent 48190ca commit f9a2799

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

lib/spec/openapi/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ function resolveResponse (fastifyResponseJson, produces, ref) {
344344
response.headers = {}
345345
Object.keys(rawJsonSchema.headers).forEach(function (key) {
346346
const header = {
347-
schema: rawJsonSchema.headers[key]
347+
schema: { ...rawJsonSchema.headers[key] }
348348
}
349349

350350
if (rawJsonSchema.headers[key].description) {

test/spec/openapi/schema.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,4 +1801,66 @@ test('support callbacks', async () => {
18011801

18021802
await Swagger.validate(openapiObject)
18031803
})
1804+
1805+
await test('should preserve original headers schema across multiple responses', async t => {
1806+
const headersSchema = {
1807+
'X-DESCRIPTION': {
1808+
type: 'string',
1809+
description: 'Foo',
1810+
},
1811+
}
1812+
1813+
const opt = {
1814+
schema: {
1815+
response: {
1816+
200: {
1817+
type: 'object',
1818+
properties: {
1819+
hello: {
1820+
type: 'string'
1821+
}
1822+
},
1823+
headers: headersSchema
1824+
},
1825+
201: {
1826+
type: 'object',
1827+
properties: {
1828+
hello: {
1829+
type: 'string'
1830+
}
1831+
},
1832+
headers: headersSchema
1833+
}
1834+
}
1835+
}
1836+
}
1837+
1838+
const fastify = Fastify()
1839+
await fastify.register(fastifySwagger, {
1840+
openapi: true
1841+
})
1842+
fastify.get('/', opt, () => {})
1843+
1844+
await fastify.ready()
1845+
1846+
const swaggerObject = fastify.swagger()
1847+
const api = await Swagger.validate(swaggerObject)
1848+
1849+
const definedPath = api.paths['/'].get
1850+
1851+
t.assert.deepStrictEqual(definedPath.responses['200'].headers['X-DESCRIPTION'], {
1852+
description: 'Foo',
1853+
schema: {
1854+
type: 'string'
1855+
}
1856+
})
1857+
t.assert.strictEqual(definedPath.responses['200'].content['application/json'].schema.headers, undefined)
1858+
t.assert.deepStrictEqual(definedPath.responses['201'].headers['X-DESCRIPTION'], {
1859+
description: 'Foo',
1860+
schema: {
1861+
type: 'string'
1862+
}
1863+
})
1864+
t.assert.strictEqual(definedPath.responses['201'].content['application/json'].schema.headers, undefined)
1865+
})
18041866
})

0 commit comments

Comments
 (0)