From 8408a97aef841228bb4e8211653b4b2cec5c9c92 Mon Sep 17 00:00:00 2001 From: xhh Date: Mon, 24 Aug 2015 16:50:57 +0800 Subject: [PATCH] Java: Support special characters in enum values and use upper case for enum names Conflicts: samples/client/petstore/java/default/src/main/java/io/swagger/client/JSON.java samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Order.java samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Pet.java --- .../codegen/languages/JavaClientCodegen.java | 25 +++++++++++++++++++ .../src/main/resources/Java/JSON.mustache | 2 ++ .../main/resources/Java/enumClass.mustache | 14 +++++++++++ .../src/main/resources/Java/model.mustache | 11 +++----- .../src/main/java/io/swagger/client/JSON.java | 4 ++- .../java/io/swagger/client/model/Order.java | 21 +++++++++++++--- .../java/io/swagger/client/model/Pet.java | 23 +++++++++++++---- .../io/swagger/petstore/test/PetApiTest.java | 6 ++--- .../swagger/petstore/test/StoreApiTest.java | 2 +- .../client/petstore/java/jersey2/hello.txt | 1 - .../src/main/java/io/swagger/client/JSON.java | 4 ++- .../java/io/swagger/client/model/Order.java | 25 +++++++++++++++---- .../java/io/swagger/client/model/Pet.java | 25 +++++++++++++++---- .../io/swagger/petstore/test/PetApiTest.java | 6 ++--- .../swagger/petstore/test/StoreApiTest.java | 2 +- 15 files changed, 134 insertions(+), 37 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/Java/enumClass.mustache delete mode 100644 samples/client/petstore/java/jersey2/hello.txt 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 379a2db2eb..3b35e85d95 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 @@ -318,6 +318,31 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { return codegenModel; } + @Override + public Map postProcessModels(Map objs) { + List models = (List) objs.get("models"); + for (Object _mo : models) { + Map mo = (Map) _mo; + CodegenModel cm = (CodegenModel) mo.get("model"); + for (CodegenProperty var : cm.vars) { + Map allowableValues = var.allowableValues; + if (allowableValues == null) + continue; + List values = (List) allowableValues.get("values"); + // put "enumVars" map into `allowableValues", including `name` and `value` + List> enumVars = new ArrayList>(); + for (String value : values) { + Map enumVar = new HashMap(); + enumVar.put("name", toVarName(value.toUpperCase())); + enumVar.put("value", value); + enumVars.add(enumVar); + } + allowableValues.put("enumVars", enumVars); + } + } + return objs; + } + private CodegenModel reconcileInlineEnums(CodegenModel codegenModel, CodegenModel parentCodegenModel) { // This generator uses inline classes to define enums, which breaks when // dealing with models that have subTypes. To clean this up, we will analyze diff --git a/modules/swagger-codegen/src/main/resources/Java/JSON.mustache b/modules/swagger-codegen/src/main/resources/Java/JSON.mustache index 4f90c6190b..7689856f5d 100644 --- a/modules/swagger-codegen/src/main/resources/Java/JSON.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/JSON.mustache @@ -14,6 +14,8 @@ public class JSON { mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); + mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); mapper.registerModule(new JodaModule()); } diff --git a/modules/swagger-codegen/src/main/resources/Java/enumClass.mustache b/modules/swagger-codegen/src/main/resources/Java/enumClass.mustache new file mode 100644 index 0000000000..1cef90b75a --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/Java/enumClass.mustache @@ -0,0 +1,14 @@ +public enum {{datatypeWithEnum}} { + {{#allowableValues}}{{#enumVars}}{{name}}("{{value}}"){{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}} + + private String value; + + StatusEnum(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } +} diff --git a/modules/swagger-codegen/src/main/resources/Java/model.mustache b/modules/swagger-codegen/src/main/resources/Java/model.mustache index 13c0c15c9a..bfb65a3d41 100644 --- a/modules/swagger-codegen/src/main/resources/Java/model.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/model.mustache @@ -18,13 +18,10 @@ import com.fasterxml.jackson.annotation.JsonProperty; {{>generatedAnnotation}} public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} { {{#vars}}{{#isEnum}} - public enum {{datatypeWithEnum}} { - {{#allowableValues}}{{#values}} {{.}}, {{/values}}{{/allowableValues}} - };{{/isEnum}}{{#items.isEnum}}{{#items}} - public enum {{datatypeWithEnum}} { - {{#allowableValues}}{{#values}} {{.}}, {{/values}}{{/allowableValues}} - }; -{{/items}}{{/items.isEnum}} + +{{>enumClass}}{{/isEnum}}{{#items.isEnum}}{{#items}} + +{{>enumClass}}{{/items}}{{/items.isEnum}} private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/vars}} {{#vars}} diff --git a/samples/client/petstore/java/default/src/main/java/io/swagger/client/JSON.java b/samples/client/petstore/java/default/src/main/java/io/swagger/client/JSON.java index 030152a997..5523b921e4 100644 --- a/samples/client/petstore/java/default/src/main/java/io/swagger/client/JSON.java +++ b/samples/client/petstore/java/default/src/main/java/io/swagger/client/JSON.java @@ -6,7 +6,7 @@ import com.fasterxml.jackson.datatype.joda.*; import java.io.IOException; -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-24T11:46:58.447+08:00") +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-24T18:19:30.060+08:00") public class JSON { private ObjectMapper mapper; @@ -14,6 +14,8 @@ public class JSON { mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); + mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); mapper.registerModule(new JodaModule()); } diff --git a/samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Order.java b/samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Order.java index df7ad48b6e..e89c1f3fb4 100644 --- a/samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Order.java +++ b/samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Order.java @@ -9,16 +9,29 @@ import com.fasterxml.jackson.annotation.JsonProperty; @ApiModel(description = "") -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-24T11:46:58.447+08:00") +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-24T18:19:30.060+08:00") public class Order { private Long id = null; private Long petId = null; private Integer quantity = null; private Date shipDate = null; - public enum StatusEnum { - placed, approved, delivered, - }; + +public enum StatusEnum { + PLACED("placed"), APPROVED("approved"), DELIVERED("delivered"); + + private String value; + + StatusEnum(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } +} + private StatusEnum status = null; private Boolean complete = null; diff --git a/samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Pet.java b/samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Pet.java index 68799092a3..b3b0e1b534 100644 --- a/samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Pet.java +++ b/samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Pet.java @@ -1,8 +1,8 @@ package io.swagger.client.model; import io.swagger.client.model.Category; -import io.swagger.client.model.Tag; import java.util.*; +import io.swagger.client.model.Tag; @@ -11,7 +11,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; @ApiModel(description = "") -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-24T11:46:58.447+08:00") +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-24T18:19:30.060+08:00") public class Pet { private Long id = null; @@ -19,9 +19,22 @@ public class Pet { private String name = null; private List photoUrls = new ArrayList(); private List tags = new ArrayList(); - public enum StatusEnum { - available, pending, sold, - }; + +public enum StatusEnum { + AVAILABLE("available"), PENDING("pending"), SOLD("sold"); + + private String value; + + StatusEnum(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } +} + private StatusEnum status = null; diff --git a/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/PetApiTest.java b/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/PetApiTest.java index b6ad5fb052..0a8f8a1665 100644 --- a/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/PetApiTest.java +++ b/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/PetApiTest.java @@ -86,7 +86,7 @@ public class PetApiTest { public void testFindPetsByStatus() throws Exception { Pet pet = createRandomPet(); pet.setName("programmer"); - pet.setStatus(Pet.StatusEnum.available); + pet.setStatus(Pet.StatusEnum.AVAILABLE); api.updatePet(pet); @@ -108,7 +108,7 @@ public class PetApiTest { public void testFindPetsByTags() throws Exception { Pet pet = createRandomPet(); pet.setName("monster"); - pet.setStatus(Pet.StatusEnum.available); + pet.setStatus(Pet.StatusEnum.AVAILABLE); List tags = new ArrayList(); Tag tag1 = new Tag(); @@ -183,7 +183,7 @@ public class PetApiTest { category.setName("really-happy"); pet.setCategory(category); - pet.setStatus(Pet.StatusEnum.available); + pet.setStatus(Pet.StatusEnum.AVAILABLE); List photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}); pet.setPhotoUrls(photos); diff --git a/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/StoreApiTest.java b/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/StoreApiTest.java index 508764b8c2..a1fd7e345a 100644 --- a/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/StoreApiTest.java +++ b/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/StoreApiTest.java @@ -64,7 +64,7 @@ public class StoreApiTest { order.setPetId(new Long(200)); order.setQuantity(new Integer(13)); order.setShipDate(new java.util.Date()); - order.setStatus(Order.StatusEnum.placed); + order.setStatus(Order.StatusEnum.PLACED); order.setComplete(true); return order; diff --git a/samples/client/petstore/java/jersey2/hello.txt b/samples/client/petstore/java/jersey2/hello.txt deleted file mode 100644 index 6769dd60bd..0000000000 --- a/samples/client/petstore/java/jersey2/hello.txt +++ /dev/null @@ -1 +0,0 @@ -Hello world! \ No newline at end of file diff --git a/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/JSON.java b/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/JSON.java index 0baddcd02c..3095c7bace 100644 --- a/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/JSON.java +++ b/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/JSON.java @@ -6,7 +6,7 @@ import com.fasterxml.jackson.datatype.joda.*; import java.io.IOException; -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-22T21:47:05.989+08:00") +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-24T16:42:49.539+08:00") public class JSON { private ObjectMapper mapper; @@ -14,6 +14,8 @@ public class JSON { mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); + mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); mapper.registerModule(new JodaModule()); } diff --git a/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/model/Order.java b/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/model/Order.java index 671d8366be..2477288848 100644 --- a/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/model/Order.java +++ b/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/model/Order.java @@ -2,21 +2,36 @@ package io.swagger.client.model; import java.util.Date; + + import io.swagger.annotations.*; import com.fasterxml.jackson.annotation.JsonProperty; @ApiModel(description = "") -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-22T21:47:05.989+08:00") -public class Order { +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-24T16:42:49.539+08:00") +public class Order { private Long id = null; private Long petId = null; private Integer quantity = null; private Date shipDate = null; - public enum StatusEnum { - placed, approved, delivered, - }; + +public enum StatusEnum { + PLACED("placed"), APPROVED("approved"), DELIVERED("delivered"); + + private String value; + + StatusEnum(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } +} + private StatusEnum status = null; private Boolean complete = null; diff --git a/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/model/Pet.java b/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/model/Pet.java index 036f694ffd..1ed7294755 100644 --- a/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/model/Pet.java +++ b/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/model/Pet.java @@ -4,22 +4,37 @@ import io.swagger.client.model.Category; import java.util.*; import io.swagger.client.model.Tag; + + import io.swagger.annotations.*; import com.fasterxml.jackson.annotation.JsonProperty; @ApiModel(description = "") -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-22T21:47:05.989+08:00") -public class Pet { +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-24T16:42:49.539+08:00") +public class Pet { private Long id = null; private Category category = null; private String name = null; private List photoUrls = new ArrayList(); private List tags = new ArrayList(); - public enum StatusEnum { - available, pending, sold, - }; + +public enum StatusEnum { + AVAILABLE("available"), PENDING("pending"), SOLD("sold"); + + private String value; + + StatusEnum(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } +} + private StatusEnum status = null; diff --git a/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/PetApiTest.java b/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/PetApiTest.java index b6ad5fb052..0a8f8a1665 100644 --- a/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/PetApiTest.java +++ b/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/PetApiTest.java @@ -86,7 +86,7 @@ public class PetApiTest { public void testFindPetsByStatus() throws Exception { Pet pet = createRandomPet(); pet.setName("programmer"); - pet.setStatus(Pet.StatusEnum.available); + pet.setStatus(Pet.StatusEnum.AVAILABLE); api.updatePet(pet); @@ -108,7 +108,7 @@ public class PetApiTest { public void testFindPetsByTags() throws Exception { Pet pet = createRandomPet(); pet.setName("monster"); - pet.setStatus(Pet.StatusEnum.available); + pet.setStatus(Pet.StatusEnum.AVAILABLE); List tags = new ArrayList(); Tag tag1 = new Tag(); @@ -183,7 +183,7 @@ public class PetApiTest { category.setName("really-happy"); pet.setCategory(category); - pet.setStatus(Pet.StatusEnum.available); + pet.setStatus(Pet.StatusEnum.AVAILABLE); List photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}); pet.setPhotoUrls(photos); diff --git a/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/StoreApiTest.java b/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/StoreApiTest.java index 508764b8c2..a1fd7e345a 100644 --- a/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/StoreApiTest.java +++ b/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/StoreApiTest.java @@ -64,7 +64,7 @@ public class StoreApiTest { order.setPetId(new Long(200)); order.setQuantity(new Integer(13)); order.setShipDate(new java.util.Date()); - order.setStatus(Order.StatusEnum.placed); + order.setStatus(Order.StatusEnum.PLACED); order.setComplete(true); return order;