Skip to content

Commit

Permalink
fix: infinity loop while deref if there's a recursive referenced defi…
Browse files Browse the repository at this point in the history
…nition in schema
  • Loading branch information
legends-killer committed Jan 31, 2024
1 parent 548ea53 commit cffee52
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yaje",
"version": "1.2.0",
"version": "1.2.1",
"author": "legends-killer <yyy@legends-killer.cq.cn>",
"description": "yet another json editor",
"keywords": [
Expand Down
13 changes: 11 additions & 2 deletions src/JsonEditor/helper/jsonSchemaProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: legends-killer
* @Date: 2023-12-27 21:58:54
* @LastEditors: legends-killer
* @LastEditTime: 2024-01-24 23:03:33
* @LastEditTime: 2024-01-31 23:31:10
* @Description:
*/

Expand All @@ -18,22 +18,31 @@ export interface ISuggestionItem {

export class JsonSchemaProcessor {
schema: any
deRefCache: Map<string, any>
constructor(schema: any) {
this.schema = schema
this.deRefCache = new Map()
}
private getSchemaByRef(ref: string) {
const refPath = ref.split('/')
const refName = refPath[refPath.length - 1]
return this.schema.definitions[refName] ?? {}
}

private deRef(currentDefinition: any) {
const properties = currentDefinition.properties
const res = {} as any
if (!properties) return res
Object.entries(properties).forEach((prop: any) => {
const [key, value] = prop
if (value.$ref?.length) {
res[key] = this.deRef(this.getSchemaByRef(value.$ref))
const cachedDeRefResult = this.deRefCache.get(value.$ref)
if (cachedDeRefResult) res[key] = cachedDeRefResult
else {
const deRefResult = this.deRef(this.getSchemaByRef(value.$ref))
res[key] = deRefResult
this.deRefCache.set(value.$ref, deRefResult)
}
} else {
res[key] = value
}
Expand Down

0 comments on commit cffee52

Please sign in to comment.