Skip to content

Commit e187497

Browse files
committed
feat: add missing handling of jsonPath route
1 parent e34532a commit e187497

File tree

7 files changed

+40
-5
lines changed

7 files changed

+40
-5
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=https://registry.npmjs.org

index.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,17 @@ declare namespace hapiswagger {
171171
cors?: boolean;
172172

173173
/**
174-
* The path of JSON endpoint at describes the API
174+
* The path of JSON endpoint that describes the API
175175
* @default '/swagger.json'
176176
*/
177177
jsonPath?: string;
178178

179+
/**
180+
* The path for the controller that serves the JSON that describes the API.If jsonPath is specified and this parameter is not, it will take jsonPath value.Useful when behind a reverse proxy
181+
* @default '/swagger.json'
182+
*/
183+
jsonRoutePath?: string;
184+
179185
/**
180186
* The base path from where the API starts i.e. `/v2/` (note, needs to start with `/`)
181187
* @default '/'
@@ -312,7 +318,7 @@ declare namespace hapiswagger {
312318

313319
/**
314320
* The path to all the SwaggerUI assets endpoints.
315-
* If swaggerUIPath is specified and this parameter is not, it will take swaggerUIPath value
321+
* If swaggerUIPath is specified and this parameter is not, it will take swaggerUIPath value. Useful when behind a reverse proxy
316322
* @default: '/swaggerui/'
317323
*/
318324
routesBasePath?: string;

lib/builder.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ internals.removeNoneSchemaOptions = function(options) {
221221
'documentationRouteTags',
222222
'documentationPage',
223223
'jsonPath',
224+
'jsonRoutePath',
224225
'auth',
225226
'swaggerUIPath',
226227
'routesBasePath',

lib/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { resolve } = require('path');
33
module.exports = {
44
debug: false,
55
jsonPath: '/swagger.json',
6+
jsonRoutePath: '/swagger.json',
67
documentationPath: '/documentation',
78
documentationRouteTags: [],
89
documentationRoutePlugins: {},

lib/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const Utilities = require('../lib/utilities');
1515
const schema = Joi.object({
1616
debug: Joi.boolean(),
1717
jsonPath: Joi.string(),
18+
jsonRoutePath: Joi.string(),
1819
documentationPath: Joi.string(),
1920
documentationRouteTags: Joi.alternatives().try(Joi.string(), Joi.array().items(Joi.string())),
2021
documentationRoutePlugins: Joi.object().default({}),
@@ -71,6 +72,10 @@ exports.plugin = {
7172
settings.routesBasePath = options.swaggerUIPath;
7273
}
7374

75+
if (!options.jsonRoutePath && options.jsonPath) {
76+
settings.jsonRoutePath = options.jsonPath;
77+
}
78+
7479
settings.log = (tags, data) => {
7580
tags.unshift('hapi-swagger');
7681
if (settings.debug) {
@@ -103,7 +108,7 @@ exports.plugin = {
103108
server.route([
104109
{
105110
method: 'GET',
106-
path: settings.jsonPath,
111+
path: settings.jsonRoutePath,
107112
options: {
108113
auth: settings.auth,
109114
cors: settings.cors,

optionsreference.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
#### JSON (JSON endpoint needed to create UI)
1313

14-
- `jsonPath`: (string) The path of JSON endpoint at describes the API - default: `/swagger.json`
14+
- `jsonPath`: (string) The path of the JSON endpoint that describes the API - default: `/swagger.json`
15+
- `jsonRoutePath`: (string) The path for the controller that serves the JSON that describes the API. If jsonPath is specified and this parameter is not, it will take jsonPath value. Useful when behind a reverse proxy - default: `/swagger.json`
1516
- `basePath`: (string) The base path from where the API starts i.e. `/v2/` (note, needs to start with `/`) - default: `/`
1617
- `pathPrefixSize`: (number) Selects what segment of the URL path is used to group endpoints - default: `1`
1718
- `pathReplacements` : (array) methods for modifying path and group names in documentation - default: `[]`
@@ -52,7 +53,7 @@
5253
5354
- `swaggerUI`: (boolean) Add files that support SwaggerUI. Only removes files if `documentationPage` is also set to false - default: `true`
5455
- `swaggerUIPath`: (string) The path of to all the SwaggerUI resources - default: `/swaggerui/`
55-
- `routesBasePath`: (string) The path to all the SwaggerUI assets endpoints. If swaggerUIPath is specified and this parameter is not, it will take swaggerUIPath value - default: `/swaggerui/`
56+
- `routesBasePath`: (string) The path to all the SwaggerUI assets endpoints. If swaggerUIPath is specified and this parameter is not, it will take swaggerUIPath value. Useful when behind a reverse proxy - default: `/swaggerui/`
5657
- `documentationPage`: (boolean) Add documentation page - default: `true`
5758
- `documentationPath`: (string) The path of the documentation page - default: `/documentation`
5859
- `templates`: (string) The directory path used by `hapi-swagger` and `@hapi/vision` to resolve and load the templates to render `swagger-ui` interface. The directory must contain `index.html` and `debug.html` templates. Default is `templates` directory in this package.

test/Integration/plugin-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ lab.experiment('plugin', () => {
109109
expect(response.statusCode).to.equal(200);
110110
});
111111

112+
lab.test('repathed jsonPath url and jsonRoutePath', async () => {
113+
const jsonRoutePath = '/testRoute/test.json';
114+
115+
const server = await Helper.createServer(
116+
{
117+
...swaggerOptions,
118+
jsonRoutePath
119+
},
120+
routes
121+
);
122+
123+
const notFoundResponse = await server.inject({ method: 'GET', url: '/test.json' });
124+
125+
expect(notFoundResponse.statusCode).to.equal(404);
126+
127+
const okResponse = await server.inject({ method: 'GET', url: jsonRoutePath });
128+
129+
expect(okResponse.statusCode).to.equal(200);
130+
});
131+
112132
lab.test('repathed documentationPath url', async () => {
113133
const server = await Helper.createServer(swaggerOptions, routes);
114134
const response = await server.inject({ method: 'GET', url: '/testdoc' });

0 commit comments

Comments
 (0)