Skip to content

Commit

Permalink
feat: add OpenAPI 3.1.0 dereference strategy (#2740)
Browse files Browse the repository at this point in the history
Refs #2717
  • Loading branch information
char0n committed Jan 23, 2023
1 parent 485bb01 commit 945bf92
Show file tree
Hide file tree
Showing 329 changed files with 8,429 additions and 80 deletions.
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@
"import/no-unresolved": [
2,
{
"ignore": ["^@swagger-api/apidom-reference/configuration/empty$"]
"ignore": [
"^@swagger-api/apidom-reference/configuration/empty$",
"^@swagger-api/apidom-reference/dereference/strategies/openapi-3-1$",
"^@swagger-api/apidom-reference/dereference/strategies/openapi-3-1/selectors/\\$anchor$",
"^@swagger-api/apidom-reference/dereference/strategies/openapi-3-1/selectors/uri$",
"^@swagger-api/apidom-reference/resolve/resolvers/file$",
"^@swagger-api/apidom-reference/resolve/strategies/openapi-3-1$",
"^@swagger-api/apidom-reference/parse/parsers/binary$"
]
}
],
"prettier/prettier": "error",
Expand Down
2 changes: 2 additions & 0 deletions config/jest/jest.unit.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ module.exports = {
'<rootDir>/test/jest.setup.js',
'<rootDir>/test/specmap/data/',
'<rootDir>/test/build-artifacts/',
'/__fixtures__/',
'/__utils__/',
],
};
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"build:es": "cross-env BABEL_ENV=es babel src --out-dir es",
"lint": "eslint src/ test/",
"lint:fix": "npm run lint -- --fix",
"link:apidom": "npm link @swagger-api/apidom-core @swagger-api/apidom-reference @swagger-api/apidom-ns-openapi-3-1 @swagger-api/apidom-ns-openapi-3-0 @swagger-api/apidom-ns-json-schema-draft-4 @swagger-api/apidom-json-pointer",
"test": "run-s test:unit:coverage test:artifact",
"test:unit": "cross-env BABEL_ENV=commonjs jest --runInBand --config ./config/jest/jest.unit.config.js",
"test:unit:coverage": "cross-env BABEL_ENV=commonjs jest --runInBand --config ./config/jest/jest.unit.coverage.config.js",
Expand Down Expand Up @@ -110,9 +111,10 @@
},
"dependencies": {
"@babel/runtime-corejs3": "^7.11.2",
"@swagger-api/apidom-core": "^0.60.0",
"@swagger-api/apidom-reference": "^0.60.0",
"@swagger-api/apidom-ns-openapi-3-1": "^0.60.0",
"@swagger-api/apidom-core": "^0.61.0",
"@swagger-api/apidom-reference": "^0.61.0",
"@swagger-api/apidom-ns-openapi-3-1": "^0.61.0",
"@swagger-api/apidom-json-pointer": "^0.61.0",
"cookie": "~0.5.0",
"cross-fetch": "^3.1.5",
"deepmerge": "~4.2.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-disable camelcase */
import { createNamespace, visit } from '@swagger-api/apidom-core';
import { ReferenceSet, Reference } from '@swagger-api/apidom-reference/configuration/empty';
import OpenApi3_1DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/openapi-3-1';
import openApi3_1Namespace, { getNodeType, keyMap } from '@swagger-api/apidom-ns-openapi-3-1';

import OpenApi3_1SwaggerClientDereferenceVisitor from './visitor.js';

const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];

const OpenApi3_1SwaggerClientDereferenceStrategy = OpenApi3_1DereferenceStrategy.compose({
props: {
useCircularStructures: true,
allowMetaPatches: false,
},
init({
useCircularStructures = this.useCircularStructures,
allowMetaPatches = this.allowMetaPatches,
} = {}) {
this.name = 'openapi-3-1-swagger-client';
this.useCircularStructures = useCircularStructures;
this.allowMetaPatches = allowMetaPatches;
},
methods: {
async dereference(file, options) {
const namespace = createNamespace(openApi3_1Namespace);
const refSet = options.dereference.refSet ?? ReferenceSet();
let reference;

if (!refSet.has(file.uri)) {
reference = Reference({ uri: file.uri, value: file.parseResult });
refSet.add(reference);
} else {
// pre-computed refSet was provided as configuration option
reference = refSet.find((ref) => ref.uri === file.uri);
}

const visitor = OpenApi3_1SwaggerClientDereferenceVisitor({
reference,
namespace,
options,
useCircularStructures: this.useCircularStructures,
allowMetaPatches: this.allowMetaPatches,
});
const dereferencedElement = await visitAsync(refSet.rootRef.value, visitor, {
keyMap,
nodeTypeGetter: getNodeType,
});

/**
* Release all memory if this refSet was not provided as a configuration option.
* If provided as configuration option, then provider is responsible for cleanup.
*/
if (options.dereference.refSet === null) {
refSet.clean();
}

return dereferencedElement;
},
},
});

export default OpenApi3_1SwaggerClientDereferenceStrategy;
/* eslint-enable camelcase */
Loading

0 comments on commit 945bf92

Please sign in to comment.