Skip to content

Commit

Permalink
Merge pull request #348 from pimterry/basePath-slash
Browse files Browse the repository at this point in the history
Strip trailing slashes when generating OpenAPI server URLs from basePath
  • Loading branch information
MikeRalphson authored Jan 24, 2021
2 parents bd728d3 + 75143f8 commit f54dde2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/swagger2openapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,8 @@ function convertObj(swagger, options, callback) {
if (swagger.host) {
for (let s of (Array.isArray(swagger.schemes) ? swagger.schemes : [''])) {
let server = {};
server.url = (s ? s+':' : '') + '//' + swagger.host + (swagger.basePath ? swagger.basePath : '');
let basePath = (swagger.basePath || '').replace(/\/$/, '') // Trailing slashes generally shouldn't be included
server.url = (s ? s+':' : '') + '//' + swagger.host + basePath;
extractServerParameters(server);
if (!openapi.servers) openapi.servers = [];
openapi.servers.push(server);
Expand Down
12 changes: 12 additions & 0 deletions test/s2o-test/pull348-trailing-slash/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
openapi: 3.0.0
info:
title: API
version: 1.0.0
servers:
- url: https://example.com
paths:
/:
get:
responses:
'200':
description: OK
14 changes: 14 additions & 0 deletions test/s2o-test/pull348-trailing-slash/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
swagger: 2.0
info:
title: API
version: 1.0.0
basePath: /
host: example.com
schemes:
- https
paths:
/:
get:
responses:
'200':
description: OK

0 comments on commit f54dde2

Please sign in to comment.