Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #1088
Closed
Open
Issue created Sep 24, 2018 by Administrator@rootContributor

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

Created by: 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

https://github.com/OpenAPITools/openapi-generator/issues/635 is a similar issue, which talks about the same problem. The https://github.com/OpenAPITools/openapi-generator/pull/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.

Assignee
Assign to
Time tracking