diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java index fcec455e9b..effcddc683 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java @@ -144,6 +144,10 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { // replace - with _ e.g. created-at => created_at name = name.replaceAll("-", "_"); + if("_".equals(name)) { + name = "_u"; + } + // if it's all uppper case, do nothing if (name.matches("^[A-Z_]*$")) { return name; diff --git a/modules/swagger-codegen/src/test/scala/Java/JavaModelTest.scala b/modules/swagger-codegen/src/test/scala/Java/JavaModelTest.scala index fc819f1f19..0df82fce37 100644 --- a/modules/swagger-codegen/src/test/scala/Java/JavaModelTest.scala +++ b/modules/swagger-codegen/src/test/scala/Java/JavaModelTest.scala @@ -3,6 +3,7 @@ package Java import io.swagger.codegen.languages.JavaClientCodegen import io.swagger.models._ import io.swagger.models.properties._ +import io.swagger.util.Json import org.junit.runner.RunWith import org.scalatest.{FlatSpec, Matchers} import org.scalatest.junit.JUnitRunner @@ -344,3 +345,32 @@ class JavaModelTest extends FlatSpec with Matchers { cm.classname should be("WithDots") } } + + +@RunWith(classOf[JUnitRunner]) +class JavaModelTest2 extends FlatSpec with Matchers { + it should "translate an invalid param name" in { + val model = new ModelImpl() + .description("a model with a 2nd char upper-case property names") + .property("_", new StringProperty()) + + val codegen = new JavaClientCodegen() + val cm = codegen.fromModel("sample", model) + + cm.name should be("sample") + cm.classname should be("Sample") + cm.vars.size should be(1) + + val vars = cm.vars + Json.prettyPrint(vars.get(0)) + vars.get(0).baseName should be("_") + vars.get(0).getter should be("getU") + vars.get(0).setter should be("setU") + vars.get(0).datatype should be("String") + vars.get(0).name should be("u") + vars.get(0).defaultValue should be("null") + vars.get(0).baseType should be("String") + vars.get(0).hasMore should equal(null) + vars.get(0).isNotContainer should equal(true) + } +}