-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): introduce POC of dereference algorithm
- Loading branch information
Showing
11 changed files
with
165 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
apidom/packages/apidom-ns-openapi-3-1/src/traversal/visitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { propOr } from 'ramda'; | ||
import { | ||
Element, | ||
visit as astVisit, | ||
keyMap as keyMapBase, | ||
getNodeType as getNodeTypeBase, | ||
} from 'apidom'; | ||
import { isReferenceElement } from '../predicates'; | ||
|
||
export { BREAK } from 'apidom'; | ||
|
||
export const getNodeType = <T extends Element>(element: T): string | undefined => { | ||
return isReferenceElement(element) ? 'reference' : getNodeTypeBase(element); | ||
}; | ||
|
||
export const keyMapDefault = { | ||
...keyMapBase, | ||
reference: ['content'], | ||
}; | ||
|
||
// @ts-ignore | ||
export const visit = (root: Element, visitor, { keyMap = keyMapDefault, ...rest } = {}): void => { | ||
// if visitor is associated with the keymap, we prefer this visitor keymap | ||
const effectiveKeyMap = propOr(keyMap, 'keyMap', visitor); | ||
|
||
// @ts-ignore | ||
return astVisit(root, visitor, { | ||
// @ts-ignore | ||
keyMap: effectiveKeyMap, | ||
// @ts-ignore | ||
nodeTypeGetter: getNodeType, | ||
...rest, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import fs from 'fs'; | ||
import util from 'util'; | ||
import path from 'path'; | ||
import stampit from 'stampit'; | ||
import { hasIn } from 'ramda'; | ||
import { isNotUndefined } from 'ramda-adjunct'; | ||
import { transclude, toValue } from 'apidom'; | ||
import { visit, isReferenceElement } from 'apidom-ns-openapi-3-1'; | ||
// @ts-ignore | ||
import { parse } from 'apidom-parser-adapter-openapi-json-3-1'; | ||
import { evaluate, uriToPointer } from '../src/selectors/json-pointer'; | ||
|
||
const DereferenceVisitor = stampit({ | ||
props: { | ||
element: null, | ||
indirections: [], | ||
}, | ||
init({ element, indirections = [] }) { | ||
this.element = element; | ||
this.indirections = indirections; | ||
}, | ||
methods: { | ||
reference(element) { | ||
this.indirections.push(element); | ||
|
||
const jsonPointer = uriToPointer(element.$ref.toValue()); | ||
let fragment = evaluate(jsonPointer, this.element); | ||
|
||
// detect direct or circular reference | ||
if (this.indirections.includes(fragment)) { | ||
throw new Error('Recursive JSON Pointer detected'); | ||
} | ||
|
||
// follow the reference | ||
if (isReferenceElement(fragment)) { | ||
const innerReference = fragment; | ||
const visitor = DereferenceVisitor({ | ||
element: this.element, | ||
indirections: [...this.indirections, innerReference], | ||
}); | ||
visit(innerReference, visitor); | ||
|
||
fragment = evaluate(jsonPointer, this.element); | ||
} | ||
|
||
// override description and summary (outer has higher priority then inner) | ||
const hasDescription = isNotUndefined(element.description); | ||
const hasSummary = isNotUndefined(element.summary); | ||
if (hasDescription || hasSummary) { | ||
fragment = fragment.clone(); | ||
|
||
if (hasDescription && hasIn('description', fragment)) { | ||
// @ts-ignore | ||
fragment.description = element.description; | ||
} | ||
if (hasSummary && hasIn('summary', fragment)) { | ||
// @ts-ignore | ||
fragment.summary = element.summary; | ||
} | ||
} | ||
|
||
this.element = transclude(element, fragment, this.element); | ||
this.indirections.pop(); | ||
}, | ||
}, | ||
}); | ||
|
||
describe('dereference', function () { | ||
specify('should dereference', async function () { | ||
const fixturePath = path.join(__dirname, 'fixtures', 'dereference', 'reference-objects.json'); | ||
const source = fs.readFileSync(fixturePath).toString(); | ||
const parseResult = await parse(source); | ||
const { api } = parseResult; | ||
|
||
const visitor = DereferenceVisitor({ element: api }); | ||
visit(api, visitor); | ||
|
||
console.log(util.inspect(toValue(api), true, null, true)); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
apidom/packages/apidom-reference/test/fixtures/dereference/reference-objects.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"components": { | ||
"parameters": { | ||
"userId": { | ||
"$ref": "#/components/parameters/indirection", | ||
"description": "override" | ||
}, | ||
"indirection": { | ||
"$ref": "#/components/parameters/userIdRef", | ||
"summary": "indirect summary" | ||
}, | ||
"userIdRef": { | ||
"name": "userId", | ||
"in": "query", | ||
"description": "ID of the user", | ||
"required": true | ||
}, | ||
"ref": { | ||
"$ref": "#/components/parameters/userIdRef", | ||
"description": "another ref" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters