diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js index 9bf81f3f..19d01150 100644 --- a/lib/spec/openapi/utils.js +++ b/lib/spec/openapi/utils.js @@ -62,6 +62,7 @@ function prepareOpenapiObject (opts) { if (opts.info) openapiObject.info = opts.info if (opts.servers) openapiObject.servers = opts.servers if (opts.components) openapiObject.components = Object.assign({}, opts.components, { schemas: Object.assign({}, opts.components.schemas) }) + if (opts.paths) openapiObject.paths = opts.paths if (opts.webhooks) openapiObject.webhooks = opts.webhooks if (opts.security) openapiObject.security = opts.security if (opts.tags) openapiObject.tags = opts.tags diff --git a/lib/spec/swagger/index.js b/lib/spec/swagger/index.js index c35803fb..cb45f378 100644 --- a/lib/spec/swagger/index.js +++ b/lib/spec/swagger/index.js @@ -24,7 +24,6 @@ module.exports = function (opts, cache, routes, Ref, done) { ...(ref.definitions().definitions) }, ref) - swaggerObject.paths = {} for (const route of routes) { const transformResult = route.config?.swaggerTransform !== undefined ? route.config.swaggerTransform diff --git a/lib/spec/swagger/utils.js b/lib/spec/swagger/utils.js index b043bb02..18719137 100644 --- a/lib/spec/swagger/utils.js +++ b/lib/spec/swagger/utils.js @@ -15,6 +15,7 @@ function prepareDefaultOptions (opts) { const consumes = swagger.consumes || null const produces = swagger.produces || null const definitions = swagger.definitions || null + const paths = swagger.paths || null const basePath = swagger.basePath || null const securityDefinitions = swagger.securityDefinitions || null const security = swagger.security || null @@ -40,6 +41,7 @@ function prepareDefaultOptions (opts) { consumes, produces, definitions, + paths, basePath, securityDefinitions, security, @@ -73,6 +75,7 @@ function prepareSwaggerObject (opts) { if (opts.consumes) swaggerObject.consumes = opts.consumes if (opts.produces) swaggerObject.produces = opts.produces if (opts.definitions) swaggerObject.definitions = opts.definitions + if (opts.paths) swaggerObject.paths = opts.paths if (opts.securityDefinitions) swaggerObject.securityDefinitions = opts.securityDefinitions if (opts.security) swaggerObject.security = opts.security if (opts.tags) swaggerObject.tags = opts.tags diff --git a/test/spec/openapi/option.js b/test/spec/openapi/option.js index b9a6357e..03d182cb 100644 --- a/test/spec/openapi/option.js +++ b/test/spec/openapi/option.js @@ -111,6 +111,56 @@ test('openapi components', async (t) => { delete openapiOption.openapi.components.schemas // remove what we just added }) +test('openapi paths', async (t) => { + t.plan(1) + const fastify = Fastify() + + openapiOption.openapi.paths = { + '/status': { + get: { + description: 'Status route, so we can check if server is alive', + tags: [ + 'Status' + ], + responses: { + 200: { + description: 'Server is alive', + content: { + 'application/json': { + schema: { + type: 'object', + properties: { + health: { + type: 'boolean' + }, + date: { + type: 'string' + } + }, + example: { + health: true, + date: '2018-02-19T15:36:46.758Z' + } + } + } + } + } + } + } + } + } + + await fastify.register(fastifySwagger, openapiOption) + + fastify.get('/status', () => {}) + + await fastify.ready() + + const openapiObject = fastify.swagger() + t.same(openapiObject.paths, openapiOption.openapi.paths) + delete openapiOption.openapi.paths // remove what we just added +}) + test('hide support when property set in transform() - property', async (t) => { t.plan(1) const fastify = Fastify() diff --git a/test/spec/swagger/option.js b/test/spec/swagger/option.js index be5cd120..f09a0c65 100644 --- a/test/spec/swagger/option.js +++ b/test/spec/swagger/option.js @@ -99,6 +99,54 @@ test('swagger definitions', async (t) => { delete swaggerOption.swagger.definitions // remove what we just added }) +test('swagger paths', async (t) => { + t.plan(1) + const fastify = Fastify() + + swaggerOption.swagger.paths = { + '/status': { + get: { + description: 'Status route, so we can check if server is alive', + tags: [ + 'Status' + ], + responses: { + 200: { + description: 'Server is alive', + content: { + 'application/json': { + schema: { + type: 'object', + properties: { + health: { + type: 'boolean' + }, + date: { + type: 'string' + } + }, + example: { + health: true, + date: '2018-02-19T15:36:46.758Z' + } + } + } + } + } + } + } + } + } + + await fastify.register(fastifySwagger, swaggerOption) + + await fastify.ready() + + const swaggerObject = fastify.swagger() + t.same(swaggerObject.paths, swaggerOption.swagger.paths) + delete swaggerOption.swagger.paths // remove what we just added +}) + test('swagger tags', async (t) => { t.plan(1) const fastify = Fastify()