Swagger-codegen: [typescript-angular2] enum with numeric name

Created on 2 Aug 2016  路  7Comments  路  Source: swagger-api/swagger-codegen

Description

When generating with the typescript-angular2 language, an enum is generated with an integer name. The typescript compiler error is:

error TS2452: An enum member cannot have a numeric name.

I am generating this from a C# Web API that on a bad request returns a ModelStateDictionary which has a validationState of type ModelValidationState.

The generated Typescript enum is:

export namespace ModelStateEntry {

    export enum ValidationStateEnum { 
        0 = <any> '0',
        1 = <any> '1',
        2 = <any> '2',
        3 = <any> '3',
    }
}
Swagger-codegen version

2.2.0

Swagger declaration file content or url

The swagger definition for this from the JSON content is:

"ModelStateEntry": {
    "type": "object",
    "properties": {
        "rawValue": {
            "type": "object"
        },
        "attemptedValue": {
            "type": "string"
        },
        "errors": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/ModelError"
            },
            "readOnly": true
        },
        "validationState": {
            "format": "int32",
            "enum": [
                0,
                1,
                2,
                3
            ],
            "type": "integer"
        },
        "isContainerNode": {
            "type": "boolean",
            "readOnly": true
        },
        "children": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/ModelStateEntry"
            },
            "readOnly": true
        }
    }
}
Steps to reproduce

Run swagger-codegen generate against a web api that returns a model result of ModelStateDictionary (C# type)

Related issues

Using Swashbuckle configuration of DescribeAllEnumsAsStrings() solves the issue, but we may be able to do something here also to prevent the case where this occurs.

Suggest a Fix

The typescript-angular2/model.mustache contains the declaration for the enum, but I'm not sure of the proper change to make here.

TypeScript Enum Bug help wanted

All 7 comments

@damienpontifex thanks for reporting the issue. Using the example you provided, what would be the correct typescript code looks like?

Adding the DescribeAllEnumsAsStrings() options on for Swashbuckle, I got

export namespace ModelStateEntry {
    export enum ValidationStateEnum { 
        unvalidated = <any> 'unvalidated',
        invalid = <any> 'invalid',
        valid = <any> 'valid',
        skipped = <any> 'skipped',
    }
}

I also have JSON.Net setup with global StringEnumConverter so the string values in this case makes sense.

In the case when swashbuckle wasn't configured for enums as strings and no stringenumconverter I would assume it would look more like:

export namespace ModelStateEntry {
    export enum ValidationStateEnum { 
        unvalidated = <any> 0,
        invalid = <any> 1,
        valid = <any> 2,
        skipped = <any> 3,
    }
}

It's probably the second case with number enums where swagger-codegen would have to manage better I believe?

In the spec

        "validationState": {
            "format": "int32",
            "enum": [
                0,
                1,
                2,
                3
            ],
            "type": "integer"
        },

There's no information about the mapping between unvalidated and 0, and other enum values.

Where are unvalidated, invalid, etc defined?

The C# enum it maps from is:

public enum ModelValidationState
{
    Unvalidated,
    Invalid,
    Valid,
    Skipped
}

That's my current problem is producing strings and get the actual keys. Or produces numbers and have no information. That's why I couldn't figure out the changes needed to the moustache template as it doesn't provide enough information for the integer case.

Would this be a problem with the generator? Or a limit in the swagger spec type?

Currently, the spec does not support mapping between enum key (name) and values but that can be worked around using vendor extensions.

@wing328 it seems this issue was resolved with https://github.com/swagger-api/swagger-codegen/pull/6233 so it could be closed

Was this page helpful?
0 / 5 - 0 ratings