Skip to content

[C#] Bug with enum classes referenced from other objects #1088

Closed
@rubms

Description

@rubms
Description

The C# auto-generated code for enum classes referenced from other objects is generated with errors, so that the auto-generated code does not compile.

The EnumClass in the sample below is auto-generated like

    public enum EnumClass
    {
        /// <summary>
        /// Enum Value1 for value: Value1
        /// </summary>
        Value1 = Value1,

        /// <summary>
        /// Enum Value2 for value: Value2
        /// </summary>
        Value2 = Value2,

        /// <summary>
        /// Enum Value3 for value: Value3
        /// </summary>
        Value3 = Value3
    }

This code does not compile. Instead it should be generated like

    [JsonConverter(typeof(StringEnumConverter))]
    public enum EnumClass
    {
        /// <summary>
        /// Enum Value1 for value: Value1
        /// </summary>
        [EnumMember(Value = "Value1")]
        Value1 = 1,

        /// <summary>
        /// Enum Value2 for value: Value2
        /// </summary>
        [EnumMember(Value = "Value2")]
        Value2 = 2,

        /// <summary>
        /// Enum Value3 for value: Value3
        /// </summary>
        [EnumMember(Value = "Value3")]
        Value3 = 3
    }
openapi-generator version

Reproduced with OpenAPI generator v3.2.3.

OpenAPI declaration file content or url
swagger: '2.0'
info:
  description: "This spec is for testing the wrong generation of enumerations referred to by other objects."
  version: 1.0.0
  title: Enumerations test case
  license:
    name: Apache-2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
schemes:
  - http
paths:
  /enum_test/:
    get:
      tags:
        - Enums
      summary: Get object referring an enumeration
      description: Get object referring an enumeration
      operationId: getObjectReferringEnum
      responses:
        '200':
          description: successful operation
          schema:
            $ref: '#/definitions/ObjectReferringEnum'

definitions:
  ObjectReferringEnum:
    type: object
    properties:
      adjustmentType:
        $ref: "#/definitions/EnumClass"
  EnumClass:
    type: string
    enum:
    - Value1
    - Value2
    - Value3
Command line used for generation

java -jar openapi-generator-cli-3.2.3.jar generate -g csharp -i test_enums.yml -o test_enums

Steps to reproduce

Auto-generate code from the specification above using the openapi-generator-cli-3.2.3, specifying the csharp generator. The auto-generated model, EnumClass, is incorrectly generated and does not compile.

Related issues/PRs

#635 is a similar issue, which talks about the same problem. The #774 pull request fixes the problem but it is still reproduced when the enumeration class is referred to from other objects in the specification.

Suggest a fix/enhancement

The code is incorrectly generated from the enumClass.mustache template because the enum values (which are of String type) a not correctly marked as strings. This isString property comes to false because of the postProcessEnumRefs() method in AbstractCSharpCodegen.java. That method post-processes enums that are referenced from other classes, and includes a call to updateCodegenPropertyEnum(). updateCodegenPropertyEnum() re-evaluates the isString condition of the enumeration values:

            enumVar.put("isString", isDataTypeString(dataType));

isDataTypeString(dataType) evaluates as false, because dataType in enum references is not String, but the name of the enum class (EnumClass in this the sample above).

I don't see the point in calling updateCodegenPropertyEnum() for these referenced enumerations. I suggest removing that call. I have checked and commenting that call fixes the problem, while it breaks no unit tests and the auto-generate samples stay correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions