Skip to content

Commit e5502a0

Browse files
committed
Handle recursion
1 parent 13b005a commit e5502a0

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/openapi/SchemaExample.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import {RefResolver} from "./RefResolver";
22
import {OpenAPIV3} from "openapi-types";
33

4-
export class SchemaExample {
5-
private resolver: RefResolver;
6-
7-
constructor(doc: any) {
8-
this.resolver = new RefResolver(doc);
4+
class SchemaExampleBuilder {
5+
constructor(private resolver: RefResolver) {
96
}
107

11-
extractExample(schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject): any {
8+
build(schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject): any {
129
schema = this.resolver.resolve(schema)
1310
if ('oneOf' in schema) {
14-
return this.extractExample(schema.oneOf!![0]);
11+
return this.build(schema.oneOf!![0]);
1512
}
1613
if ('allOf' in schema) {
17-
const examples = schema.allOf!!.map((s) => this.extractExample(s));
14+
const examples = schema.allOf!!.map((s) => this.build(s));
1815
return Object.assign({}, ...examples);
1916
}
2017
if (schema.example !== undefined) {
@@ -26,13 +23,25 @@ export class SchemaExample {
2623
if (schema.properties) {
2724
const obj: any = {};
2825
for (const key in schema.properties) {
29-
obj[key] = this.extractExample(schema.properties[key]);
26+
obj[key] = this.build(schema.properties[key]);
3027
}
3128
return obj;
3229
}
3330
if ('items' in schema && schema.items) {
34-
return [this.extractExample(schema.items)];
31+
return [this.build(schema.items)];
3532
}
3633
return undefined;
3734
}
3835
}
36+
37+
export class SchemaExample {
38+
private resolver: RefResolver;
39+
40+
constructor(doc: any) {
41+
this.resolver = new RefResolver(doc);
42+
}
43+
44+
extractExample(schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject): any {
45+
return new SchemaExampleBuilder(this.resolver).build(schema);
46+
}
47+
}

0 commit comments

Comments
 (0)