Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #56 Cannot read property 'name' of undefined when ApiModelsProperties are circular references. #58

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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