Skip to content

Commit

Permalink
fix: bump dependencies, fix tests, fix imports from deps
Browse files Browse the repository at this point in the history
  • Loading branch information
jonluca committed Oct 10, 2024
1 parent 7a2245b commit eeb97de
Show file tree
Hide file tree
Showing 10 changed files with 8,244 additions and 21,684 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/CI-CD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: 'npm'
cache: "npm"

- name: Install dependencies
run: npm ci
Expand Down Expand Up @@ -61,18 +61,18 @@ jobs:
fail-fast: true
matrix:
os:
- ubuntu-latest # Chrome, Firefox, Safari (TODO)
- ubuntu-latest # Chrome, Firefox, Safari (TODO)
# - windows-latest # Internet Explorer, Edge

steps:
- name: Checkout source
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 12
cache: 'npm'
node-version: lts
cache: "npm"

- name: Install dependencies
run: npm ci
Expand Down Expand Up @@ -117,13 +117,13 @@ jobs:

steps:
- name: Checkout source
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 12
cache: 'npm'
node-version: lts
cache: "npm"

- name: Install dependencies
run: npm ci
Expand Down
32 changes: 23 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

const validateSchema = require("./validators/schema");
const validateSpec = require("./validators/spec");
const normalizeArgs = require("@apidevtools/json-schema-ref-parser/lib/normalize-args");
const { jsonSchemaParserNormalizeArgs: normalizeArgs } = require("@apidevtools/json-schema-ref-parser");
const util = require("./util");
const Options = require("./options");
const maybe = require("call-me-maybe");
const { ono } = require("@jsdevtools/ono");
const $RefParser = require("@apidevtools/json-schema-ref-parser");
const dereference = require("@apidevtools/json-schema-ref-parser/lib/dereference");
const { $RefParser } = require("@apidevtools/json-schema-ref-parser");
const { dereferenceInternal: dereference } = require("@apidevtools/json-schema-ref-parser");

/**
* This class parses a Swagger 2.0 or 3.0 API, resolves its JSON references and their resolved values,
Expand Down Expand Up @@ -176,14 +176,28 @@ Object.defineProperty(SwaggerParser.prototype, "api", {
* @typedef {{swagger: string, info: {}, paths: {}}} SwaggerObject
*/

const defaultInstance = new SwaggerParser();
const defaultExport = SwaggerParser;

defaultExport.validate = (...args) => { return defaultInstance.validate(...args); };
defaultExport.dereference = (...args) => { return defaultInstance.dereference(...args); };
defaultExport.bundle = (...args) => { return defaultInstance.bundle(...args); };
defaultExport.parse = (...args) => { return defaultInstance.parse(...args); };
defaultExport.resolve = (...args) => { return defaultInstance.resolve(...args); };
defaultExport.validate = (...args) => {
const defaultInstance = new SwaggerParser();
return defaultInstance.validate(...args);
};
defaultExport.dereference = (...args) => {
const defaultInstance = new SwaggerParser();
return defaultInstance.dereference(...args);
};
defaultExport.bundle = (...args) => {
const defaultInstance = new SwaggerParser();
return defaultInstance.bundle(...args);
};
defaultExport.parse = (...args) => {
const defaultInstance = new SwaggerParser();
return defaultInstance.parse(...args);
};
defaultExport.resolve = (...args) => {
const defaultInstance = new SwaggerParser();
return defaultInstance.resolve(...args);
};
defaultExport.default = defaultExport;
defaultExport.SwaggerParser = defaultExport;

Expand Down
50 changes: 44 additions & 6 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
"use strict";

const $RefParserOptions = require("@apidevtools/json-schema-ref-parser/lib/options");
const { getJsonSchemaRefParserDefaultOptions} = require("@apidevtools/json-schema-ref-parser");
const schemaValidator = require("./validators/schema");
const specValidator = require("./validators/spec");
const util = require("util");

module.exports = ParserOptions;



/**
* Merges the properties of the source object into the target object.
*
* @param target - The object that we're populating
* @param source - The options that are being merged
* @returns
*/
function merge(target, source) {
if (isMergeable(source)) {
// prevent prototype pollution
const keys = Object.keys(source).filter((key) => !["__proto__", "constructor", "prototype"].includes(key));
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const sourceSetting = source[key];
const targetSetting = target[key];

if (isMergeable(sourceSetting)) {
// It's a nested object, so merge it recursively
target[key] = merge(targetSetting || {}, sourceSetting);
} else if (sourceSetting !== undefined) {
// It's a scalar value, function, or array. No merging necessary. Just overwrite the target value.
target[key] = sourceSetting;
}
}
}
return target;
}
/**
* Determines whether the given value can be merged,
* or if it is a scalar value that should just override the target value.
*
* @param val
* @returns
*/
function isMergeable(val) {
return val && typeof val === "object" && !Array.isArray(val) && !(val instanceof RegExp) && !(val instanceof Date);
}

/**
* Options that determine how Swagger APIs are parsed, resolved, dereferenced, and validated.
*
Expand All @@ -15,8 +54,9 @@ module.exports = ParserOptions;
* @augments $RefParserOptions
*/
function ParserOptions (_options) {
$RefParserOptions.call(this, ParserOptions.defaults);
$RefParserOptions.apply(this, arguments);
const defaultOptions = getJsonSchemaRefParserDefaultOptions();
const options = merge(defaultOptions, ParserOptions.defaults);
return merge(options, _options);
}

ParserOptions.defaults = {
Expand All @@ -31,5 +71,3 @@ ParserOptions.defaults = {
spec: specValidator,
},
};

util.inherits(ParserOptions, $RefParserOptions);
4 changes: 2 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use strict";

const util = require("util");
const url = require("@apidevtools/json-schema-ref-parser/lib/util/url");

exports.format = util.format;
exports.inherits = util.inherits;
const parse = (u) => new URL(u);

/**
* Regular Expression that matches Swagger path params.
Expand All @@ -27,7 +27,7 @@ const operationsList = ["get", "post", "put", "delete", "patch", "options", "hea
function fixServers (server, path) {
// Server url starting with "/" tells that it is not an http(s) url
if (server.url && server.url.startsWith("/")) {
const inUrl = url.parse(path);
const inUrl = parse(path);
const finalUrl = inUrl.protocol + "//" + inUrl.hostname + server.url;
server.url = finalUrl;
return server;
Expand Down
Loading

0 comments on commit eeb97de

Please sign in to comment.