Skip to content

Commit

Permalink
Fix exceptions when loading OAS documents with extensions on paths an…
Browse files Browse the repository at this point in the history
…d responses
  • Loading branch information
alasdairhurst committed Feb 19, 2020
1 parent 662d57e commit 533ca57
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 30 deletions.
4 changes: 2 additions & 2 deletions dist/sway-min.js

Large diffs are not rendered by default.

60 changes: 48 additions & 12 deletions dist/sway.js

Large diffs are not rendered by default.

20 changes: 13 additions & 7 deletions lib/types/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,19 @@ function SwaggerApi (definition, definitionRemotesResolved, definitionFullyResol
debug(' Paths:');

// Create the Path objects
this.pathObjects = _.map(definitionFullyResolved.paths, function (pathDef, path) {
return new Path(that,
path,
_.get(definitionRemotesResolved, ['paths', path]),
pathDef,
['paths', path]);
});
this.pathObjects = _.reduce(definitionFullyResolved.paths, function (paths, pathDef, path) {
// do not process extensions
if (_.startsWith(path, 'x-')) {
return paths;
}
paths.push(new Path(
that,
path,
_.get(definitionRemotesResolved, ['paths', path]),
pathDef,
['paths', path]));
return paths;
}, []);
}

/**
Expand Down
19 changes: 12 additions & 7 deletions lib/types/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,20 @@ function Operation (pathObject, method, definition, definitionFullyResolved, pat
this._debug(' Responses:');

// Create response objects from responses defined in the operation definition
this.responseObjects = _.map(this.definitionFullyResolved.responses, function (responseDef, code) {
this.responseObjects = _.reduce(this.definitionFullyResolved.responses, function (responses, responseDef, code) {
// do not process extensions
if (_.startsWith(code, 'x-')) {
return responses;
}
var rPath = pathToDefinition.concat(['responses', code])

return new Response(that,
code,
_.get(that.pathObject.api.definitionRemotesResolved, rPath),
responseDef,
rPath);
});
responses.push(new Response(that,
code,
_.get(that.pathObject.api.definitionRemotesResolved, rPath),
responseDef,
rPath));
return responses;
}, []);

this._debug(' Security:');

Expand Down
25 changes: 25 additions & 0 deletions lib/validation/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ function validateReferences (api) {
});

_.forEach(api.definitionFullyResolved.responses, function (def, name) {
// do not process extensions
if (_.startsWith(name, 'x-')) {
return;
}

referenceable.push(JsonRefs.pathToPtr(['responses', name]));
});

Expand Down Expand Up @@ -398,6 +403,11 @@ function validateReferences (api) {
_.forEach(api.definitionFullyResolved.security, createSecurityProcessor(['security']));

_.forEach(api.definitionFullyResolved.paths, function (pathDef, name) {
// do not process extensions
if (_.startsWith(name, 'x-')) {
return;
}

var pPath = ['paths', name];

_.forEach(pathDef.security, createSecurityProcessor(pPath.concat('security')));
Expand Down Expand Up @@ -477,6 +487,11 @@ function validateSchemaObjects (api) {

function validateResponses (responses, path) {
_.forEach(responses, function (responseDef, name) {
// do not process extensions
if (_.startsWith(name, 'x-')) {
return;
}

var rPath = path.concat(name);

_.forEach(responseDef.headers, function (header, hName) {
Expand All @@ -502,6 +517,11 @@ function validateSchemaObjects (api) {

// Validate paths and operations
_.forEach(api.definitionFullyResolved.paths, function (pathDef, path) {
// do not process extensions
if (_.startsWith(path, 'x-')) {
return;
}

var pPath = ['paths', path];

// Validate path-level parameter definitions
Expand Down Expand Up @@ -570,6 +590,11 @@ function validatePathsAndOperations (api) {
var normalizedPath = path;
var pPath = ['paths', path];

// do not process extensions
if (_.startsWith(path, 'x-')) {
return metadata;
}

_.forEach(path.match(/\{(.*?)\}/g), function (arg, index) {
// Record the path parameter name
declaredPathParameters.push(arg.replace(/[{}]/g, ''));
Expand Down
8 changes: 8 additions & 0 deletions test/browser/documents/2.0/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ tags:
schemes:
- "http"
paths:
x-an-extension-null:
x-an-extension-primitive: 1
x-an-extension-array: []
x-an-extension-object: {}
/pet:
post:
tags:
Expand All @@ -52,6 +56,10 @@ paths:
schema:
$ref: "#/definitions/Pet"
responses:
x-an-extension-null:
x-an-extension-primitive: 1
x-an-extension-array: []
x-an-extension-object: {}
405:
description: "Invalid input"
put:
Expand Down
8 changes: 6 additions & 2 deletions test/test-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,12 @@ function runTests (mode) {
});

describe('#getPaths', function () {
it('should return the expected path objects', function () {
assert.equal(swaggerApi.getPaths().length, Object.keys(swaggerApi.definitionFullyResolved.paths).length);
it('should return the expected path objects without extensions', function () {
var paths = Object.keys(swaggerApi.definitionFullyResolved.paths).filter(function (path) {
return path.indexOf('x-') !== 0;
});

assert.equal(swaggerApi.getPaths().length, paths.length);
});
});

Expand Down

0 comments on commit 533ca57

Please sign in to comment.