openapi-enforcer
Advanced tools
Comparing version 1.15.5 to 1.16.0
@@ -7,2 +7,26 @@ # Change Log | ||
## 1.16.0 | ||
All functionality should be the same as before, but due to the types of changes we're making this a minor release instead of a patch. | ||
### Changed | ||
- **Improved JSON Ref Resolution** | ||
There has been a built-in JSON schema ref resolver for some time now, but it has not been the default. This has now been made both the default and the only option for ref resolution allowing the `json-schema-ref-parser` dependency to be removed. | ||
This ref resolver is slightly better than the generic `json-schema-ref-parser` because it recognizes references in OpenAPI (and Swagger) documents that are not identified by the `$ref` property. As an example, discriminators have non `$ref` references. | ||
- **Removed axios Dependency** | ||
This dependency was only used for HTTP/S GET requests. Now the core NodeJS `http` or `https` library is being used instead. | ||
- **Remove json-schema-ref-parser Dependency** | ||
See the first bullet point of this change entry. | ||
- **Added js-yaml Dependency** | ||
The built-in json schema reference parser uses js-yaml. Previously it was using the js-yaml dependency that was included by the json-schema-ref-parse, but with that gone we had to add it in as a dependency. | ||
## 1.15.5 | ||
@@ -9,0 +33,0 @@ |
54
index.js
@@ -24,3 +24,2 @@ /** | ||
const NewRefParser = require('./src/ref-parser'); | ||
const OldRefParser = require('json-schema-ref-parser'); | ||
const Result = require('./src/result'); | ||
@@ -52,11 +51,6 @@ const Super = require('./src/super'); | ||
definition = util.copy(definition); | ||
const useNewRefParser = Enforcer.config.useNewRefParser; | ||
const refParser = useNewRefParser ? new NewRefParser(definition) : new OldRefParser(); | ||
if (useNewRefParser) { | ||
const [ dereferenceValue, dereferenceErr ] = await refParser.dereference(); | ||
definition = dereferenceValue; | ||
exception = dereferenceErr; | ||
} else { | ||
definition = await refParser.dereference(definition); | ||
} | ||
const refParser = new NewRefParser(definition); | ||
const [ dereferenceValue, dereferenceErr ] = await refParser.dereference(); | ||
definition = dereferenceValue; | ||
exception = dereferenceErr; | ||
@@ -89,9 +83,5 @@ if (!exception) { | ||
openapi.getBundledDefinition = async function () { | ||
if (useNewRefParser) { | ||
const result = await refParser.bundle() | ||
if (result.error) throw Error(result.error) | ||
return result.value | ||
} else { | ||
return await refParser.bundle(definition) | ||
} | ||
const result = await refParser.bundle() | ||
if (result.error) throw Error(result.error) | ||
return result.value | ||
} | ||
@@ -106,19 +96,29 @@ | ||
Enforcer.bundle = function (definition) { | ||
Enforcer.bundle = async function (definition) { | ||
// We have gotten rid of the old ref parser, but realized that the return signature for the old ref parser | ||
// did not match that of the new ref parser, so we have to keep the (if useNewRefParser) statement to | ||
// continue to return the expected signatures. | ||
const refParser = new NewRefParser(definition); | ||
const data = await refParser.bundle(); | ||
if (Enforcer.config.useNewRefParser) { | ||
const refParser = new NewRefParser(definition); | ||
return refParser.bundle(); | ||
return data | ||
} else { | ||
const refParser = new OldRefParser(); | ||
return refParser.bundle(definition); | ||
const [bundle, error] = data | ||
if (error) throw error | ||
return bundle | ||
} | ||
}; | ||
Enforcer.dereference = function (definition) { | ||
Enforcer.dereference = async function (definition) { | ||
// We have gotten rid of the old ref parser, but realized that the return signature for the old ref parser | ||
// did not match that of the new ref parser, so we have to keep the (if useNewRefParser) statement to | ||
// continue to return the expected signatures. | ||
const refParser = new NewRefParser(definition); | ||
const data = await refParser.dereference(); | ||
if (Enforcer.config.useNewRefParser) { | ||
const refParser = new NewRefParser(definition); | ||
return refParser.dereference(); | ||
return data | ||
} else { | ||
const refParser = new OldRefParser(); | ||
return refParser.dereference(definition); | ||
const [ deref, error ] = data | ||
if (error) throw error | ||
return deref | ||
} | ||
@@ -125,0 +125,0 @@ }; |
{ | ||
"name": "openapi-enforcer", | ||
"version": "1.15.5", | ||
"version": "1.16.0", | ||
"description": "Library for validating, parsing, and formatting data against open api schemas.", | ||
@@ -49,14 +49,11 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@gi60s/markdown-docs": "^0.1.2", | ||
"chai": "^4.2.0", | ||
"chokidar-cli": "^1.2.3", | ||
"coveralls": "^3.1.0", | ||
"mocha": "^8.3.0", | ||
"nyc": "^14.1.1" | ||
"chai": "^4.3.4", | ||
"coveralls": "^3.1.1", | ||
"mocha": "^9.1.3", | ||
"nyc": "^15.1.0" | ||
}, | ||
"dependencies": { | ||
"axios": "^0.21.1", | ||
"json-schema-ref-parser": "^6.1.0", | ||
"js-yaml": "^4.1.0", | ||
"randexp": "^0.5.3" | ||
} | ||
} |
@@ -19,3 +19,2 @@ /** | ||
const axios = require('axios').default; | ||
const Exception = require('./exception'); | ||
@@ -28,2 +27,4 @@ const fs = require('fs'); | ||
const yaml = require('js-yaml'); | ||
const Https = require("https"); | ||
const Http = require("http"); | ||
@@ -372,9 +373,10 @@ const rxHttp = /^https?:\/\//i; | ||
function httpLoad (url, exception) { | ||
const transformResponse = [res => res]; // stop response body from being parsed | ||
return axios.get(url, { transformResponse }) | ||
.then(res => { | ||
const contentType = res.headers['content-type']; | ||
if (res.status < 200 || res.status >= 300) { | ||
exception.message('Unable to load resource due to unexpected response status code ' + res.status + ' for URL: ' + url ); | ||
return new Promise((resolve, reject) => { | ||
const mode = url.startsWith('https') ? Https : Http | ||
const req = mode.request(url, {}, (res) => { | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
exception.message('Request failed with status code ' + res.statusCode); | ||
resolve() | ||
} else { | ||
const contentType = res.headers['content-type']; | ||
let type; | ||
@@ -384,10 +386,20 @@ if (/^application\/json/.test(contentType)) type = 'json'; | ||
const result = parseString(res.data, type, exception.nest('Unable to parse resource: ' + url)); | ||
res.data = result.value; | ||
let data = '' | ||
res.setEncoding('utf8') | ||
res.on('data', (chunk) => { | ||
data += chunk | ||
}) | ||
res.on('end', () => { | ||
const result = parseString(data, type, exception.nest('Unable to parse resource: ' + url)); | ||
resolve(result.value); | ||
}) | ||
res.on('error', (err) => { | ||
exception.message('Unexpected error: ' + err.message); | ||
resolve() | ||
}) | ||
} | ||
return res.data | ||
}) | ||
.catch(err => { | ||
exception.message('Unexpected error: ' + err.message); | ||
}); | ||
req.on('error', reject) | ||
req.end() | ||
}) | ||
} | ||
@@ -432,3 +444,3 @@ | ||
try { | ||
value = yaml.safeLoad(content); | ||
value = yaml.load(content); | ||
} catch (err) { | ||
@@ -443,3 +455,3 @@ exception.message(err.toString()); | ||
try { | ||
value = yaml.safeLoad(content); | ||
value = yaml.load(content); | ||
type = 'yaml'; | ||
@@ -446,0 +458,0 @@ } catch (err) { |
Sorry, the diff of this file is too big to display
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
1588963
2
4
20946
4
+ Addedjs-yaml@^4.1.0
+ Addedargparse@2.0.1(transitive)
+ Addedjs-yaml@4.1.0(transitive)
- Removedaxios@^0.21.1
- Removedjson-schema-ref-parser@^6.1.0
- Removedargparse@1.0.10(transitive)
- Removedaxios@0.21.4(transitive)
- Removedcall-me-maybe@1.0.2(transitive)
- Removedesprima@4.0.1(transitive)
- Removedfollow-redirects@1.15.9(transitive)
- Removedformat-util@1.0.5(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedjson-schema-ref-parser@6.1.0(transitive)
- Removedono@4.0.11(transitive)
- Removedsprintf-js@1.0.3(transitive)