#Swagger response generation from TS Interfaces

4 messages · Page 1 of 1 (latest)

agile kraken
#

I've got swagger set up on my application, but a known issue is that it needs a class to generate documentation about. Normally an interface will not work since that's not runtime code.

I found this project typeconv that can generate OpenAPI schema files form TS interfaces: https://github.com/grantila/typeconv

So I run npx typeconv -f ts -t oapi -o swagger-schemas --oapi-format json 'src/**/models/*.ts' and it generates swagger-schemas/{folder}/model/*.json files

Now that I have these, how do I add them to my Nest-Swagger config so it know about them?

In my main.ts this is my pretty basic setup:

const swaggerConfig = new DocumentBuilder()
  .setTitle('Example Server')
  .build();
const swaggerDocument = SwaggerModule.createDocument(app, swaggerConfig);
SwaggerModule.setup(swaggerPath, app, swaggerDocument);
GitHub

Convert between JSON Schema, TypeScript, GraphQL, Open API and SureType - grantila/typeconv

plain osprey
#

From the type definitions, I think the third parameter of SwaggerModule.setup is the OpenAPI spec itself in JSON format. You could try to pass your generated JSON as that parameter instead of using SwaggerModule.createDocument.

agile kraken
#

I've looked at what SwaggerModule.createDocument generates, and it is quite a lot of information which I don't think I could recreate on my own with this tool.

typeconv will generate the JSON for my interfaces. I suppose I need to combine that data with the swagger-generated data manually somehow? Wow, this might not be worth the effort if that is the case

plain osprey
#

Yes, you'll probably need to inject each of your JSON models into the components.schemas property generated by SwaggerModule.createDocument, and make sure that these schemas are referenced by your operations. That sure sounds like a lot of work.
I personally find it rather handy to use classes for DTOs because it also makes it possible to validate incoming request bodies using ValidationPipe (which uses class-validator decorators under the hood).