Skip to content

Commit

Permalink
fix(models): recursive model definitions filter
Browse files Browse the repository at this point in the history
Filter for already parsed definitions. Otherwise when generating tags, recursive model definitions would get stuck in loop.

Signed-off-by: Vojtech Mašek <[email protected]>
  • Loading branch information
vmasek committed Sep 1, 2018
1 parent b3aa275 commit c80631f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,20 @@ function parseDefinitions(
];

if (methods) {
const filterByName = (name: string): Definition[] => {
const namedDefs = allDefs.filter(def => def.name === name);
const filterByName = (defName: string, parentDefs: Definition[] = []): Definition[] => {
const namedDefs = allDefs.filter(({name}) => name === defName);
return namedDefs
.reduce<Definition[]>(
(acc, def) => [
...acc,
...def.properties
.filter(prop => prop.typescriptType && prop.isRef)
.reduce<Definition[]>((a, prop) => [...a, ...filterByName(prop.typescriptType!)], [])
.reduce<Definition[]>(
(a, prop) => parentDefs.some(({name}) => name === prop.typescriptType)
? a // do not parse if type def is already in parsed definitions
: [...a, ...filterByName(prop.typescriptType!, namedDefs)],
[]
)
],
namedDefs
);
Expand All @@ -163,9 +168,9 @@ function parseDefinitions(
],
[]
);
} else {
return allDefs;
}

return allDefs;
}

function defineEnumOrInterface(key: string, definition: Schema | ExtendedParameter): Definition {
Expand Down
1 change: 1 addition & 0 deletions tests/custom/api/models/model.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export interface Model {
created: number;
deleted: number;
id: string;
recursivePointer: Model;
updated: number;
}
2 changes: 2 additions & 0 deletions tests/custom/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ definitions:
type: "integer"
deleted:
type: "integer"
recursivePointer:
$ref: '#/definitions/model'
customer:
type: "object"
allOf:
Expand Down

0 comments on commit c80631f

Please sign in to comment.