Skip to content

Commit

Permalink
Default pkg to empty object in case readPackageJson fails (#354)
Browse files Browse the repository at this point in the history
* Default pkg to empty object in case readPackageJson fails

* update readPackageJson to return an empty object on exception rather than calling done

* update readPackageJson usages and remove unnecessary references to done

* add test case
  • Loading branch information
agramian committed Feb 9, 2021
1 parent 13fb912 commit 8e840b0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ function prepareDefaultOptions (opts) {
}
}

function prepareOpenapiObject (opts, done) {
const pkg = readPackageJson(done)
function prepareOpenapiObject (opts) {
const pkg = readPackageJson()
const openapiObject = {
openapi: '3.0.3',
info: {
Expand Down
4 changes: 2 additions & 2 deletions lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ function prepareDefaultOptions (opts) {
}
}

function prepareSwaggerObject (opts, done) {
const pkg = readPackageJson(done)
function prepareSwaggerObject (opts) {
const pkg = readPackageJson()
const swaggerObject = {
swagger: '2.0',
info: {
Expand Down
4 changes: 2 additions & 2 deletions lib/util/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ function resolveLocalRef (jsonSchema, externalSchemas) {
return resolveLocalRef(externalSchemas[localRef], externalSchemas)
}

function readPackageJson (done) {
function readPackageJson () {
try {
return JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'package.json')))
} catch (err) {
return done(err)
return {}
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/mode/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Fastify = require('fastify')
const fastifySwagger = require('../../index')
const fastifySwaggerDynamic = require('../../lib/mode/dynamic')
const yaml = require('js-yaml')
const Swagger = require('swagger-parser')

const resolve = require('path').resolve
const readFileSync = require('fs').readFileSync
Expand Down Expand Up @@ -803,3 +804,42 @@ test('/documentation/uiConfig can be customize', t => {
t.strictEqual(res.payload, '{"docExpansion":"full"}')
})
})

test('should still return valid swagger object when missing package.json', t => {
const config = {
mode: 'dynamic',
specification: {
path: './examples/example-static-specification.json'
},
exposeRoute: true
}

t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, config)

const originalPathJoin = path.join
const testPackageJSON = path.join(__dirname, 'missing.json')

path.join = (...args) => {
if (args[3] === 'package.json') {
return testPackageJSON
}
return originalPathJoin(...args)
}

fastify.ready(err => {
t.error(err)

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

Swagger.validate(swaggerObject)
.then(function (api) {
t.pass('Swagger object is still valid.')
})
.catch(function (err) {
t.fail(err)
})
})
})

0 comments on commit 8e840b0

Please sign in to comment.