1

I'm using this wonderful lib: https://www.npmjs.com/package/ts-mixer

The whole traceback looks as follows:


(node:22654) UnhandledPromiseRejectionWarning: Error: A circular dependency has been detected (property key: "firstName"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
    at SchemaObjectFactory.createNotBuiltInTypeReference (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:212:19)
    at SchemaObjectFactory.mergePropertyWithMetadata (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:143:25)
    at /Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:79:35
    at Array.map (<anonymous>)
    at SchemaObjectFactory.extractPropertiesFromType (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:78:52)
    at SchemaObjectFactory.exploreModelSchema (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:92:41)
    at /Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:33:36
    at Array.map (<anonymous>)
    at SchemaObjectFactory.createFromModel (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:20:45)
    at exploreApiParametersMetadata (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/explorers/api-parameters.explorer.js:33:55)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:22654) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:22654) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

register-common-user.dto.ts

export class RegisterCommonUserDto {
    @decorate(ApiProperty())
    @decorate(IsNotEmpty())
    @decorate(IsString())
    firstName: string;

    // @decorate(ApiProperty())
    @decorate(IsNotEmpty())
    @decorate(IsString())
    lastName: string;

    // @decorate(ApiProperty({enum: EUserRoleName}))
    @decorate(IsNotEmpty())
    @decorate(IsEnum(EUserRoleName))
    roleName: EUserRoleName;

    // @decorate(ApiProperty())
    @decorate(IsNotEmpty())
    @decorate(IsPhoneNumber())
    phoneNumber: string;

    // @decorate(ApiProperty())
    @decorate(IsNotEmpty())
    @decorate(IsEmail())
    email: string;
}

register-user.dto.ts

export class RegisterUserDto extends Mixin(
    RegisterFighterDto,
    RegisterCommonUserDto,
    RegisterLocationProviderDto,
) {}

The method:

    @Post('register')
    public async registerUser(@Body() registerUserDto: RegisterUserDto): Promise<any> {
        // code
    }

What might be causing the problem? It has surely something to do with the @ApiProperty decorator being wrapped in decorate, cos when I comment out that line, the error disappears. But in that case decorators don't get inherited and that's the whole point of using the ts-mixer lib.

EDIT:

Any ideas?

EDIT:

Are these libs incompatible or what?

Albert
  • 2,146
  • 10
  • 32
  • 54

1 Answers1

3

Recently, I learning Nest also have the same problem when attempt to use Swagger. This is my whole traceback looks as follows:

Error: A circular dependency has been detected (property key: "user"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
at SchemaObjectFactory.createNotBuiltInTypeReference (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:170:19)
at SchemaObjectFactory.createSchemaMetadata (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:277:25)
at SchemaObjectFactory.mergePropertyWithMetadata (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:122:21)
at /Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:79:35
at Array.map (<anonymous>)
at SchemaObjectFactory.extractPropertiesFromType (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:78:52)
at SchemaObjectFactory.exploreModelSchema (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:92:41)
at /Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/swagger-explorer.js:217:64
at Array.forEach (<anonymous>)
at SwaggerExplorer.registerExtraModels (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/swagger-explorer.js:217:21)

Analyze the error code information,I found the problem is the decorator ApiExtraModels(), and the code I wrote is:

import { applyDecorators, Type } from '@nestjs/common';
import { ApiOkResponse, ApiExtraModels, getSchemaPath } from '@nestjs/swagger';
import { ApiUserResDto, PaginatedDto } from './dto/api-res.dto';

/** 用户信息返回限定装饰器 */
export const ApiUserResponse = <TModel extends Type<any>>(model: TModel) => {
  return applyDecorators(
    ApiOkResponse({
      schema: {
        title: `UserResponseOf${model.name}`,
        allOf: [
          { $ref: getSchemaPath(ApiUserResDto) },
          {
            properties: {
              user: {
                type: 'object',
                $ref: getSchemaPath(model)
              }
            }
          }
        ]
      }
    }),
    ApiExtraModels(ApiUserResDto),
    ApiExtraModels(model)
  );
};`

and the apiUserResDto class is:

export class ApiUserResDto<Dto> {
  user: Dto;
}

according to the cli tips, just change ApiExtraModels(ApiUserResDto) to ApiExtraModels(() => ApiUserResDto), should solve this problem.

jenemy
  • 91
  • 3
  • Good work! It's tricky to find the source because the error only shows the property key. Another author also showed that it's possible to have these circular errors even when [no actual classes are involved](https://stackoverflow.com/a/68578794/10779988) – Looi May 08 '23 at 03:46