-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle complex circular reference cases (#671)
- Loading branch information
1 parent
4f18144
commit d9c3be0
Showing
14 changed files
with
251 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
import { applyTraitsV2 } from './apply-traits'; | ||
import { checkCircularRefs } from './check-circular-refs'; | ||
import { resolveCircularRefs } from './resolve-circular-refs'; | ||
import { parseSchemasV2 } from './parse-schema'; | ||
import { anonymousNaming } from './anonymous-naming'; | ||
|
||
import type { RulesetFunctionContext } from '@stoplight/spectral-core'; | ||
import type { Parser } from '../parser'; | ||
import type { ParseOptions } from '../parse'; | ||
import type { AsyncAPIDocumentInterface } from '../models'; | ||
import type { DetailedAsyncAPI } from '../types'; | ||
|
||
export async function customOperations(parser: Parser, document: AsyncAPIDocumentInterface, detailed: DetailedAsyncAPI, options: ParseOptions): Promise<void> { | ||
export async function customOperations(parser: Parser, document: AsyncAPIDocumentInterface, detailed: DetailedAsyncAPI, inventory: RulesetFunctionContext['documentInventory'], options: ParseOptions): Promise<void> { | ||
switch (detailed.semver.major) { | ||
case 2: return operationsV2(parser, document, detailed, options); | ||
case 2: return operationsV2(parser, document, detailed, inventory, options); | ||
// case 3: return operationsV3(parser, document, detailed, options); | ||
} | ||
} | ||
|
||
async function operationsV2(parser: Parser, document: AsyncAPIDocumentInterface, detailed: DetailedAsyncAPI, options: ParseOptions): Promise<void> { | ||
async function operationsV2(parser: Parser, document: AsyncAPIDocumentInterface, detailed: DetailedAsyncAPI, inventory: RulesetFunctionContext['documentInventory'], options: ParseOptions): Promise<void> { | ||
if (options.applyTraits) { | ||
applyTraitsV2(detailed.parsed); | ||
} | ||
if (options.parseSchemas) { | ||
await parseSchemasV2(parser, detailed); | ||
} | ||
|
||
// anonymous naming and checking circular refrences should be done after custom schemas parsing | ||
checkCircularRefs(document); | ||
// anonymous naming and resolving circular refrences should be done after custom schemas parsing | ||
if (inventory) { | ||
resolveCircularRefs(document, inventory); | ||
} | ||
anonymousNaming(document); | ||
} | ||
|
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,67 @@ | ||
import { setExtension, toJSONPathArray, retrieveDeepData, findSubArrayIndex } from '../utils'; | ||
import { xParserCircular } from '../constants'; | ||
|
||
import type { RulesetFunctionContext } from '@stoplight/spectral-core'; | ||
import type { AsyncAPIDocumentInterface } from '../models'; | ||
import type { AsyncAPIObject } from '../types'; | ||
|
||
interface Context { | ||
document: AsyncAPIObject; | ||
hasCircular: boolean; | ||
inventory: RulesetFunctionContext['documentInventory']; | ||
visited: Set<any>; | ||
} | ||
|
||
export function resolveCircularRefs(document: AsyncAPIDocumentInterface, inventory: RulesetFunctionContext['documentInventory']) { | ||
const documentJson = document.json(); | ||
const ctx: Context = { document: documentJson, hasCircular: false, inventory, visited: new Set() }; | ||
traverse(documentJson, [], null, '', ctx); | ||
if (ctx.hasCircular) { | ||
setExtension(xParserCircular, true, document); | ||
} | ||
} | ||
|
||
function traverse(data: any, path: Array<string | number>, parent: any, property: string | number, ctx: Context) { | ||
if (typeof data !== 'object' || !data || ctx.visited.has(data)) { | ||
return; | ||
} | ||
|
||
ctx.visited.add(data); | ||
if (Array.isArray(data)) { | ||
data.forEach((item, idx) => traverse(item, [...path, idx], data, idx, ctx)); | ||
} | ||
if ('$ref' in data) { | ||
ctx.hasCircular = true; | ||
const resolvedRef = retrieveCircularRef(data, path, ctx); | ||
if (resolvedRef) { | ||
parent[property] = resolvedRef; | ||
} | ||
} else { | ||
for (const p in data) { | ||
traverse(data[p], [...path, p], data, p, ctx); | ||
} | ||
} | ||
ctx.visited.delete(data); | ||
} | ||
|
||
function retrieveCircularRef(data: { $ref: string }, path: Array<string | number>, ctx: Context): any { | ||
const $refPath = toJSONPathArray(data.$ref); | ||
const item = ctx.inventory.findAssociatedItemForPath(path, true); | ||
|
||
// root document case | ||
if (item === null) { | ||
return retrieveDeepData(ctx.document, $refPath); | ||
} | ||
|
||
// referenced document case | ||
if (item) { | ||
const subArrayIndex = findSubArrayIndex(path, $refPath); | ||
let dataPath: Array<string | number> | undefined; | ||
if (subArrayIndex === -1) { // create subarray based on location of the assiociated document - use item.path | ||
dataPath = [...path.slice(0, path.length - item.path.length), ...$refPath]; | ||
} else { // create subarray based on $refPath | ||
dataPath = path.slice(0, subArrayIndex + $refPath.length); | ||
} | ||
return retrieveDeepData(ctx.document, dataPath); | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.