Skip to content

Commit 4e20bd9

Browse files
authored
[Java][Spring][Inflector][Jax-RS] To fix various enum issues (#3615)
* fix spring enum deserialization issue * fix enum class for spring * update java inflector to fix enum tostring * fix jaxrs jersey1 enum toString * fix jaxrs jersey patch issue
1 parent 64e0342 commit 4e20bd9

File tree

187 files changed

+3960
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+3960
-229
lines changed

bin/jaxrs-jersey1-petstore-server.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fi
2626

2727
# if you've executed sbt assembly previously it will use that instead.
2828
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29-
ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaJaxRS/libraries/jersey1 -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l jaxrs -o samples/server/petstore/jaxrs/jersey1 -DhideGenerationTimestamp=true --library=jersey1 --artifact-id=swagger-jaxrs-jersey1-server"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaJaxRS/libraries/jersey1 -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l jaxrs -o samples/server/petstore/jaxrs/jersey1 -DhideGenerationTimestamp=true --library=jersey1 --artifact-id=swagger-jaxrs-jersey1-server"
3030

3131
echo "Removing files and folders under samples/server/petstore/jaxrs/jersey1/src/main"
3232
rm -rf samples/server/petstore/jaxrs/jersey1/src/main

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaJAXRSServerCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public AbstractJavaJAXRSServerCodegen()
3333
modelPackage = "io.swagger.model";
3434

3535
additionalProperties.put("title", title);
36+
// java inflector uses the jackson lib
37+
additionalProperties.put("jackson", "true");
3638

3739
cliOptions.add(new CliOption(CodegenConstants.IMPL_FOLDER, CodegenConstants.IMPL_FOLDER_DESC));
3840
cliOptions.add(new CliOption("title", "a title describing the application"));

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaInflectorServerCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public JavaInflectorServerCodegen() {
3939
modelPackage = System.getProperty("swagger.codegen.inflector.modelpackage", "io.swagger.model");
4040

4141
additionalProperties.put("title", title);
42+
// java inflector uses the jackson lib
43+
additionalProperties.put("jackson", "true");
4244
}
4345

4446
@Override

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io.swagger.models.Operation;
55
import io.swagger.models.Path;
66
import io.swagger.models.Swagger;
7-
import org.apache.commons.lang3.BooleanUtils;
87

98
import java.io.File;
109
import java.util.*;
@@ -42,6 +41,9 @@ public SpringCodegen() {
4241
additionalProperties.put(CONFIG_PACKAGE, configPackage);
4342
additionalProperties.put(BASE_PACKAGE, basePackage);
4443

44+
// spring uses the jackson lib
45+
additionalProperties.put("jackson", "true");
46+
4547
cliOptions.add(new CliOption(TITLE, "server title name or client service name"));
4648
cliOptions.add(new CliOption(CONFIG_PACKAGE, "configuration package for generated code"));
4749
cliOptions.add(new CliOption(BASE_PACKAGE, "base package for generated code"));
@@ -352,17 +354,22 @@ public void setSingleContentTypes(boolean singleContentTypes) {
352354
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
353355
super.postProcessModelProperty(model, property);
354356

355-
if("null".equals(property.example)) {
357+
if ("null".equals(property.example)) {
356358
property.example = null;
357359
}
358360

359361
//Add imports for Jackson
360-
if(!BooleanUtils.toBoolean(model.isEnum)) {
362+
if (!Boolean.TRUE.equals(model.isEnum)) {
361363
model.imports.add("JsonProperty");
362364

363-
if(BooleanUtils.toBoolean(model.hasEnums)) {
365+
if (Boolean.TRUE.equals(model.hasEnums)) {
364366
model.imports.add("JsonValue");
365367
}
368+
} else { // enum class
369+
//Needed imports for Jackson's JsonCreator
370+
if (additionalProperties.containsKey("jackson")) {
371+
model.imports.add("JsonCreator");
372+
}
366373
}
367374
}
368375

modules/swagger-codegen/src/main/resources/JavaInflector/enumClass.mustache

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,18 @@
2727
}
2828

2929
@Override
30+
@JsonValue
3031
public String toString() {
3132
return String.valueOf(value);
3233
}
34+
35+
@JsonCreator
36+
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
37+
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
38+
if (String.valueOf(b.value).equals(text)) {
39+
return b;
40+
}
41+
}
42+
return null;
43+
}
3344
}

modules/swagger-codegen/src/main/resources/JavaInflector/enumOuterClass.mustache

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
{{#jackson}}
2+
import com.fasterxml.jackson.annotation.JsonCreator;
3+
{{/jackson}}
4+
15
/**
26
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
37
*/
@@ -21,7 +25,18 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
2125
}
2226

2327
@Override
28+
@JsonValue
2429
public String toString() {
2530
return String.valueOf(value);
2631
}
32+
33+
@JsonCreator
34+
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
35+
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
36+
if (String.valueOf(b.value).equals(text)) {
37+
return b;
38+
}
39+
}
40+
return null;
41+
}
2742
}

modules/swagger-codegen/src/main/resources/JavaJaxRS/enumClass.mustache

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,18 @@
2727
}
2828

2929
@Override
30+
@JsonValue
3031
public String toString() {
3132
return String.valueOf(value);
3233
}
34+
35+
@JsonCreator
36+
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
37+
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
38+
if (String.valueOf(b.value).equals(text)) {
39+
return b;
40+
}
41+
}
42+
return null;
43+
}
3344
}

modules/swagger-codegen/src/main/resources/JavaJaxRS/enumOuterClass.mustache

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
{{#jackson}}
2+
import com.fasterxml.jackson.annotation.JsonCreator;
3+
{{/jackson}}
4+
15
/**
26
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
37
*/
@@ -21,7 +25,18 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
2125
}
2226

2327
@Override
28+
@JsonValue
2429
public String toString() {
2530
return String.valueOf(value);
2631
}
32+
33+
@JsonCreator
34+
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
35+
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
36+
if (String.valueOf(b.value).equals(text)) {
37+
return b;
38+
}
39+
}
40+
return null;
41+
}
2742
}

modules/swagger-codegen/src/main/resources/JavaJaxRS/libraries/jersey1/api.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {{package}}.{{classname}}Service;
55
import {{package}}.factories.{{classname}}ServiceFactory;
66

77
import io.swagger.annotations.ApiParam;
8+
import io.swagger.jaxrs.*;
89

910
import com.sun.jersey.multipart.FormDataParam;
1011

modules/swagger-codegen/src/main/resources/JavaSpring/enumClass.mustache

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,18 @@
2727
}
2828

2929
@Override
30+
@JsonValue
3031
public String toString() {
3132
return String.valueOf(value);
3233
}
34+
35+
@JsonCreator
36+
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
37+
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
38+
if (String.valueOf(b.value).equals(text)) {
39+
return b;
40+
}
41+
}
42+
return null;
43+
}
3344
}

modules/swagger-codegen/src/main/resources/JavaSpring/enumOuterClass.mustache

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
{{#jackson}}
2+
import com.fasterxml.jackson.annotation.JsonCreator;
3+
{{/jackson}}
4+
15
/**
26
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
37
*/
@@ -21,7 +25,18 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
2125
}
2226

2327
@Override
28+
@JsonValue
2429
public String toString() {
2530
return String.valueOf(value);
2631
}
32+
33+
@JsonCreator
34+
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
35+
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
36+
if (String.valueOf(b.value).equals(text)) {
37+
return b;
38+
}
39+
}
40+
return null;
41+
}
2742
}

samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/PetApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.swagger.api;
22

33
import io.swagger.model.Pet;
4-
import io.swagger.model.ModelApiResponse;
54
import java.io.File;
5+
import io.swagger.model.ModelApiResponse;
66

77
import io.swagger.annotations.*;
88
import org.springframework.http.ResponseEntity;

samples/client/petstore/spring-cloud/src/main/java/io/swagger/configuration/ClientConfiguration.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
@EnableConfigurationProperties
2323
public class ClientConfiguration {
2424

25+
@Value("${ swaggerPetstore.security.apiKey.key:}")
26+
private String apiKeyKey;
27+
28+
@Bean
29+
@ConditionalOnProperty(name = "swaggerPetstore.security.apiKey.key")
30+
public ApiKeyRequestInterceptor apiKeyRequestInterceptor() {
31+
return new ApiKeyRequestInterceptor("header", "api_key", this.apiKeyKey);
32+
}
33+
2534
@Bean
2635
@ConditionalOnProperty("swaggerPetstore.security.petstoreAuth.client-id")
2736
public OAuth2FeignRequestInterceptor petstoreAuthRequestInterceptor() {
@@ -37,13 +46,4 @@ public ImplicitResourceDetails petstoreAuthResourceDetails() {
3746
return details;
3847
}
3948

40-
@Value("${ swaggerPetstore.security.apiKey.key:}")
41-
private String apiKeyKey;
42-
43-
@Bean
44-
@ConditionalOnProperty(name = "swaggerPetstore.security.apiKey.key")
45-
public ApiKeyRequestInterceptor apiKeyRequestInterceptor() {
46-
return new ApiKeyRequestInterceptor("header", "api_key", this.apiKeyKey);
47-
}
48-
4949
}

samples/client/petstore/spring-cloud/src/main/java/io/swagger/model/Category.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
*/
1515

1616
public class Category {
17+
@JsonProperty("id")
1718
private Long id = null;
1819

20+
@JsonProperty("name")
1921
private String name = null;
2022

2123
public Category id(Long id) {

samples/client/petstore/spring-cloud/src/main/java/io/swagger/model/ModelApiResponse.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
*/
1515

1616
public class ModelApiResponse {
17+
@JsonProperty("code")
1718
private Integer code = null;
1819

20+
@JsonProperty("type")
1921
private String type = null;
2022

23+
@JsonProperty("message")
2124
private String message = null;
2225

2326
public ModelApiResponse code(Integer code) {

samples/client/petstore/spring-cloud/src/main/java/io/swagger/model/Order.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
*/
1717

1818
public class Order {
19+
@JsonProperty("id")
1920
private Long id = null;
2021

22+
@JsonProperty("petId")
2123
private Long petId = null;
2224

25+
@JsonProperty("quantity")
2326
private Integer quantity = null;
2427

28+
@JsonProperty("shipDate")
2529
private DateTime shipDate = null;
2630

2731
/**
@@ -41,13 +45,26 @@ public enum StatusEnum {
4145
}
4246

4347
@Override
48+
@JsonValue
4449
public String toString() {
4550
return String.valueOf(value);
4651
}
52+
53+
@JsonCreator
54+
public static StatusEnum fromValue(String text) {
55+
for (StatusEnum b : StatusEnum.values()) {
56+
if (String.valueOf(b.value).equals(text)) {
57+
return b;
58+
}
59+
}
60+
return null;
61+
}
4762
}
4863

64+
@JsonProperty("status")
4965
private StatusEnum status = null;
5066

67+
@JsonProperty("complete")
5168
private Boolean complete = false;
5269

5370
public Order id(Long id) {

samples/client/petstore/spring-cloud/src/main/java/io/swagger/model/Pet.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@
1919
*/
2020

2121
public class Pet {
22+
@JsonProperty("id")
2223
private Long id = null;
2324

25+
@JsonProperty("category")
2426
private Category category = null;
2527

28+
@JsonProperty("name")
2629
private String name = null;
2730

31+
@JsonProperty("photoUrls")
2832
private List<String> photoUrls = new ArrayList<String>();
2933

34+
@JsonProperty("tags")
3035
private List<Tag> tags = new ArrayList<Tag>();
3136

3237
/**
@@ -46,11 +51,23 @@ public enum StatusEnum {
4651
}
4752

4853
@Override
54+
@JsonValue
4955
public String toString() {
5056
return String.valueOf(value);
5157
}
58+
59+
@JsonCreator
60+
public static StatusEnum fromValue(String text) {
61+
for (StatusEnum b : StatusEnum.values()) {
62+
if (String.valueOf(b.value).equals(text)) {
63+
return b;
64+
}
65+
}
66+
return null;
67+
}
5268
}
5369

70+
@JsonProperty("status")
5471
private StatusEnum status = null;
5572

5673
public Pet id(Long id) {

samples/client/petstore/spring-cloud/src/main/java/io/swagger/model/Tag.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
*/
1515

1616
public class Tag {
17+
@JsonProperty("id")
1718
private Long id = null;
1819

20+
@JsonProperty("name")
1921
private String name = null;
2022

2123
public Tag id(Long id) {

0 commit comments

Comments
 (0)