From 50cd25e64f748c240813d29002896d7a6f05aa63 Mon Sep 17 00:00:00 2001 From: Shook Date: Thu, 11 Jul 2024 23:29:27 +0800 Subject: [PATCH 1/2] refactor: migrate swagger to use openapi@3.1.0, fixing the oneOf/anyOf rendering issue in the docs --- src/plugins/swagger.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/plugins/swagger.ts b/src/plugins/swagger.ts index 1900fd48..536651b2 100644 --- a/src/plugins/swagger.ts +++ b/src/plugins/swagger.ts @@ -11,36 +11,36 @@ export const DOCS_ROUTE_PREFIX = '/docs'; export default fp(async (fastify) => { fastify.register(swagger, { hideUntagged: true, - swagger: { + openapi: { + openapi: '3.1.0', info: { title: 'Bitcoin/RGB++ Assets API', version: pkg.version, }, - consumes: ['application/json'], - produces: ['application/json'], security: [{ apiKey: [] }], - securityDefinitions: { - apiKey: { - type: 'apiKey', - name: 'Authorization', - in: 'header', - description: 'JWT token for authentication. Example: Bearer ', + components: { + securitySchemes: { + apiKey: { + type: 'apiKey', + name: 'Authorization', + in: 'header', + description: 'JWT token for authentication. Example: Bearer ', + }, }, }, }, transform: jsonSchemaTransform, - transformObject: ({ swaggerObject }) => { + transformObject: ({ openapiObject }) => { if (env.NODE_ENV === 'production') { - const { paths = {} } = swaggerObject; - const newPaths = Object.entries(paths).reduce((acc, [path, methods]) => { + const { paths = {} } = openapiObject; + openapiObject.paths = Object.entries(paths).reduce((acc, [path, methods]) => { if (SWAGGER_PROD_IGNORE_URLS.some((ignorePath) => path.startsWith(ignorePath))) { return acc; } return { ...acc, [path]: methods }; }, {}); - swaggerObject.paths = newPaths; } - return swaggerObject; + return openapiObject; }, }); fastify.register(swaggerUI, { From 10d699e4dc0b3c61d479feab819fbd7b6780698a Mon Sep 17 00:00:00 2001 From: Shook Date: Fri, 12 Jul 2024 00:13:11 +0800 Subject: [PATCH 2/2] test: update openapi version check --- test/app.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/app.test.ts b/test/app.test.ts index 91bb1099..19770efe 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -12,7 +12,7 @@ test('`/docs/json` - 200', async () => { const data = response.json(); expect(response.statusCode).toBe(200); - expect(data.swagger).toBe('2.0'); + expect(data.openapi).toBe('3.1.0'); expect(Object.keys(data.paths)).toStrictEqual([ '/token/generate', '/bitcoin/v1/info',