NestJS: Weird swagger request body loading problem because of the circular dependency

Mümin Celal Pinar
2 min readJan 22, 2021
Photo by Unsplash

JavaScript is an interesting language. Sometimes, a code block/line that gives same output in other languages like C#, Java etc. doesn’t give same output in JavaScript. Welcome the JavaScript world.

Today JavaScript environment has a lot of frameworks. Each of them is solving one problem. Some of them has huge impact, some of them has not. NestJS is the framework has huge impact in JavaScript world. It is used to develop back-end applications and inspired by Angular.

When I was developing and back-end application with NestJS, I realized a weird problem. When developing a Web API with NestJS, the framework creates swagger document from API controllers. One of my API endpoint was POST request and getting data as body. What I realized is that somehow API body was not rendering on swagger document and below output was emerging.

Document says that “Could not render r, see the console”. And I looked at browser console to find logical error to understand the reason.

As you see from the browser console we couldn’t understand what is causing the problem. Unfortunately, it is really hard to understand cause of the problem and solve it because there is no logical and understandable error message anywhere. If you experienced this error before, it takes a lot of time to solve, days or weeks.

Almost whole NodeJS developers know and use index.js or index.ts files to group related files called barrel. At some point in the application, related files could be called from each other. The problem arises at this point. Imagine two classes in different files, both are being exported from their barrel file and one class is calling another.

index.ts (barrel file)
identifier.dto.ts
cat.dto.ts (Wrong Usage)

If you looked at cat.dto.ts file, you can see that identifier.dto.ts file is being called from this file and these two files are exported from barrel file. In this usage, a circular dependency is happening between cat.dto.ts and identifier.dto.ts with help of barrel file because identifier.dto.ts file is called from barrel file in cat.dto.ts. Below approach will solve the problem.

cat.dto.ts (true-usage)

I hope it help you with this problem. Thank you for reading. If you have further questions and comments, please write down to comments section.

--

--