From 458712b97065f6354627ed679e3ab3daae3f2d10 Mon Sep 17 00:00:00 2001 From: Elie Rotenberg Date: Mon, 28 Feb 2022 19:33:40 +0100 Subject: [PATCH 1/2] Support ref relative pointers in openapi params * Add test for failing use case * Implement fix Fix #553. --- lib/spec/openapi/utils.js | 3 ++- test/spec/openapi/refs.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js index 0a5a8980..8d98897e 100644 --- a/lib/spec/openapi/utils.js +++ b/lib/spec/openapi/utils.js @@ -228,7 +228,8 @@ function resolveCommonParams (container, parameters, schema, ref, sharedSchemas, // if the resolved definition is in global schema if (resolved.$ref && resolved.$ref.startsWith(schemasPath)) { const parts = resolved.$ref.split(schemasPath) - resolved = ref.definitions().definitions[parts[1]] + const pathParts = parts[1].split("/") + resolved = pathParts.reduce((resolved, pathPart) => resolved[pathPart], ref.definitions().definitions) } const arr = plainJsonObjectToOpenapi3(container, resolved, sharedSchemas, securityIgnores) diff --git a/test/spec/openapi/refs.js b/test/spec/openapi/refs.js index d62a2652..03125c67 100644 --- a/test/spec/openapi/refs.js +++ b/test/spec/openapi/refs.js @@ -32,6 +32,37 @@ test('support $ref schema', async (t) => { await Swagger.validate(openapiObject) }) +test("support $ref relative pointers in params", async (t) => { + const fastify = Fastify() + + fastify.register(fastifySwagger, openapiOption) + fastify.register(async (instance) => { + instance.addSchema({ + $id: 'Order', + type: 'object', + properties: { + OrderId: { + type: 'object', + properties: { + id: { + type: 'string' + } + } + } + } + }) + instance.get('/:id', { schema: { params: { $ref: "Order#/properties/OrderId" }, response: { 200: { $ref: 'Order#' } } } }, () => {}) + }) + + await fastify.ready() + + const openapiObject = fastify.swagger() + t.equal(typeof openapiObject, 'object') + t.match(Object.keys(openapiObject.components.schemas), ['Order']) + + await Swagger.validate(openapiObject) +}) + test('support nested $ref schema : simple test', async (t) => { const fastify = Fastify() fastify.register(fastifySwagger, openapiOption) From c29418628ebb020844470927c07c10cd062ea61c Mon Sep 17 00:00:00 2001 From: Elie Rotenberg Date: Mon, 28 Feb 2022 21:24:31 +0100 Subject: [PATCH 2/2] Fix lint --- lib/spec/openapi/utils.js | 2 +- test/spec/openapi/refs.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js index 8d98897e..6cc05ecb 100644 --- a/lib/spec/openapi/utils.js +++ b/lib/spec/openapi/utils.js @@ -228,7 +228,7 @@ function resolveCommonParams (container, parameters, schema, ref, sharedSchemas, // if the resolved definition is in global schema if (resolved.$ref && resolved.$ref.startsWith(schemasPath)) { const parts = resolved.$ref.split(schemasPath) - const pathParts = parts[1].split("/") + const pathParts = parts[1].split('/') resolved = pathParts.reduce((resolved, pathPart) => resolved[pathPart], ref.definitions().definitions) } diff --git a/test/spec/openapi/refs.js b/test/spec/openapi/refs.js index 03125c67..113474c5 100644 --- a/test/spec/openapi/refs.js +++ b/test/spec/openapi/refs.js @@ -32,7 +32,7 @@ test('support $ref schema', async (t) => { await Swagger.validate(openapiObject) }) -test("support $ref relative pointers in params", async (t) => { +test('support $ref relative pointers in params', async (t) => { const fastify = Fastify() fastify.register(fastifySwagger, openapiOption) @@ -51,7 +51,7 @@ test("support $ref relative pointers in params", async (t) => { } } }) - instance.get('/:id', { schema: { params: { $ref: "Order#/properties/OrderId" }, response: { 200: { $ref: 'Order#' } } } }, () => {}) + instance.get('/:id', { schema: { params: { $ref: 'Order#/properties/OrderId' }, response: { 200: { $ref: 'Order#' } } } }, () => {}) }) await fastify.ready()