Generating type with enums causes erroneous code to be generated
Description
2.4.0-SNAPSHOT typescript processor is now processing ENUMS. However the processing is faulty.
Given a yaml model which has an enum portion invalid typescript code is produced. This was discovered while checking: https://github.com/swagger-api/swagger-codegen/pull/7479
Swagger-codegen version
2.4.0-SNAPSHOT
Swagger declaration file content or url
test:
type: string
description: one of [TEST, TEST1, TEST2]
enum: [TEST, TEST1, TEST2]
Command line used for generation
mvn clean compile
Steps to reproduce
Run the compile and check the output Test.ts
Related issues/PRs
https://github.com/swagger-api/swagger-codegen/pull/7479
Suggest a fix/enhancement
The output code is as follows:
export type Test = 'TEST' | 'TEST1' | 'TEST2';
export const Test = {
TEST: 'TEST' as Test,
TEST1: 'TEST1' as Test,
TEST2: 'TEST2' as Test
}
and leads to the following warning if an import of the "Test" is attempted:
0] WARNING in ./src/app/model/Test.ts [0] There are multiple modules with names that only differ in casing. [0] This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. [0] Use equal casing. Compare these module identifiers:
Ideally the enum should have a different name so we know how to use it. In production mode the warning becomes an error:
ERROR in ./src/app/model/test.ts Module build failed: Error: \src\app\model\test.ts is not part of the compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property. at AngularCompilerPlugin.getCompiledFile (C:\workspaces\e2e\nge2e-dashboard-ui\node_modules@ngtools\webpack\src\angular_compiler_plugin.js:643:23)
Again this is due to naming confusion.
@inthegarage could you please provide a swagger definition with the minimum details needed to generate an api?
also, looking at e.g. https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/typescript-angular-v4.3/npm/model/order.ts it seems that enums in models are processed correctly.
The warning that you posted
[0] WARNING in ./src/app/model/Test.ts [0] There are multiple modules with names that only differ in casing.
indicates that ./src/app/model/Test.ts and ./src/app/model/test.ts both exist. This should not be the case. Can you delete the generated model files and re-generate them again? My guess would be that this was caused due to generation / git commit once on a system with case-sensitive paths and again on a system with case-insensitive paths.
The production warning
ERROR in ./src/app/model/test.ts Module build failed: Error: \src\app\model\test.ts is not part of the compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
might be the result of import * from 'src/app/model/Test' on a case-sensitive system while only /src/app/model/test.ts exists.
@macjohnny The swagger definition provided above is the minimal that you need, simply add it to the definitions section of the yaml file. Only Test.ts existed with the contents as mentioned. The directory was cleaned.
I notice that in the order yaml the enum is defined as follows:
enum:
- placed
- approved
- delivered
However we are using the array above is that relevant? Both seem valid when used with other plugins, so it's how the typescript one is approaching the definition above.
@inthegarage from the swagger definition given above I don't see whether test is a parameter, or a model property, nor can I see that this should be an array of enums, which would be relevant for finding the error.
Please provide a valid, minimal swagger file for further analysis.
No problem, this will produce the file as mentioned:
swagger: "2.0"
info:
version: "1.0.0"
title: minimal
description: Example of the bare minimum Swagger spec
host: localhost:3000 # otherwise, a random port will be used each time
paths:
/users:
get:
responses:
"200":
description: Describe the 200 response in more detail
definitions:
test:
type: string
description: one of [TEST, TEST1, TEST2]
enum: [TEST, TEST1, TEST2]
@inthegarage I generated the code with your swagger definition and successfully included it in a dummy angular app created with angular cli.
import { Component } from '@angular/core';
import {Test} from "./output";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
bli: Test = Test.TEST;
}

The app compiles successfully.
Could you please provide the relevant code where you are using the generated test enum, causing compilation errors?
@inthegarage I am using TypeScript version 2.4.2
The version is the same. I will set-up a clean environment to check this.
I am closing this one as I cannot reproduce it anymore.
Hello, don't you see an issue of having
export type Test
export const Test
in the same file?
Why not just
export enum Test
?
I'm also seeing the issue. nSwagger is generating multiple classes(3 in my case) for one c# enum class. The multiple classes are appended with numbers starting 1.
Seeing the same issue as @ricardasjak . Was there any fix or solution for this?
@JosephDesmet , @coder8keyboard I closed the issue as I couldn't reproduce it anymore. I doubt if anyone is fixing a closed issue. You need to raise a new one with the environment, yaml and config you are using. Update: I have just tried a new generation with enums and this is all ok with my set-up. Note I'm using a pretty up-to-date Angular, Typescript and other variants.