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 e141d82
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 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
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import $RefParser from '@apidevtools/json-schema-ref-parser';
import { JSONPath } from 'jsonpath-plus';
import { merge } from 'lodash';

import type { $Refs } from '@apidevtools/json-schema-ref-parser';
import $Refs from '@apidevtools/json-schema-ref-parser/dist/lib/refs';
import type { AsyncAPIObject, ComponentsObject, MessageObject } from './spec-types';

/**
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) => {
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('#/');
let inlineAsyncapiDocumentPath = inlineAsyncapiDocumentURI[0];
let inlineAsyncapiDocumentPointer = inlineAsyncapiDocumentURI[1];

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

inlineAsyncapiDocument =
inlineAsyncapiDocument[inlineAsyncapiDocumentPointer];

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

0 comments on commit e141d82

Please sign in to comment.