Merge pull request #1126 from xhh/java-enum-refactor

[Java] Support special characters in enum values
This commit is contained in:
wing328 2015-08-26 16:41:56 +08:00
commit 1996aa9244
15 changed files with 134 additions and 37 deletions

View File

@ -319,6 +319,31 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
return codegenModel; return codegenModel;
} }
@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
List<Object> models = (List<Object>) objs.get("models");
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");
for (CodegenProperty var : cm.vars) {
Map<String, Object> allowableValues = var.allowableValues;
if (allowableValues == null)
continue;
List<String> values = (List<String>) allowableValues.get("values");
// put "enumVars" map into `allowableValues", including `name` and `value`
List<Map<String, String>> enumVars = new ArrayList<Map<String, String>>();
for (String value : values) {
Map<String, String> enumVar = new HashMap<String, String>();
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) { private CodegenModel reconcileInlineEnums(CodegenModel codegenModel, CodegenModel parentCodegenModel) {
// This generator uses inline classes to define enums, which breaks when // This generator uses inline classes to define enums, which breaks when
// dealing with models that have subTypes. To clean this up, we will analyze // dealing with models that have subTypes. To clean this up, we will analyze

View File

@ -14,6 +14,8 @@ public class JSON {
mapper = new ObjectMapper(); mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); 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()); mapper.registerModule(new JodaModule());
} }

View File

@ -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;
}
}

View File

@ -18,13 +18,10 @@ import com.fasterxml.jackson.annotation.JsonProperty;
{{>generatedAnnotation}} {{>generatedAnnotation}}
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} { public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
{{#vars}}{{#isEnum}} {{#vars}}{{#isEnum}}
public enum {{datatypeWithEnum}} {
{{#allowableValues}}{{#values}} {{.}}, {{/values}}{{/allowableValues}} {{>enumClass}}{{/isEnum}}{{#items.isEnum}}{{#items}}
};{{/isEnum}}{{#items.isEnum}}{{#items}}
public enum {{datatypeWithEnum}} { {{>enumClass}}{{/items}}{{/items.isEnum}}
{{#allowableValues}}{{#values}} {{.}}, {{/values}}{{/allowableValues}}
};
{{/items}}{{/items.isEnum}}
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/vars}} private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/vars}}
{{#vars}} {{#vars}}

View File

@ -6,7 +6,7 @@ import com.fasterxml.jackson.datatype.joda.*;
import java.io.IOException; 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 { public class JSON {
private ObjectMapper mapper; private ObjectMapper mapper;
@ -14,6 +14,8 @@ public class JSON {
mapper = new ObjectMapper(); mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); 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()); mapper.registerModule(new JodaModule());
} }

View File

@ -9,16 +9,29 @@ import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel(description = "") @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 { public class Order {
private Long id = null; private Long id = null;
private Long petId = null; private Long petId = null;
private Integer quantity = null; private Integer quantity = null;
private Date shipDate = null; private Date shipDate = null;
public enum StatusEnum { public enum StatusEnum {
placed, approved, delivered, 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 StatusEnum status = null;
private Boolean complete = null; private Boolean complete = null;

View File

@ -1,8 +1,8 @@
package io.swagger.client.model; package io.swagger.client.model;
import io.swagger.client.model.Category; import io.swagger.client.model.Category;
import io.swagger.client.model.Tag;
import java.util.*; import java.util.*;
import io.swagger.client.model.Tag;
@ -11,7 +11,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel(description = "") @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 { public class Pet {
private Long id = null; private Long id = null;
@ -19,9 +19,22 @@ public class Pet {
private String name = null; private String name = null;
private List<String> photoUrls = new ArrayList<String>(); private List<String> photoUrls = new ArrayList<String>();
private List<Tag> tags = new ArrayList<Tag>(); private List<Tag> tags = new ArrayList<Tag>();
public enum StatusEnum { public enum StatusEnum {
available, pending, sold, 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; private StatusEnum status = null;

View File

@ -86,7 +86,7 @@ public class PetApiTest {
public void testFindPetsByStatus() throws Exception { public void testFindPetsByStatus() throws Exception {
Pet pet = createRandomPet(); Pet pet = createRandomPet();
pet.setName("programmer"); pet.setName("programmer");
pet.setStatus(Pet.StatusEnum.available); pet.setStatus(Pet.StatusEnum.AVAILABLE);
api.updatePet(pet); api.updatePet(pet);
@ -108,7 +108,7 @@ public class PetApiTest {
public void testFindPetsByTags() throws Exception { public void testFindPetsByTags() throws Exception {
Pet pet = createRandomPet(); Pet pet = createRandomPet();
pet.setName("monster"); pet.setName("monster");
pet.setStatus(Pet.StatusEnum.available); pet.setStatus(Pet.StatusEnum.AVAILABLE);
List<Tag> tags = new ArrayList<Tag>(); List<Tag> tags = new ArrayList<Tag>();
Tag tag1 = new Tag(); Tag tag1 = new Tag();
@ -183,7 +183,7 @@ public class PetApiTest {
category.setName("really-happy"); category.setName("really-happy");
pet.setCategory(category); pet.setCategory(category);
pet.setStatus(Pet.StatusEnum.available); pet.setStatus(Pet.StatusEnum.AVAILABLE);
List<String> photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}); List<String> photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"});
pet.setPhotoUrls(photos); pet.setPhotoUrls(photos);

View File

@ -64,7 +64,7 @@ public class StoreApiTest {
order.setPetId(new Long(200)); order.setPetId(new Long(200));
order.setQuantity(new Integer(13)); order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date()); order.setShipDate(new java.util.Date());
order.setStatus(Order.StatusEnum.placed); order.setStatus(Order.StatusEnum.PLACED);
order.setComplete(true); order.setComplete(true);
return order; return order;

View File

@ -1 +0,0 @@
Hello world!

View File

@ -6,7 +6,7 @@ import com.fasterxml.jackson.datatype.joda.*;
import java.io.IOException; 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 { public class JSON {
private ObjectMapper mapper; private ObjectMapper mapper;
@ -14,6 +14,8 @@ public class JSON {
mapper = new ObjectMapper(); mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); 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()); mapper.registerModule(new JodaModule());
} }

View File

@ -2,21 +2,36 @@ package io.swagger.client.model;
import java.util.Date; import java.util.Date;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel(description = "") @ApiModel(description = "")
@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 Order { public class Order {
private Long id = null; private Long id = null;
private Long petId = null; private Long petId = null;
private Integer quantity = null; private Integer quantity = null;
private Date shipDate = null; private Date shipDate = null;
public enum StatusEnum { public enum StatusEnum {
placed, approved, delivered, 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 StatusEnum status = null;
private Boolean complete = null; private Boolean complete = null;

View File

@ -4,12 +4,14 @@ import io.swagger.client.model.Category;
import java.util.*; import java.util.*;
import io.swagger.client.model.Tag; import io.swagger.client.model.Tag;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel(description = "") @ApiModel(description = "")
@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 Pet { public class Pet {
private Long id = null; private Long id = null;
@ -17,9 +19,22 @@ public class Pet {
private String name = null; private String name = null;
private List<String> photoUrls = new ArrayList<String>(); private List<String> photoUrls = new ArrayList<String>();
private List<Tag> tags = new ArrayList<Tag>(); private List<Tag> tags = new ArrayList<Tag>();
public enum StatusEnum { public enum StatusEnum {
available, pending, sold, 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; private StatusEnum status = null;

View File

@ -86,7 +86,7 @@ public class PetApiTest {
public void testFindPetsByStatus() throws Exception { public void testFindPetsByStatus() throws Exception {
Pet pet = createRandomPet(); Pet pet = createRandomPet();
pet.setName("programmer"); pet.setName("programmer");
pet.setStatus(Pet.StatusEnum.available); pet.setStatus(Pet.StatusEnum.AVAILABLE);
api.updatePet(pet); api.updatePet(pet);
@ -108,7 +108,7 @@ public class PetApiTest {
public void testFindPetsByTags() throws Exception { public void testFindPetsByTags() throws Exception {
Pet pet = createRandomPet(); Pet pet = createRandomPet();
pet.setName("monster"); pet.setName("monster");
pet.setStatus(Pet.StatusEnum.available); pet.setStatus(Pet.StatusEnum.AVAILABLE);
List<Tag> tags = new ArrayList<Tag>(); List<Tag> tags = new ArrayList<Tag>();
Tag tag1 = new Tag(); Tag tag1 = new Tag();
@ -183,7 +183,7 @@ public class PetApiTest {
category.setName("really-happy"); category.setName("really-happy");
pet.setCategory(category); pet.setCategory(category);
pet.setStatus(Pet.StatusEnum.available); pet.setStatus(Pet.StatusEnum.AVAILABLE);
List<String> photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}); List<String> photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"});
pet.setPhotoUrls(photos); pet.setPhotoUrls(photos);

View File

@ -64,7 +64,7 @@ public class StoreApiTest {
order.setPetId(new Long(200)); order.setPetId(new Long(200));
order.setQuantity(new Integer(13)); order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date()); order.setShipDate(new java.util.Date());
order.setStatus(Order.StatusEnum.placed); order.setStatus(Order.StatusEnum.PLACED);
order.setComplete(true); order.setComplete(true);
return order; return order;