@fastify/swagger
Advanced tools
Comparing version 8.3.1 to 8.4.0
@@ -14,7 +14,31 @@ 'use strict' | ||
const globalExposeHeadRoutes = fastify.initialConfig.exposeHeadRoutes | ||
fastify.addHook('onRoute', (routeOptions) => { | ||
// we need to skip HEAD route | ||
// since it may automatically added by fastify | ||
// and will conflict with the existing route | ||
if (routeOptions.method === 'HEAD') return | ||
// fastify will generate HEAD routes if either the global `exposeHeadRoutes` | ||
// (default behavior since https://github.com/fastify/fastify/pull/2826) or | ||
// `exposeHeadRoute` on route level flag is `true`. If two routes with | ||
// operationId are added to the swagger object, it is no longer valid. | ||
// therefore we suffix the operationId with `-head`. | ||
const hasRouteExposeHeadRouteFlag = routeOptions.exposeHeadRoute != null | ||
const exposesHeads = hasRouteExposeHeadRouteFlag | ||
? routeOptions.exposeHeadRoute | ||
: globalExposeHeadRoutes | ||
if ( | ||
routeOptions.method === 'HEAD' && | ||
exposesHeads === true && | ||
routeOptions.schema !== undefined && | ||
routeOptions.schema.operationId !== undefined | ||
) { | ||
routes.push( | ||
Object.assign({}, routeOptions, { | ||
schema: Object.assign({}, routeOptions.schema, { | ||
operationId: `${routeOptions.schema.operationId}-head` | ||
}) | ||
}) | ||
) | ||
return | ||
} | ||
routes.push(routeOptions) | ||
@@ -21,0 +45,0 @@ }) |
{ | ||
"name": "@fastify/swagger", | ||
"version": "8.3.1", | ||
"version": "8.4.0", | ||
"description": "Serve Swagger/OpenAPI documentation for Fastify, supporting dynamic generation", | ||
@@ -44,3 +44,3 @@ "main": "index.js", | ||
"@fastify/pre-commit": "^2.0.2", | ||
"@types/node": "^18.0.0", | ||
"@types/node": "^20.1.0", | ||
"fastify": "^4.0.0", | ||
@@ -53,3 +53,3 @@ "fluent-json-schema": "^4.0.0", | ||
"tap": "^16.2.0", | ||
"tsd": "^0.25.0" | ||
"tsd": "^0.28.0" | ||
}, | ||
@@ -61,3 +61,3 @@ "dependencies": { | ||
"rfdc": "^1.3.0", | ||
"yaml": "^2.1.1" | ||
"yaml": "^2.2.2" | ||
}, | ||
@@ -64,0 +64,0 @@ "standard": { |
@@ -484,1 +484,76 @@ 'use strict' | ||
}) | ||
test('support "exposeHeadRoute" option', async (t) => { | ||
const fastify = Fastify({ exposeHeadRoutes: false }) | ||
await fastify.register(fastifySwagger, { | ||
routePrefix: '/docs', | ||
exposeRoute: true | ||
}) | ||
fastify.get('/with-head', { | ||
exposeHeadRoute: true, | ||
schema: { | ||
operationId: 'with-head', | ||
response: { | ||
200: { | ||
description: 'Expected Response', | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'string' } | ||
} | ||
} | ||
} | ||
} | ||
}, () => {}) | ||
await fastify.ready() | ||
const swaggerObject = fastify.swagger() | ||
const api = await Swagger.validate(swaggerObject) | ||
t.same( | ||
api.paths['/with-head'].get.responses['200'].description, | ||
'Expected Response' | ||
) | ||
t.same( | ||
api.paths['/with-head'].head.responses['200'].description, | ||
'Expected Response' | ||
) | ||
}) | ||
test('support "exposeHeadRoutes" option', async (t) => { | ||
const fastify = Fastify({ exposeHeadRoutes: true }) | ||
await fastify.register(fastifySwagger, { | ||
routePrefix: '/docs', | ||
exposeRoute: true | ||
}) | ||
fastify.get('/with-head', { | ||
schema: { | ||
operationId: 'with-head', | ||
response: { | ||
200: { | ||
description: 'Expected Response', | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'string' } | ||
} | ||
} | ||
} | ||
} | ||
}, () => {}) | ||
await fastify.ready() | ||
const swaggerObject = fastify.swagger() | ||
const api = await Swagger.validate(swaggerObject) | ||
t.same( | ||
api.paths['/with-head'].get.responses['200'].description, | ||
'Expected Response' | ||
) | ||
t.same( | ||
api.paths['/with-head'].head.responses['200'].description, | ||
'Expected Response' | ||
) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
241849
6916
Updatedyaml@^2.2.2