openapi-enforcer
Advanced tools
Comparing version 1.17.1 to 1.17.2
@@ -7,3 +7,10 @@ # Change Log | ||
## 1.17.2 | ||
### Fixed | ||
- **Allow Two Similar Yet Distinct Paths When Methods Do Not Collide** | ||
Before this fix you could not define two OpenAPI paths with the same path parameter location and different methods. For example, it was not possible to have both a `GET /{x}` and `POST /{y}`. This fix now allows that which also allows for different variable input so long as the paths have different variable names. | ||
## 1.17.1 | ||
@@ -10,0 +17,0 @@ |
{ | ||
"name": "openapi-enforcer", | ||
"version": "1.17.1", | ||
"version": "1.17.2", | ||
"description": "Library for validating, parsing, and formatting data against open api schemas.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -47,4 +47,4 @@ /** | ||
// find the path that matches the request | ||
const pathMatch = this.paths.findMatch(path); | ||
if (!pathMatch) { | ||
const pathMatches = this.paths.findMatches(path); | ||
if (pathMatches.length === 0) { | ||
exception.message('Path not found'); | ||
@@ -56,8 +56,16 @@ exception.statusCode = 404; | ||
// check that a valid method was specified | ||
const pathEnforcer = pathMatch.path; | ||
if (!pathEnforcer.methods.includes(method)) { | ||
const pathMatch = pathMatches.find(v => v.path.methods.includes(method)) | ||
if (!pathMatch) { | ||
const allowedMethods = new Set() | ||
pathMatches.forEach(pathMatch => { | ||
pathMatch.path.methods.forEach(method => { | ||
allowedMethods.add(method.toUpperCase()) | ||
}) | ||
}) | ||
exception.message('Method not allowed: ' + method.toUpperCase()); | ||
exception.statusCode = 405; | ||
exception.pathItem = pathEnforcer | ||
exception.headers = { Allow: pathEnforcer.methods.map(v => v.toUpperCase()).join(', ') }; | ||
exception.pathItem = pathMatches[0].path | ||
exception.pathItems = pathMatches.map(v => v.path) | ||
exception.headers = { Allow: Array.from(allowedMethods).join(', ') }; | ||
return new Result(undefined, exception); | ||
@@ -67,2 +75,3 @@ } | ||
// parse and validate path parameters | ||
const pathEnforcer = pathMatch.path; | ||
const operation = pathEnforcer[method]; | ||
@@ -69,0 +78,0 @@ const pathParams = operation.parametersMap.path; |
@@ -220,6 +220,11 @@ /** | ||
findMatch: function (pathString) { | ||
return this.findMatches(pathString)[0] | ||
}, | ||
findMatches: function (pathString) { | ||
const { pathParsers } = this.enforcerData; | ||
const matches = [] | ||
// normalize the path | ||
pathString = pathString.split('?')[0]; // util.edgeSlashes(pathString.split('?')[0], true, false); | ||
pathString = pathString.split('?')[0]; | ||
@@ -231,3 +236,3 @@ // get all parsers that fit the path length | ||
// if the parser was not found then they have a bad path | ||
if (!parsers) return; | ||
if (!parsers) return matches; | ||
@@ -239,4 +244,5 @@ // find the right parser and run it | ||
const results = parser(pathString); | ||
if (results) return results; | ||
if (results) matches.push(results); | ||
} | ||
return matches | ||
} | ||
@@ -243,0 +249,0 @@ }, |
@@ -87,2 +87,30 @@ /** | ||
it('can distinguish between paths with mathing variable placement and different operations', () => { | ||
const [ openapi, error ] = Enforcer.v2_0.Swagger({ | ||
swagger: '2.0', | ||
info: { title: '', version: '1.0.0' }, | ||
paths: { | ||
'/{x}': { // distinct path due to different variable name | ||
get: { // not a collision due to different method | ||
parameters: [{ name: 'x', in: 'path', required: true, type: 'string' }], | ||
responses: { 200: { description: 'ok' } } | ||
} | ||
}, | ||
'/{y}': { // distinct path due to different variable name | ||
put: { // not a collision due to different method | ||
parameters: [{ name: 'y', in: 'path', required: true, type: 'string' }], | ||
responses: { 200: { description: 'ok' } } | ||
} | ||
} | ||
} | ||
}); | ||
expect(error).to.equal(undefined) | ||
const [ getPath ] = openapi.path('get', '/123'); | ||
expect(getPath.params.x).to.equal('123'); | ||
const [ putPath ] = openapi.path('put', '/abc'); | ||
expect(putPath.params.y).to.equal('abc'); | ||
}) | ||
}); | ||
@@ -89,0 +117,0 @@ |
1038341
113
20602