Skip to content

[Java][Spring][Inflector][Jax-RS] To fix various enum issues #3615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/jaxrs-jersey1-petstore-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
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"
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"

echo "Removing files and folders under samples/server/petstore/jaxrs/jersey1/src/main"
rm -rf samples/server/petstore/jaxrs/jersey1/src/main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public AbstractJavaJAXRSServerCodegen()
modelPackage = "io.swagger.model";

additionalProperties.put("title", title);
// java inflector uses the jackson lib
additionalProperties.put("jackson", "true");

cliOptions.add(new CliOption(CodegenConstants.IMPL_FOLDER, CodegenConstants.IMPL_FOLDER_DESC));
cliOptions.add(new CliOption("title", "a title describing the application"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public JavaInflectorServerCodegen() {
modelPackage = System.getProperty("swagger.codegen.inflector.modelpackage", "io.swagger.model");

additionalProperties.put("title", title);
// java inflector uses the jackson lib
additionalProperties.put("jackson", "true");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Swagger;
import org.apache.commons.lang3.BooleanUtils;

import java.io.File;
import java.util.*;
Expand Down Expand Up @@ -42,6 +41,9 @@ public SpringCodegen() {
additionalProperties.put(CONFIG_PACKAGE, configPackage);
additionalProperties.put(BASE_PACKAGE, basePackage);

// spring uses the jackson lib
additionalProperties.put("jackson", "true");

cliOptions.add(new CliOption(TITLE, "server title name or client service name"));
cliOptions.add(new CliOption(CONFIG_PACKAGE, "configuration package for generated code"));
cliOptions.add(new CliOption(BASE_PACKAGE, "base package for generated code"));
Expand Down Expand Up @@ -352,17 +354,22 @@ public void setSingleContentTypes(boolean singleContentTypes) {
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);

if("null".equals(property.example)) {
if ("null".equals(property.example)) {
property.example = null;
}

//Add imports for Jackson
if(!BooleanUtils.toBoolean(model.isEnum)) {
if (!Boolean.TRUE.equals(model.isEnum)) {
model.imports.add("JsonProperty");

if(BooleanUtils.toBoolean(model.hasEnums)) {
if (Boolean.TRUE.equals(model.hasEnums)) {
model.imports.add("JsonValue");
}
} else { // enum class
//Needed imports for Jackson's JsonCreator
if (additionalProperties.containsKey("jackson")) {
model.imports.add("JsonCreator");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@
}

@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{{#jackson}}
import com.fasterxml.jackson.annotation.JsonCreator;
{{/jackson}}

/**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
*/
Expand All @@ -21,7 +25,18 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
}

@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@
}

@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{{#jackson}}
import com.fasterxml.jackson.annotation.JsonCreator;
{{/jackson}}

/**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
*/
Expand All @@ -21,7 +25,18 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
}

@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {{package}}.{{classname}}Service;
import {{package}}.factories.{{classname}}ServiceFactory;

import io.swagger.annotations.ApiParam;
import io.swagger.jaxrs.*;

import com.sun.jersey.multipart.FormDataParam;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@
}

@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{{#jackson}}
import com.fasterxml.jackson.annotation.JsonCreator;
{{/jackson}}

/**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
*/
Expand All @@ -21,7 +25,18 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
}

@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.swagger.api;

import io.swagger.model.Pet;
import io.swagger.model.ModelApiResponse;
import java.io.File;
import io.swagger.model.ModelApiResponse;

import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
@EnableConfigurationProperties
public class ClientConfiguration {

@Value("${ swaggerPetstore.security.apiKey.key:}")
private String apiKeyKey;

@Bean
@ConditionalOnProperty(name = "swaggerPetstore.security.apiKey.key")
public ApiKeyRequestInterceptor apiKeyRequestInterceptor() {
return new ApiKeyRequestInterceptor("header", "api_key", this.apiKeyKey);
}

@Bean
@ConditionalOnProperty("swaggerPetstore.security.petstoreAuth.client-id")
public OAuth2FeignRequestInterceptor petstoreAuthRequestInterceptor() {
Expand All @@ -37,13 +46,4 @@ public ImplicitResourceDetails petstoreAuthResourceDetails() {
return details;
}

@Value("${ swaggerPetstore.security.apiKey.key:}")
private String apiKeyKey;

@Bean
@ConditionalOnProperty(name = "swaggerPetstore.security.apiKey.key")
public ApiKeyRequestInterceptor apiKeyRequestInterceptor() {
return new ApiKeyRequestInterceptor("header", "api_key", this.apiKeyKey);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
*/

public class Category {
@JsonProperty("id")
private Long id = null;

@JsonProperty("name")
private String name = null;

public Category id(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
*/

public class ModelApiResponse {
@JsonProperty("code")
private Integer code = null;

@JsonProperty("type")
private String type = null;

@JsonProperty("message")
private String message = null;

public ModelApiResponse code(Integer code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
*/

public class Order {
@JsonProperty("id")
private Long id = null;

@JsonProperty("petId")
private Long petId = null;

@JsonProperty("quantity")
private Integer quantity = null;

@JsonProperty("shipDate")
private DateTime shipDate = null;

/**
Expand All @@ -41,13 +45,26 @@ public enum StatusEnum {
}

@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static StatusEnum fromValue(String text) {
for (StatusEnum b : StatusEnum.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}

@JsonProperty("status")
private StatusEnum status = null;

@JsonProperty("complete")
private Boolean complete = false;

public Order id(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
*/

public class Pet {
@JsonProperty("id")
private Long id = null;

@JsonProperty("category")
private Category category = null;

@JsonProperty("name")
private String name = null;

@JsonProperty("photoUrls")
private List<String> photoUrls = new ArrayList<String>();

@JsonProperty("tags")
private List<Tag> tags = new ArrayList<Tag>();

/**
Expand All @@ -46,11 +51,23 @@ public enum StatusEnum {
}

@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static StatusEnum fromValue(String text) {
for (StatusEnum b : StatusEnum.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}

@JsonProperty("status")
private StatusEnum status = null;

public Pet id(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
*/

public class Tag {
@JsonProperty("id")
private Long id = null;

@JsonProperty("name")
private String name = null;

public Tag id(Long id) {
Expand Down
Loading