question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swagger plugin type Date throws circular dependency error

See original GitHub issue

I’m submitting a…


[ ] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

The new Swagger plugin https://docs.nestjs.com/recipes/swagger does not correctly work with type Date. It returns the following error:

(node:2603) UnhandledPromiseRejectionWarning: Error: A circular dependency has been detected (property key: "createdOn"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
    at SchemaObjectFactory.createNotBuiltInTypeReference (/Users/skgu/Projects/NestCliTest/wtf/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:107:19)
    at SchemaObjectFactory.mergePropertyWithMetadata (/Users/skgu/Projects/NestCliTest/wtf/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:88:25)
    at /Users/skgu/Projects/NestCliTest/wtf/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:55:35
    at Array.map (<anonymous>)
    at SchemaObjectFactory.exploreModelSchema (/Users/skgu/Projects/NestCliTest/wtf/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:54:52)
    at ResponseObjectFactory.create (/Users/skgu/Projects/NestCliTest/wtf/node_modules/@nestjs/swagger/dist/services/response-object-factory.js:45:47)
    at /Users/skgu/Projects/NestCliTest/wtf/node_modules/@nestjs/swagger/dist/explorers/api-response.explorer.js:50:96
    at /Users/skgu/Projects/NestCliTest/wtf/node_modules/lodash/lodash.js:13401:38
    at /Users/skgu/Projects/NestCliTest/wtf/node_modules/lodash/lodash.js:4905:15
    at baseForOwn (/Users/skgu/Projects/NestCliTest/wtf/node_modules/lodash/lodash.js:2990:24)
(node:2603) 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(). (rejection id: 1)
(node:2603) [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.

Using ApiProperty still gives the error. If I put @IsDate() then the error disappears. Without the Date the schema is generated correctly.

Expected behavior

it should not give a circular dependency error.

Minimal reproduction of the problem with instructions

Create CLI project and add swagger plugin in compile options:

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "plugins": [
      {
        "name": "@nestjs/swagger/plugin",
        "options": {
          "classValidatorShim": false
        }
      }
    ]
  }
}

Next create a DTO with a property of type Date:

export class HelloDto {
  public createdOn: Date;
}

Run app

npm start

this should result in an error.

What is the motivation / use case for changing the behavior?

Environment


```
  "dependencies": {
    "@nestjs/common": "^6.7.2",
    "@nestjs/core": "^6.7.2",
    "@nestjs/platform-express": "^6.7.2",
    "@nestjs/swagger": "4.1.2",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.0",
    "rxjs": "^6.5.3",
    "swagger-ui-express": "^4.1.2"
  },
```

Others:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
anotherpitcommented, Jan 5, 2020

Seems like it got broken in this commit: https://github.com/nestjs/swagger/commit/321ff913083c66f3d385d5ed3c4933201ad97b7e

Specifically, on this line there should be

...existingMetadata

not

existingMetadata

Downgrade to version 4.0.9 (not ~4.0.9, but exactly 4.0.9) fixed the issue for me

0reactions
kamilmysliwieccommented, Jan 8, 2020

Problem described in this issue seems to be resolved:

image

@treffynnon if you’re still seeing errors, please, create a separate issue with a repository that reproduces your problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Circular Dependency with Nestjs Swagger 4 - Stack Overflow
The solution that worked for me was to declare in @ApiProperty() the type with arrow function, like below: @Entity() export class Job {...
Read more >
Swagger Parser - API Dev Tools
Swagger 2.0 and OpenAPI 3.0 parser/validator. ... Then, if a circular reference is found, a ReferenceError will be thrown.
Read more >
Common errors - FAQ - A progressive Node.js framework
Circular dependencies can arise from both providers depending on each other, or typescript files depending on each other for constants, such as exporting ......
Read more >
Springfox Reference Documentation - GitHub Pages
Usually this happens when we add a new contract test for a bug we've fixed or a ... form: webjars/springfox-swagger-ui/3.0.0/swagger-ui.html.
Read more >
Release Notes: 3.0.0 - OpenAPI Generator
9c79297d6 [MAVEN PLUGIN] Use latest version in dependencies snippet ... edf6be8c0 [Ada] bug fix by defining x-is-model-type when property is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found