Skip to content

Commit

Permalink
Added fix for issue olivierlsc#56 circular reference causing cannot r…
Browse files Browse the repository at this point in the history
…ead property name of undefined.
  • Loading branch information
rnunleyaoa committed Jan 6, 2020
1 parent 009f3ec commit 8859d2a
Show file tree
Hide file tree
Showing 7 changed files with 5,470 additions and 8,811 deletions.
38 changes: 27 additions & 11 deletions lib/swagger-express-ts-lib/src/api-model-property.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,32 @@ export function ApiModelProperty(
args?: IApiModelPropertyArgs
): PropertyDecorator {
return (target: any, propertyKey: string | symbol) => {
const propertyType = Reflect.getMetadata(
'design:type',
target,
propertyKey
).name;
SwaggerService.getInstance().addApiModelProperty(
args,
target,
propertyKey,
propertyType
);
let propertyType = '';

if (typeof args.itemType !== 'undefined' && args.itemType !== null) {
propertyType = args.itemType;
} else {
try {
propertyType = Reflect.getMetadata(
'design:type',
target,
propertyKey
).name;
} catch (err) {
if (err.message === "Cannot read property 'name' of undefined") {
throw new Error(
`${err.message}. This usually occours due to a circular reference between models. A possible solution is to set the itemType argument of the @ApiModelProperty to a string that matches the class name of the field type. The field in question is named ${String(propertyKey)}.`);
} else {
throw err;
}
}
}

SwaggerService.getInstance().addApiModelProperty(
args,
target,
propertyKey,
propertyType
);
};
}
Loading

0 comments on commit 8859d2a

Please sign in to comment.