Open satyanarayan18 opened 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...
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.
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
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
This feature is not implemented yet
Please when will this feature be implemented or any temporary workaround?
Anything we can do to avoid these duplicate enums in TypeScript?
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
I'm also seeing nSwag generating multiple classes for one c# enum class. Is it a bug in nSwag?
Duplicate of https://github.com/RSuter/NJsonSchema/issues/17
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');
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 }