Skip to content

Commit

Permalink
Added fix for basic relative URLs in servers (#67)
Browse files Browse the repository at this point in the history
Co-authored-by: Igor Savin <iselwin@gmail.com>
  • Loading branch information
dipsmishra and kibertoad committed May 11, 2021
1 parent 15d8007 commit 4165d0e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Master

# 2.0.5 - 08 Mar, 2021
# 2.0.5 - 1 February, 2021
### Improvements
- Added basic support for relative URLs #59
- Update dependencies to fix security vulnerabilities

# 2.0.4 - 26 Nov, 2020
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function buildValidations(referenced, dereferenced, receivedOptions) {
const schemas = {};

const basePaths = dereferenced.servers && dereferenced.servers.length
? dereferenced.servers.map(({ url }) => new URL(url).pathname)
? dereferenced.servers.map(({ url }) => url.indexOf('://') > -1 ? new URL(url).pathname : url)
: [dereferenced.basePath || '/'];

Object.keys(dereferenced.paths).forEach(currentPath => {
Expand Down
18 changes: 18 additions & 0 deletions test/openapi3/general/general-oai3-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ describe('oai3 - general tests', () => {
expect(typeof schema['/json'].put.body['application/json'].validate).to.eql('function');
expect(typeof schema['/staging/json'].put.body['application/json'].validate).to.eql('function');
});
it('correctly works with servers with relative paths', () => {
const swaggerPath = path.join(__dirname, 'pets-general-relative-paths-too.yaml');
const schema = schemaValidatorGenerator.buildSchemaSync(swaggerPath, {});
expect(Object.keys(schema)).to.eql([
'/text',
'/staging/text',
'/v1/reports/text',
'/empty',
'/staging/empty',
'/v1/reports/empty',
'/json',
'/staging/json',
'/v1/reports/json'
]);
expect(typeof schema['/json'].put.body['application/json'].validate).to.eql('function');
expect(typeof schema['/staging/json'].put.body['application/json'].validate).to.eql('function');
expect(typeof schema['/v1/reports/json'].put.body['application/json'].validate).to.eql('function');
});
it('correctly works with empty servers', () => {
const swaggerPath = path.join(__dirname, 'pets-general-empty-servers.yaml');
const schema = schemaValidatorGenerator.buildSchemaSync(swaggerPath, {});
Expand Down
98 changes: 98 additions & 0 deletions test/openapi3/general/pets-general-relative-paths-too.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
paths:
/text:
put:
requestBody:
content:
plain/text:
schema:
type: string
required: true
responses:
"200":
description: Expected response to a valid request
content:
plain/text:
schema:
properties:
lastname:
type: string
default:
description: unexpected error
content:
text/plain:
schema:
$ref: "#/components/schemas/Error"

/empty:
post:
requestBody:
description: Expected response to a valid request
content:
plain/text:
schema:
type: string
required: true
responses:
"200":
description: Expected response to a valid request
content:
plain/text:
schema:
properties:
lastname:
type: string
default:
description: unexpected error
content:
text/plain:
schema:
$ref: "#/components/schemas/Error"
/json:
put:
parameters:
- in: header
name: public-key
schema:
type: string
requestBody:
content:
application/json:
schema:
type: string
required: true
responses:
"200":
description: Expected response to a valid request
content:
application/json:
schema:
properties:
lastname:
type: string
default:
description: unexpected error
content:
text/plain:
schema:
$ref: "#/components/schemas/Error"
servers:
- url: http://petstore.swagger.io
- url: http://petstore.swagger.io/staging/
- url: /v1/reports
components:
schemas:
Error:
required:
- code
- message
properties:
code:
type: integer
message:
type: string

0 comments on commit 4165d0e

Please sign in to comment.