swagger-api/swagger-codegen

[Java] Elision of underscore characters in property names leads to invalid Java

Open

#4,805 创建于 2017年2月15日

在 GitHub 查看
 (30 评论) (0 反应) (0 负责人)HTML (12,701 star) (5,474 fork)batch import
Client: JavaIssue: Bughelp wanted

描述

Description

The swagger file, being generated from an external model contains some property names that contain underscores e.g. "CHT_Provider". It also contains some other property names that are identical once the underscores are removed e.g. "CHTProvider".

Swagger-codegen version

λ java -jar swagger-codegen\snapshot\swagger-codegen-cli-2.2.2-SNAPSHOT.jar version 2.2.2-SNAPSHOT

NOTE: Also replicates with 2.2.1

Swagger declaration file content or url

Below see an edited version of the swagger file, removed all paths and not required definitions

{
  "swagger": "2.0",
  "info": {
    "version": "v2",
    "title": "StayinFront.WebAPI V2"
  },
  "host": "localhost",
  "basePath": "/revapi",
  "schemes": [
    "http"
  ],
  "paths": {},
  "definitions": {
    "ClassInstanceBaseModel": {
      "description": "Base data for a class instance",
      "type": "object",
      "properties": {
        "class": {
          "description": "The class name",
          "type": "string"
        }
      }
    },
    "enums_CHTProvider": {
      "description": "CHTProvider",
      "type": "object",
      "properties": {
        "description": {
          "description": "description",
          "type": "string"
        },
        "data": {
          "enum": [
            "MOX",
            "RC"
          ],
          "type": "string"
        }
      }
    },
    "CHT_Configuration_Data": {
      "type": "object",
      "properties": {
        "CHTProvider": {
          "$ref": "#/definitions/enums_CHTProvider"
        },
        "CHT_Provider": {
          "$ref": "#/definitions/ClassInstanceBaseModel"
        }
      }
    }
  }
}

Using the below command line to generate code, the following Java code is generated for the CHTConfigurationData class:

...

/**
 * CHTConfigurationData
 */
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2017-02-16T11:28:41.907+13:00")
public class CHTConfigurationData {
  @SerializedName("CHTProvider")
  private EnumsCHTProvider cHTProvider = null;

  @SerializedName("CHT_Provider")
  private ClassInstanceBaseModel cHTProvider = null;
...
}


Obviously this fails to compile due to the duplicate member names. Adding underscore characters back into the generated Java in appropriate places seems to resolve the compilation errors.

I would expect that the underscores weren't removed, but failing that could an option be added to avoid this behaviour?

Command line used for generation

java -jar swagger-codegen\snapshot\swagger-codegen-cli-2.2.1.jar generate -i swagger-file.json -l java -o output-dir

Steps to reproduce
  1. generate Java source using the above command line
  2. attempt to compile
Related issues

Here is a similar issue relating to changing of case in property names: https://github.com/swagger-api/swagger-codegen/issues/4066

Here is a similar issue in PHP language generation: https://github.com/swagger-api/swagger-codegen/issues/4551

Here is a similar issue in node javascript code gen - there is a configuration option to disable the behaviour in this case: https://github.com/swagger-api/swagger-codegen/issues/2766

贡献者指南