openapi-enforcer
Advanced tools
Comparing version 1.17.2 to 1.18.0
@@ -7,2 +7,10 @@ # Change Log | ||
## 1.18.0 | ||
### Added | ||
- **Case Sensitivity Optional For Paths** | ||
The default behavior is for paths to be case sensitive. There is now an option `Enforcer.config.useCaseSensitivePaths` (defaulting to `true`) that when set to `false` will change how paths duplicates are validated and how paths are looked up when attempting to match a path to a request. | ||
## 1.17.2 | ||
@@ -14,3 +22,3 @@ | ||
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. | ||
Before this fix you could define two OpenAPI paths with the same path parameter location and different methods, but when attempting to match a path to a request the second path would be unreachable. This fix resolves the issue, allowing both paths to be found. | ||
@@ -17,0 +25,0 @@ ## 1.17.1 |
@@ -91,2 +91,3 @@ /** | ||
examplesWarnAdditionalProperty: true, | ||
useCaseSensitivePaths: true, | ||
useNewRefParser: false | ||
@@ -93,0 +94,0 @@ }; |
{ | ||
"name": "openapi-enforcer", | ||
"version": "1.17.2", | ||
"version": "1.18.0", | ||
"description": "Library for validating, parsing, and formatting data against open api schemas.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -43,2 +43,4 @@ /** | ||
plugins.push(() => { | ||
const enforcerConfig = require('../../').config | ||
Object.keys(result).forEach((pathKey, index) => { | ||
@@ -114,3 +116,5 @@ const path = result[pathKey]; | ||
const rx = new RegExp('^' + rxStr + '$'); | ||
const rx = enforcerConfig.useCaseSensitivePaths | ||
? new RegExp('^' + rxStr + '$') | ||
: new RegExp('^' + rxStr + '$', 'i'); | ||
@@ -166,3 +170,5 @@ // define parser function | ||
method, | ||
signature: [method].concat(pathSignature) | ||
signature: enforcerConfig.useCaseSensitivePaths | ||
? [method].concat(pathSignature) | ||
: [method].concat(pathSignature.map(v => typeof v === 'string' ? v.toLowerCase() : v)) | ||
}) | ||
@@ -169,0 +175,0 @@ }) |
@@ -115,2 +115,84 @@ /** | ||
describe('path case sensitivity', () => { | ||
const config = require('../index').config | ||
let defaultCaseSensitivity | ||
before(() => { | ||
defaultCaseSensitivity = config.useCaseSensitivePaths | ||
}) | ||
after(() => { | ||
config.useCaseSensitivePaths = defaultCaseSensitivity | ||
}) | ||
function init (useCaseSensitivePaths) { | ||
config.useCaseSensitivePaths = useCaseSensitivePaths | ||
return Enforcer.v2_0.Swagger({ | ||
swagger: '2.0', | ||
info: { title: '', version: '1.0.0' }, | ||
paths: { | ||
'/foo/Bar/baz': { | ||
get: { | ||
responses: { 200: { description: 'ok' } } | ||
} | ||
}, | ||
'/foo/bar/baz': { | ||
get: { | ||
responses: { 200: { description: 'ok' } } | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
it('can have case sensitive paths', async () => { | ||
config.useCaseSensitivePaths = true | ||
const [ openapi ] = Enforcer.v2_0.Swagger({ | ||
swagger: '2.0', | ||
info: { title: '', version: '1.0.0' }, | ||
paths: { | ||
'/foo/Bar/baz': { | ||
get: { | ||
responses: { 200: { description: 'ok' } } | ||
} | ||
}, | ||
'/foo/bar/baz': { | ||
get: { | ||
responses: { 200: { description: 'ok' } } | ||
} | ||
} | ||
} | ||
}); | ||
const [ first ] = openapi.path('get', '/foo/Bar/baz'); | ||
expect(first.pathKey).to.equal('/foo/Bar/baz'); | ||
const [ second ] = openapi.path('get', '/foo/bar/baz'); | ||
expect(second.pathKey).to.equal('/foo/bar/baz'); | ||
}) | ||
it('can have case insensitive paths', async () => { | ||
config.useCaseSensitivePaths = false | ||
const [ openapi ] = Enforcer.v2_0.Swagger({ | ||
swagger: '2.0', | ||
info: { title: '', version: '1.0.0' }, | ||
paths: { | ||
'/foo/bar/baz': { | ||
get: { | ||
responses: { 200: { description: 'ok' } } | ||
} | ||
} | ||
} | ||
}); | ||
const [ first ] = openapi.path('get', '/foo/bar/baz'); | ||
expect(first.pathKey).to.equal('/foo/bar/baz'); | ||
const [ second ] = openapi.path('get', '/FOO/Bar/Baz'); | ||
expect(second.pathKey).to.equal('/foo/bar/baz'); | ||
}) | ||
}) | ||
}); | ||
@@ -117,0 +199,0 @@ |
@@ -117,2 +117,20 @@ /** | ||
it('allows duplicate paths using case sensitivity (default)', () => { | ||
const [ , err ] = Enforcer.v2_0.Paths({ | ||
'/a': validPathObject(), | ||
'/A': validPathObject() | ||
}); | ||
expect(err).to.equal(undefined); | ||
}); | ||
it('does not allow duplicate paths using case insensitivity', () => { | ||
const config = require('../index').config | ||
config.useCaseSensitivePaths = false | ||
const [ , err, warning ] = Enforcer.v2_0.Paths({ | ||
'/a': validPathObject(), | ||
'/A': validPathObject() | ||
}); | ||
expect(err).to.match(/Equivalent paths are not allowed/); | ||
}); | ||
it('correctly prioritizes path selection', () => { | ||
@@ -119,0 +137,0 @@ const [ paths ] = Enforcer.v2_0.Paths({ |
1031899
112
20438