Skip to content

Commit e8a14d9

Browse files
committed
update getRefsByObject method
1 parent a4ad113 commit e8a14d9

File tree

1 file changed

+12
-36
lines changed

1 file changed

+12
-36
lines changed

src/swagger/OpenAPIService.ts

+12-36
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { OpenAPITypesGuard } from './OpenAPITypesGuard';
44
import { IOpenAPI3 } from './v3/open-api';
55
import { IOpenAPI3Operation } from './v3/operation';
66
import { IOpenAPI3Reference } from './v3/reference';
7-
import { IOpenAPI3AllOfSchema } from './v3/schemas/all-of-schema';
87
import { IOpenAPI3EnumSchema } from './v3/schemas/enum-schema';
98
import { IOpenAPI3ObjectSchema } from './v3/schemas/object-schema';
109
import { OpenAPI3Schema, OpenAPI3SchemaContainer } from './v3/schemas/schema';
@@ -201,17 +200,11 @@ export class OpenAPIService {
201200
refKeys.add(ref.$ref);
202201

203202
const schema = this.getSchemaByRef(ref);
204-
if (this.typesGuard.isObject(schema)) {
203+
if (schema) {
205204
const refsFromObject = this.getRefsByObject(schema, ref);
206205
const expanded = this.expandRefs(refsFromObject, refKeys);
207206
collectedRefs.push(...expanded);
208207
}
209-
210-
if (this.typesGuard.isAllOf(schema)) {
211-
const refsFromObject = this.getRefsByAllOf(schema, ref);
212-
const expanded = this.expandRefs(refsFromObject, refKeys);
213-
collectedRefs.push(...expanded);
214-
}
215208
});
216209

217210
return collectedRefs;
@@ -221,46 +214,29 @@ export class OpenAPIService {
221214
* @description Gets refs from object schema only one level down
222215
*/
223216
private getRefsByObject(
224-
object: IOpenAPI3ObjectSchema,
217+
schema: IOpenAPI3ObjectSchema | IOpenAPI3EnumSchema | OpenAPI3Schema,
225218
objectRef: IOpenAPI3Reference,
226219
outerRefs: IOpenAPI3Reference[] = []
227220
): IOpenAPI3Reference[] {
228221
const refs = outerRefs;
229222

230-
Object.values(object.properties || []).forEach((property) => {
231-
this.getRefsFromSchema(property)
232-
.filter((ref) => ref.$ref !== objectRef.$ref && !outerRefs.find((x) => x.$ref === ref.$ref))
233-
.forEach((ref) => {
234-
refs.push(ref);
235-
236-
if (this.typesGuard.isObject(property)) {
237-
this.getRefsByObject(property, objectRef, refs);
238-
}
239-
});
240-
});
223+
const properties: OpenAPI3Schema[] = [];
241224

242-
return refs;
243-
}
225+
if (this.typesGuard.isAllOf(schema)) {
226+
properties.push(...schema.allOf);
227+
}
244228

245-
/**
246-
* @description Gets refs from allof schema only one level down
247-
*/
248-
private getRefsByAllOf(
249-
object: IOpenAPI3AllOfSchema,
250-
objectRef: IOpenAPI3Reference,
251-
outerRefs: IOpenAPI3Reference[] = []
252-
): IOpenAPI3Reference[] {
253-
const refs = outerRefs;
229+
if (this.typesGuard.isObject(schema)) {
230+
properties.push(...Object.values(schema.properties || []));
231+
}
254232

255-
Object.values(object.allOf || []).forEach((property) => {
233+
properties.forEach((property) => {
256234
this.getRefsFromSchema(property)
257235
.filter((ref) => ref.$ref !== objectRef.$ref && !outerRefs.find((x) => x.$ref === ref.$ref))
258236
.forEach((ref) => {
259237
refs.push(ref);
260-
261-
if (this.typesGuard.isObject(property)) {
262-
this.getRefsByObject(property, objectRef, refs);
263-
}
238+
this.getRefsByObject(property, objectRef, refs);
239+
this.getRefsByObject(property, objectRef, refs);
264240
});
265241
});
266242

0 commit comments

Comments
 (0)