Skip to content

Commit

Permalink
fastify#462 Allow nested $ref to resolve for OpenAPI
Browse files Browse the repository at this point in the history
 implementation
  • Loading branch information
briceruzand committed Sep 20, 2021
1 parent b7a8814 commit c4dd960
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/spec/openapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ module.exports = function (opts, cache, routes, Ref, done) {
openapiObject.components.schemas = prepareOpenapiSchemas({
...openapiObject.components.schemas,
...(ref.definitions().definitions)
},ref)
}, 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
Expand Down
4 changes: 1 addition & 3 deletions test/spec/openapi/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ test('support nested $ref schema', (t) => {
t.equal(typeof openapiObject, 'object')
t.match(Object.keys(openapiObject.components.schemas), ['def-0', 'def-1'])

// OrderItem ref is not prefixed by '#/components/schemas/'
// OrderItem ref must be prefixed by '#/components/schemas/'
t.equal(openapiObject.components.schemas['def-1'].properties.products.items.$ref, '#/components/schemas/def-0')

// open api validation failed
// ENOENT: no such file or directory ${cwd}/OrderItem
Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
Expand Down

0 comments on commit c4dd960

Please sign in to comment.