Skip to content

Commit

Permalink
feat: add x-origin property
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet committed Feb 16, 2024
1 parent a87a211 commit 30a0f3c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^9.1.2",
"@types/json-schema": "^7.0.11",
"axios": "^1.6.7",
"js-yaml": "^4.1.0",
"jsonpath-plus": "^6.0.1",
"lodash": "^4.17.21"
Expand Down
36 changes: 34 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fs from 'fs';
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { cloneDeep } from 'lodash';
import axios from 'axios';
import { cloneDeep, merge } from 'lodash';
import yaml from 'js-yaml';
import { parse, isExternalReference } from './parser';
import { ParserError } from './errors';
Expand Down Expand Up @@ -105,15 +107,45 @@ export function versionCheck(asyncapiDocuments: AsyncAPIObject[]): number {
export function addXOrigins(asyncapiDocument: AsyncAPIObject) {
// VALUE from 'asyncapiDocument' becomes KEY for the
// underlying and recursive functions
Object.values(asyncapiDocument).forEach((key: any) => {
Object.values(asyncapiDocument).forEach(async (key: any) => {

Check warning on line 110 in src/util.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed
if (typeof key === 'object' && key !== '$ref') {
if (Object.keys(key).indexOf('$ref') !== -1) {
if (isExternalReference(key['$ref'])) {
key['x-origin'] = key['$ref'];

// If an external `$ref` is found, the function goes into
// second-level recursion to see if there are more `$ref`s whose
// values need to be copied to the `x-origin` properties of the
// `$ref`ed file.
// If an external `$ref` is found again, the function goes into the
// third-level recursion, and so on, until it reaches a file that
// contains no external `$ref`s at all.
// Then it exits all the way up in the opposite direction.

let inlineAsyncapiDocumentURI = key['$ref'].split('#/');

Check failure on line 125 in src/util.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

'inlineAsyncapiDocumentURI' is never reassigned. Use 'const' instead
let inlineAsyncapiDocumentPath = inlineAsyncapiDocumentURI[0];

Check failure on line 126 in src/util.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

'inlineAsyncapiDocumentPath' is never reassigned. Use 'const' instead
let inlineAsyncapiDocumentPointer = inlineAsyncapiDocumentURI[1];

Check failure on line 127 in src/util.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

'inlineAsyncapiDocumentPointer' is never reassigned. Use 'const' instead

let inlineAsyncapiDocument = inlineAsyncapiDocumentPath.startsWith(
'http'
)
? yaml.load(await axios(inlineAsyncapiDocumentPath))
: (yaml.load(
fs.readFileSync(inlineAsyncapiDocumentPath, 'utf-8')

Check failure on line 134 in src/util.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Expected indentation of 14 spaces but found 16

Check warning on line 134 in src/util.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Found fs.readFileSync with non literal argument at index 0
) as any);

Check failure on line 135 in src/util.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Expected indentation of 12 spaces but found 14

inlineAsyncapiDocument =
inlineAsyncapiDocument[inlineAsyncapiDocumentPointer];

Check warning on line 138 in src/util.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Generic Object Injection Sink

if (inlineAsyncapiDocument) {
addXOrigins(inlineAsyncapiDocument as AsyncAPIObject);
merge(key, inlineAsyncapiDocument);
}
}
} else {
addXOrigins(key);
}
}
});
return asyncapiDocument;
}

0 comments on commit 30a0f3c

Please sign in to comment.