Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rubms opened this issue Sep 24, 2018 · 2 comments
Closed

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

rubms opened this issue Sep 24, 2018 · 2 comments

Comments

@rubms
Copy link
Contributor

rubms commented Sep 24, 2018

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.

rubms pushed a commit to rubms/openapi-generator that referenced this issue Sep 24, 2018
…on properties while processing objects that reference them. Launched the ./bin/csharp-petstore-all.sh script.
@rubms
Copy link
Contributor Author

rubms commented Sep 27, 2018

I have filed a pull request that fixes the problem.

jimschubert pushed a commit that referenced this issue Oct 11, 2018
…other objects (#1089)

* Run ./bin/utils/ensure-up-to-date to re-generate samples run in the CI.

* Fixed the #1088 issue by removing the update of enumeration properties while processing objects that reference them. Launched the ./bin/csharp-petstore-all.sh script.
@rubms
Copy link
Contributor Author

rubms commented Oct 16, 2018

Fixed, as #1089 has been merged into master. Thanks @jimschubert !

@rubms rubms closed this as completed Oct 16, 2018
A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this issue Feb 27, 2019
…renced from other objects (OpenAPITools#1089)

* Run ./bin/utils/ensure-up-to-date to re-generate samples run in the CI.

* Fixed the OpenAPITools#1088 issue by removing the update of enumeration properties while processing objects that reference them. Launched the ./bin/csharp-petstore-all.sh script.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant