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 8dd737d
Show file tree
Hide file tree
Showing 7 changed files with 5,464 additions and 8,805 deletions.
26 changes: 21 additions & 5 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,11 +54,27 @@ export function ApiModelProperty(
args?: IApiModelPropertyArgs
): PropertyDecorator {
return (target: any, propertyKey: string | symbol) => {
const propertyType = Reflect.getMetadata(
'design:type',
target,
propertyKey
).name;
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,
Expand Down
Loading

0 comments on commit 8dd737d

Please sign in to comment.