6

Is there any approaches to auto-generate OpenAPI/Swagger metadata for WebApi methods or maybe even to generate client typescript models?

I found Swashbuckle and Swagger to JS & Typescript Codegen to be nice but I wonder to find out any other libs.

It would be perfect if you can give pros and cons of each approach.

4 Answers 4

3

You can also check out NSwag, a Swagger toolchain for .NET and TypeScript.

The tool generates Swagger specs from existing Web API controllers and client code based on these Swagger specs. You can generate TypeScript client code for JQuery with Callbacks or Promises, AngularJS or Angular2. The generated DTOs (request/response types) can either be interfaces or classes (with date conversion, lower camel casing, etc..)

The project can be used with a simple to use desktop GUI, build scripts or a command line tool.

enter image description here

Disclaimer: I'm one of the contributors of NSwag.

Sign up to request clarification or add additional context in comments.

Comments

2

Please try Swagger-Codegen, which is a free and open-source code generator to generate REST API clients (e.g. C#, TypeScript, Javascript and more) or server stubs (ASP.NET5) given an OpenAPI/Swagger spec.

Here is a good starting point: https://github.com/swagger-api/swagger-codegen#getting-started

For typescript, Swagger Codegen supports different libraries: typescript-node, typescript-angular, typescript-angular2 and typescript-fetch

Comments

0

Ultimately you want TypeScript models, not Swagger meta data. ASP.NET Web API Client Generators may be more handy, less overhead than swagger toolchain during SDLC.

While programmers generally use WebApiClientGen to generate client API codes, this project also provides POCO2TS.exe, a command line program that generates TypsScript interfaces from POCO classes. You may use either Poco2ts.exe or the poco2ts component to integrate the code generation with your build pipeline.

Comments

0

openapi-typescript

openapi client generator for typescript

npm install @cblx.br/openapi-typescript --save-dev

create an openapi-typescript.config.js

var config = {
    url: "<url>/swagger.json",
    outputDir: "./src/client",
};

module.exports = config;

execute it:

npx openapi-typescript


Angular example:

create an app-connector.ts

import { Injectable, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { OpenApiConnector } from '@cblx.br/openapi-typescript';

@Injectable({
    providedIn: 'root'
})
export class AppConnector implements OpenApiConnector {

    constructor(@Inject('BASE_URL') private baseUrl: string, private http: HttpClient) {}

    async request(method: string, path: string, parameters: any, body: any) {
        return await this.http.request(method, this.baseUrl + path, {
            body: body,
            params: parameters
        }).toPromise();
    }
}

openapi-typescript.config.js

var config = {
    url: "<url>/swagger.json",
    outputDir: "./src/client",
     events: {
        beforeWriteServiceClass: () => {
            var content = '';
            content += `import { Inject, Injectable } from '@angular/core';\n`;
            content += `import { AppConnector } from 'app/app-connector';\n`;
            content += `@Injectable({ providedIn: 'root' })\n`;
            return content;
        },
        createConnectorDecorator: () => {
            return `@Inject(AppConnector) `;
        }
    }
};

module.exports = config;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.