0% found this document useful (0 votes)
16 views3 pages

Swagger To Code

Uploaded by

Karthik Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Swagger To Code

Uploaded by

Karthik Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Swagger to NestJS Code

Introduction:

An npm package which automatically generates typescript code for modules,


controllers, services, DTOs, and Entities for NestJS projects by using the swagger
documentation Json file.

Swagger file structure:

Paths:

- Unique Paths ex: /users:


o GET, POST etc...
▪ operationId
▪ summary
▪ parameters:
• [array of parameters such as size, page]
▪ requestBody:
• content: [multipart/form-data, application/json]
• Schema: [dto used]
▪ Responses:
• 201 and it’s type
• 400

Components:

- SecuritySchemes:
o Bearer
- Schemas:
o DTOs or entities [ex: userSearchDTO, Client etc…] :
▪ Properties:
• Id,name,email etc…
o Type: [string, int, etc… ]
o Example:
o Description:
Initial plan of action:

1. Input [Link] file from the user.

2. Fetching the different types of modules by scanning/parsing through the Paths and storing
them in an object.

a. Ex: if /users, /users/search, /users/invite are paths then Module user will be formed.

3. For each module identified in the previous step, generating respective folder structure for
them. Only files will be built initially.

4. Scanning through the schemas and getting information about the DTO’s and Entities.

a. A schema will be considered as a DTO if and only if it has a DTO/dto in the classname
otherwise it’ll be considered as entity.

b. Creating a HashMap with modules as keys and values will be their DTO for mapping
DTO’s to their respective modules.

i. Sample mapping: createUserDTO, searchUserDTO, UserResponseDTO, will be


mapped to the Module User and code will be generated in [Link]

ii. Post+[moduleName] will be used to generate their Entity files.

5. After generating DTO’s and entities, creating the controllers and services will be created and
only CRUD operation codes can be generated.

Complications:

1. Only Limited to basic CRUD creations, can’t generate logical code for more complex
operations such as searching through other repositories.

2. Can’t add Dependency Injections across the modules due to non-availability of the
information in the Swagger files.

3. Some complex controllers can have the functions and their bodies in service code but not
the logic.

4. Test files will be difficult to create.


Flowchart:

You might also like