Skip to content

Commit e7f2e5d

Browse files
authoredNov 30, 2023
fix: freeze problem when $ref is deeply nested (#1083)
1 parent ff2b7d3 commit e7f2e5d

File tree

3 files changed

+149
-1
lines changed

3 files changed

+149
-1
lines changed
 

‎packages/core/src/resolvers/object.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ContextSpecs, ResolverValue } from '../types';
44
import { jsDoc } from '../utils';
55
import { resolveValue } from './value';
66

7-
export const resolveObject = ({
7+
const resolveObjectOriginal = ({
88
schema,
99
propName,
1010
combined = false,
@@ -76,3 +76,39 @@ export const resolveObject = ({
7676

7777
return resolvedValue;
7878
};
79+
80+
const resolveObjectCacheMap = new Map<string, ResolverValue>();
81+
82+
export const resolveObject = ({
83+
schema,
84+
propName,
85+
combined = false,
86+
context,
87+
}: {
88+
schema: SchemaObject | ReferenceObject;
89+
propName?: string;
90+
combined?: boolean;
91+
context: ContextSpecs;
92+
}): ResolverValue => {
93+
const hashKey = JSON.stringify({
94+
schema,
95+
propName,
96+
combined,
97+
specKey: context.specKey,
98+
});
99+
100+
if (resolveObjectCacheMap.has(hashKey)) {
101+
return resolveObjectCacheMap.get(hashKey)!;
102+
}
103+
104+
const result = resolveObjectOriginal({
105+
schema,
106+
propName,
107+
combined,
108+
context,
109+
});
110+
111+
resolveObjectCacheMap.set(hashKey, result);
112+
113+
return result;
114+
};

‎tests/configs/default.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,11 @@ export default defineConfig({
8787
mock: true,
8888
},
8989
},
90+
'deeply-nested-refs': {
91+
input: '../specifications/deeply-nested-refs.yaml',
92+
output: {
93+
schemas: '../generated/default/deeply-nested-refs/model',
94+
target: '../generated/default/deeply-nested-refs/endpoints.ts',
95+
},
96+
},
9097
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Deeply nested refs
4+
description: ''
5+
version: 1.0.0
6+
paths:
7+
/deeply-nested:
8+
get:
9+
summary: deeply nested
10+
responses:
11+
'200':
12+
description: OK
13+
content:
14+
application/json:
15+
schema:
16+
$ref: '#/components/schemas/DeeplyNestedRefSchema1'
17+
components:
18+
schemas:
19+
DeeplyNestedRefSchema1:
20+
type: object
21+
properties:
22+
item1:
23+
type: array
24+
items:
25+
allOf:
26+
- $ref: '#/components/schemas/DeeplyNestedRefSchema1'
27+
item2:
28+
type: array
29+
items:
30+
allOf:
31+
- $ref: '#/components/schemas/DeeplyNestedRefSchema2'
32+
item_all:
33+
type: array
34+
items:
35+
allOf:
36+
- $ref: '#/components/schemas/DeeplyNestedRefSchema1'
37+
- $ref: '#/components/schemas/DeeplyNestedRefSchema2'
38+
- $ref: '#/components/schemas/DeeplyNestedRefSchema3'
39+
- $ref: '#/components/schemas/DeeplyNestedRefSchema4'
40+
- $ref: '#/components/schemas/DeeplyNestedRefSchema5'
41+
DeeplyNestedRefSchema2:
42+
type: object
43+
properties:
44+
item3:
45+
type: object
46+
allOf:
47+
- $ref: '#/components/schemas/DeeplyNestedRefSchema3'
48+
item_all:
49+
type: array
50+
items:
51+
allOf:
52+
- $ref: '#/components/schemas/DeeplyNestedRefSchema1'
53+
- $ref: '#/components/schemas/DeeplyNestedRefSchema2'
54+
- $ref: '#/components/schemas/DeeplyNestedRefSchema3'
55+
- $ref: '#/components/schemas/DeeplyNestedRefSchema4'
56+
- $ref: '#/components/schemas/DeeplyNestedRefSchema5'
57+
DeeplyNestedRefSchema3:
58+
type: object
59+
properties:
60+
item4:
61+
type: object
62+
allOf:
63+
- $ref: '#/components/schemas/DeeplyNestedRefSchema4'
64+
item_all:
65+
type: array
66+
items:
67+
allOf:
68+
- $ref: '#/components/schemas/DeeplyNestedRefSchema1'
69+
- $ref: '#/components/schemas/DeeplyNestedRefSchema2'
70+
- $ref: '#/components/schemas/DeeplyNestedRefSchema3'
71+
- $ref: '#/components/schemas/DeeplyNestedRefSchema4'
72+
- $ref: '#/components/schemas/DeeplyNestedRefSchema5'
73+
DeeplyNestedRefSchema4:
74+
type: object
75+
properties:
76+
item5:
77+
type: object
78+
allOf:
79+
- $ref: '#/components/schemas/DeeplyNestedRefSchema5'
80+
item_all:
81+
type: array
82+
items:
83+
allOf:
84+
- $ref: '#/components/schemas/DeeplyNestedRefSchema1'
85+
- $ref: '#/components/schemas/DeeplyNestedRefSchema2'
86+
- $ref: '#/components/schemas/DeeplyNestedRefSchema3'
87+
- $ref: '#/components/schemas/DeeplyNestedRefSchema4'
88+
- $ref: '#/components/schemas/DeeplyNestedRefSchema5'
89+
DeeplyNestedRefSchema5:
90+
type: object
91+
properties:
92+
item1:
93+
type: array
94+
items:
95+
allOf:
96+
- $ref: '#/components/schemas/DeeplyNestedRefSchema1'
97+
item_all:
98+
type: array
99+
items:
100+
allOf:
101+
- $ref: '#/components/schemas/DeeplyNestedRefSchema1'
102+
- $ref: '#/components/schemas/DeeplyNestedRefSchema2'
103+
- $ref: '#/components/schemas/DeeplyNestedRefSchema3'
104+
- $ref: '#/components/schemas/DeeplyNestedRefSchema4'
105+
- $ref: '#/components/schemas/DeeplyNestedRefSchema5'

0 commit comments

Comments
 (0)
Please sign in to comment.