Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nested swagger ref #479

Merged
merged 2 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions lib/spec/openapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ module.exports = function (opts, cache, routes, Ref, done) {
...(ref.definitions().definitions)
}, ref)

// Swagger doesn't accept $id on /definitions schemas.
// The $ids are needed by Ref() to check the URI so we need
// to remove them at the end of the process
// definitions are added by resolve but they are replace by components.schemas
Object.values(openapiObject.components.schemas)
.forEach((_) => {
delete _.$id
delete _.definitions
})

for (const route of routes) {
const schema = defOpts.transform
? defOpts.transform(route.schema)
Expand Down
14 changes: 10 additions & 4 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,16 @@ function prepareOpenapiSchemas (schemas, ref) {
.reduce((res, [name, schema]) => {
const _ = { ...schema }
const resolved = transformDefsToComponents(ref.resolve(_, { externalSchemas: [schemas] }))
return {
...res,
[name]: resolved
}

// Swagger doesn't accept $id on /definitions schemas.
// The $ids are needed by Ref() to check the URI so we need
// to remove them at the end of the process
// definitions are added by resolve but they are replace by components.schemas
delete resolved.$id
delete resolved.definitions

res[name] = resolved
return res
}, {})
}

Expand Down
12 changes: 3 additions & 9 deletions lib/spec/swagger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const yaml = require('js-yaml')
const { shouldRouteHide } = require('../../util/common')
const { prepareDefaultOptions, prepareSwaggerObject, prepareSwaggerMethod, normalizeUrl } = require('./utils')
const { prepareDefaultOptions, prepareSwaggerObject, prepareSwaggerMethod, normalizeUrl, prepareSwaggerDefinitions } = require('./utils')

module.exports = function (opts, cache, routes, Ref, done) {
let ref
Expand All @@ -19,16 +19,10 @@ module.exports = function (opts, cache, routes, Ref, done) {
const swaggerObject = prepareSwaggerObject(defOpts, done)

ref = Ref()
swaggerObject.definitions = {
swaggerObject.definitions = prepareSwaggerDefinitions({
...swaggerObject.definitions,
...(ref.definitions().definitions)
}

// Swagger doesn't accept $id on /definitions schemas.
// The $ids are needed by Ref() to check the URI so we need
// to remove them at the end of the process
Object.values(swaggerObject.definitions)
.forEach(_ => { delete _.$id })
}, ref)

swaggerObject.paths = {}
for (const route of routes) {
Expand Down
20 changes: 19 additions & 1 deletion lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,27 @@ function prepareSwaggerMethod (schema, ref, swaggerObject) {
return swaggerMethod
}

function prepareSwaggerDefinitions (definitions, ref) {
return Object.entries(definitions)
.reduce((res, [name, definition]) => {
const _ = { ...definition }
const resolved = ref.resolve(_, { externalSchemas: [definitions] })

// Swagger doesn't accept $id on /definitions schemas.
// The $ids are needed by Ref() to check the URI so we need
// to remove them at the end of the process
delete resolved.$id
delete resolved.definitions

res[name] = resolved
return res
}, {})
}

module.exports = {
prepareDefaultOptions,
prepareSwaggerObject,
prepareSwaggerMethod,
normalizeUrl
normalizeUrl,
prepareSwaggerDefinitions
}
28 changes: 28 additions & 0 deletions test/spec/openapi/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,31 @@ test('support $ref in response schema', async (t) => {

await Swagger.validate(openapiObject)
})

test('support nested $ref schema : complex case without modifying buildLocalReference', async (t) => {
const fastify = Fastify()
fastify.register(fastifySwagger, { openapi: {} })
fastify.register(async (instance) => {
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
instance.addSchema({ $id: 'schemaB', type: 'object', properties: { id: { type: 'string' } } })
instance.addSchema({ $id: 'schemaC', type: 'object', properties: { a: { type: 'array', items: { $ref: 'schemaA' } } } })
instance.addSchema({ $id: 'schemaD', type: 'object', properties: { b: { $ref: 'schemaB' }, c: { $ref: 'schemaC' } } })
instance.post('/url1', { schema: { body: { $ref: 'schemaD' }, response: { 200: { $ref: 'schemaB' } } } }, () => {})
instance.post('/url2', { schema: { body: { $ref: 'schemaC' }, response: { 200: { $ref: 'schemaA' } } } }, () => {})
})

await fastify.ready()

const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')

const schemas = openapiObject.components.schemas
t.match(Object.keys(schemas), ['def-0', 'def-1', 'def-2', 'def-3'])

// ref must be prefixed by '#/components/schemas/'
t.equal(schemas['def-2'].properties.a.items.$ref, '#/components/schemas/def-0')
t.equal(schemas['def-3'].properties.b.$ref, '#/components/schemas/def-1')
t.equal(schemas['def-3'].properties.c.$ref, '#/components/schemas/def-2')

await Swagger.validate(openapiObject)
})
66 changes: 66 additions & 0 deletions test/spec/swagger/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,69 @@ test('support $ref schema', async t => {
await Swagger.validate(res.json())
t.pass('valid swagger object')
})

test('support nested $ref schema : complex case', async (t) => {
const options = {
swagger: {},
refResolver: {
buildLocalReference: (json, baseUri, fragment, i) => {
return json.$id || `def-${i}`
}
}
}
const fastify = Fastify()
fastify.register(fastifySwagger, options)
fastify.register(async (instance) => {
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
instance.addSchema({ $id: 'schemaB', type: 'object', properties: { id: { type: 'string' } } })
instance.addSchema({ $id: 'schemaC', type: 'object', properties: { a: { type: 'array', items: { $ref: 'schemaA' } } } })
instance.addSchema({ $id: 'schemaD', type: 'object', properties: { b: { $ref: 'schemaB' }, c: { $ref: 'schemaC' } } })
instance.post('/url1', { schema: { body: { $ref: 'schemaD' }, response: { 200: { $ref: 'schemaB' } } } }, () => {})
instance.post('/url2', { schema: { body: { $ref: 'schemaC' }, response: { 200: { $ref: 'schemaA' } } } }, () => {})
})

await fastify.ready()

const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
const definitions = swaggerObject.definitions
t.match(Object.keys(definitions), ['schemaA', 'schemaB', 'schemaC', 'schemaD'])

// ref must be prefixed by '#/definitions/'
t.equal(definitions.schemaC.properties.a.items.$ref, '#/definitions/schemaA')
t.equal(definitions.schemaD.properties.b.$ref, '#/definitions/schemaB')
t.equal(definitions.schemaD.properties.c.$ref, '#/definitions/schemaC')

await Swagger.validate(swaggerObject)
})

test('support nested $ref schema : complex case without modifying buildLocalReference', async (t) => {
const fastify = Fastify()
fastify.register(fastifySwagger, {
routePrefix: '/docs',
exposeRoute: true
})
fastify.register(async (instance) => {
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
instance.addSchema({ $id: 'schemaB', type: 'object', properties: { id: { type: 'string' } } })
instance.addSchema({ $id: 'schemaC', type: 'object', properties: { a: { type: 'array', items: { $ref: 'schemaA' } } } })
instance.addSchema({ $id: 'schemaD', type: 'object', properties: { b: { $ref: 'schemaB' }, c: { $ref: 'schemaC' } } })
instance.post('/url1', { schema: { body: { $ref: 'schemaD' }, response: { 200: { $ref: 'schemaB' } } } }, () => {})
instance.post('/url2', { schema: { body: { $ref: 'schemaC' }, response: { 200: { $ref: 'schemaA' } } } }, () => {})
})

await fastify.ready()

const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')

const definitions = swaggerObject.definitions
t.match(Object.keys(definitions), ['def-0', 'def-1', 'def-2', 'def-3'])

// ref must be prefixed by '#/definitions/'
t.equal(definitions['def-2'].properties.a.items.$ref, '#/definitions/def-0')
t.equal(definitions['def-3'].properties.b.$ref, '#/definitions/def-1')
t.equal(definitions['def-3'].properties.c.$ref, '#/definitions/def-2')

await Swagger.validate(swaggerObject)
})