From 8e840b0236a2d869e3eff4ee45f0d42076dd2fd3 Mon Sep 17 00:00:00 2001 From: Abtin Gramian Date: Tue, 9 Feb 2021 02:38:12 -0800 Subject: [PATCH] Default pkg to empty object in case readPackageJson fails (#354) * 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 --- lib/spec/openapi/utils.js | 4 ++-- lib/spec/swagger/utils.js | 4 ++-- lib/util/common.js | 4 ++-- test/mode/static.js | 40 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js index e3d97784..688c56b7 100644 --- a/lib/spec/openapi/utils.js +++ b/lib/spec/openapi/utils.js @@ -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: { diff --git a/lib/spec/swagger/utils.js b/lib/spec/swagger/utils.js index ca8dab94..4cb4c65f 100644 --- a/lib/spec/swagger/utils.js +++ b/lib/spec/swagger/utils.js @@ -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: { diff --git a/lib/util/common.js b/lib/util/common.js index b4a3d656..ff5808ca 100644 --- a/lib/util/common.js +++ b/lib/util/common.js @@ -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 {} } } diff --git a/test/mode/static.js b/test/mode/static.js index 3db7ef45..f652d8c2 100644 --- a/test/mode/static.js +++ b/test/mode/static.js @@ -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 @@ -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) + }) + }) +})