Skip to content

Commit

Permalink
Merge 04faef6 into 1c3e0c0
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed May 4, 2020
2 parents 1c3e0c0 + 04faef6 commit e4c4771
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -40,7 +40,7 @@ function buildValidations(referenced, dereferenced, receivedOptions) {

const schemas = {};

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

Expand Down
21 changes: 21 additions & 0 deletions test/openapi3/general/general-oai3-test.js
Expand Up @@ -118,6 +118,27 @@ 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 empty servers', () => {
const swaggerPath = path.join(__dirname, 'pets-general-empty-servers.yaml');
const schema = schemaValidatorGenerator.buildSchemaSync(swaggerPath, {});
expect(Object.keys(schema)).to.eql([
'/text',
'/empty',
'/json'
]);
expect(typeof schema['/json'].put.body['application/json'].validate).to.eql('function');
});
it('correctly works without servers', () => {
const swaggerPath = path.join(__dirname, 'pets-general-no-servers.yaml');
const schema = schemaValidatorGenerator.buildSchemaSync(swaggerPath, {});
expect(Object.keys(schema)).to.eql([
'/text',
'/empty',
'/json'
]);
expect(typeof schema['/json'].put.body['application/json'].validate).to.eql('function');
});
});
});

Expand Down
95 changes: 95 additions & 0 deletions test/openapi3/general/pets-general-empty-servers.yaml
@@ -0,0 +1,95 @@
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: []
components:
schemas:
Error:
required:
- code
- message
properties:
code:
type: integer
message:
type: string
94 changes: 94 additions & 0 deletions test/openapi3/general/pets-general-no-servers.yaml
@@ -0,0 +1,94 @@
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"
components:
schemas:
Error:
required:
- code
- message
properties:
code:
type: integer
message:
type: string

0 comments on commit e4c4771

Please sign in to comment.