RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.79k stars 1.29k forks source link

Enum Generating as many times as used in the class #1499

Open satyanarayan18 opened 6 years ago

satyanarayan18 commented 6 years ago

I have created an enum in c#

public enum Color { Red, Green }

public class A { public Color Color }

public class B { public Color Color }

Enum generated in the client side proxy using n-swag as

export enum AColor { _0=0, _1=1 }

public enum BColor { _0=0, _1=1 }

Expected result,

export enum Color { _0=0, _1=1 }

RicoSuter commented 6 years ago

How did you generate the Swagger spec? It's probably not generated with NSwag and contains only 0, 1 as enum values (and no names). This is why NSwag cannot know the actual enum value names...

satyanarayan18 commented 6 years ago

API is documented using SwashBuckle but the client-side code is generated using N-Swag. My concern is not related to 0 or 1. The concern over here is that enum name in client side is prepared by appending class name so if an enum is used in 10 classed then 10 enums will be generated on the client side which will increase the size of js thereby affecting the performance.

RicoSuter commented 6 years ago

NSwag uses $refs for enums so that they are generated only once, Swashbuckle and a lot of other tools repeat the enums which lead to lots of duplicated code... There is an open issue to first "merge" enums: https://github.com/RSuter/NJsonSchema/issues/17

satyanarayan18 commented 6 years ago

Thanks for the response. Could you please provide or guide me through some examples to use the Merge enum while generating client side using NSwag

RicoSuter commented 6 years ago

This feature is not implemented yet

Estar1 commented 6 years ago

Please when will this feature be implemented or any temporary workaround?

Kordonme commented 6 years ago

Anything we can do to avoid these duplicate enums in TypeScript?

Kordonme commented 6 years ago

I've created a regex to remove these duplicate enums using a PowerShell script that is running after NSwag.

(Get-Content -Raw ./src/apiClient/apiClient.ts) `
  -replace '(\w+\??: )([A-Z][A-Za-z]+)(\d+)', '$1$2' `
  -replace '((\/\*\*[\w\s]+\*\/\s+)?(export enum [A-Za-z]+(\d+)[^}]+})(\s*))', '' |
  Out-File ./src/apiClient/apiClient.ts -encoding utf8
coder8keyboard commented 5 years ago

I'm also seeing nSwag generating multiple classes for one c# enum class. Is it a bug in nSwag?

RicoSuter commented 5 years ago

Duplicate of https://github.com/RSuter/NJsonSchema/issues/17

xprees commented 8 months ago

I've created a regex to remove these duplicate enums using a PowerShell script that is running after NSwag.

(Get-Content -Raw ./src/apiClient/apiClient.ts) `
  -replace '(\w+\??: )([A-Z][A-Za-z]+)(\d+)', '$1$2' `
  -replace '((\/\*\*[\w\s]+\*\/\s+)?(export enum [A-Za-z]+(\d+)[^}]+})(\s*))', '' |
  Out-File ./src/apiClient/apiClient.ts -encoding utf8

In case someone can find this helpful. I've created a JS script inspired by @Kordonme. I also added it as a script to package.json to simplify the workflow. Usage: node fix-api-enums.js ./src/api/api.service.ts

const fs = require('fs');

const file = process.argv[2];
if (!file || !fs.existsSync(file)) {
    console.error('Please provide a correct file path as an argument.');
    process.exit(1);
}

const content = fs.readFileSync(file, 'utf8');
const updatedContent = content.replace(/(\w+\??: )([A-Z][A-Za-z]+)(\d+)/g, '$1$2');
const finalContent = updatedContent.replace(/(\/\*\*[\w\s]+\*\/\s+)?(export enum [A-Za-z]+(\d+)[^}]+})(\s*)/g, '');
fs.writeFileSync(file, finalContent, 'utf8');