Skip to content

Latest commit

 

History

History
98 lines (79 loc) · 2.44 KB

README.md

File metadata and controls

98 lines (79 loc) · 2.44 KB

Server config sample

ASP.NET Core NSwag configuration

You can open Swaggie.Nswag/Swaggie.Nswag.sln in Rider or VS to see the sample ASP.NET Core project with NSwag configured. It is working out of the box and it requires dotnet 8.0. It should be compatible with other dotnet versions as well.

Concerns

If you use complex query parameter types, then I suggest you strongly to migrate away from NSwag to Swashbuckle. Both are generating wrong specs, but for Swashbuckle it's possible to fix it by using custom OperationFilter. NSwag offers same extensibility, but in practice it's not working as expected (or, it's very hard to make it work).

Swaggie result

This is how the generated API Client in TypeScript looks like:

/* tslint:disable */
/* eslint-disable */
//----------------------
// <auto-generated>
//   Generated using Swaggie (https://github.com/yhnavein/swaggie)
//   Please avoid doing any manual changes in this file
// </auto-generated>
//----------------------
// ReSharper disable InconsistentNaming

import Axios, { AxiosPromise, AxiosRequestConfig } from 'axios';

export const axios = Axios.create({
  baseURL: '/api',
});

export const userClient = {
  /**
   * @param user
   */
  createUser(user: UserViewModel, $config?: AxiosRequestConfig): AxiosPromise<UserViewModel> {
    let url = '/user';

    return axios.request<UserViewModel>({
      url: url,
      method: 'POST',
      data: user,
      ...$config,
    });
  },

  /**
   * @param id
   */
  deleteUser(id: number, $config?: AxiosRequestConfig): AxiosPromise<any> {
    let url = '/user/{id}';

    url = url.replace('{id}', encodeURIComponent('' + id));

    return axios.request<any>({
      url: url,
      method: 'DELETE',
      ...$config,
    });
  },

  /**
   */
  getUsers($config?: AxiosRequestConfig): AxiosPromise<UserViewModel[]> {
    let url = '/user';

    return axios.request<UserViewModel[]>({
      url: url,
      method: 'GET',
      ...$config,
    });
  },
};

function serializeQueryParam(obj: any) {
  if (obj === null || obj === undefined) return '';
  if (obj instanceof Date) return obj.toJSON();
  if (typeof obj !== 'object' || Array.isArray(obj)) return obj;
  return Object.keys(obj)
    .reduce((a: any, b) => a.push(b + '=' + obj[b]) && a, [])
    .join('&');
}

export interface UserViewModel {
  id: number;
  role: UserRole;
  name?: string;
  email?: string;
}

export enum UserRole {
  Admin = 'Admin',
  User = 'User',
  Guest = 'Guest',
}