Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public void processSwagger(Swagger swagger) {
// override with any special handling of the JMustache compiler
@SuppressWarnings("unused")
public Compiler processCompiler(Compiler compiler) {
return compiler;
return compiler;
}

// override with any special text escaping logic
Expand Down Expand Up @@ -1680,7 +1680,7 @@ public CodegenProperty fromProperty(String name, Property p) {

property.baseType = getSwaggerType(p);

if (p instanceof ArrayProperty) {
if (p instanceof ArrayProperty) {
property.isContainer = true;
property.isListContainer = true;
property.containerType = "array";
Expand All @@ -1689,7 +1689,7 @@ public CodegenProperty fromProperty(String name, Property p) {
ArrayProperty ap = (ArrayProperty) p;
CodegenProperty cp = fromProperty(property.name, ap.getItems());
updatePropertyForArray(property, cp);
} else if (p instanceof MapProperty) {
} else if (p instanceof MapProperty) {
property.isContainer = true;
property.isMapContainer = true;
property.containerType = "map";
Expand Down Expand Up @@ -1876,7 +1876,7 @@ protected Response findMethodResponse(Map<String, Response> responses) {
* @return Codegen Operation object
*/
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
return fromOperation(path, httpMethod, operation, definitions, null);
return fromOperation(path, httpMethod, operation, definitions, null);
}

/**
Expand Down Expand Up @@ -2531,7 +2531,7 @@ public boolean isDataTypeFile(String dataType) {
@SuppressWarnings("static-method")
public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes) {
if (schemes == null) {
return Collections.emptyList();
return Collections.emptyList();
}

List<CodegenSecurity> secs = new ArrayList<CodegenSecurity>(schemes.size());
Expand All @@ -2555,8 +2555,8 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
sec.isKeyInHeader = sec.isKeyInQuery = sec.isApiKey = sec.isOAuth = false;
sec.isBasic = true;
} else {
final OAuth2Definition oauth2Definition = (OAuth2Definition) schemeDefinition;
sec.isKeyInHeader = sec.isKeyInQuery = sec.isApiKey = sec.isBasic = false;
final OAuth2Definition oauth2Definition = (OAuth2Definition) schemeDefinition;
sec.isKeyInHeader = sec.isKeyInQuery = sec.isApiKey = sec.isBasic = false;
sec.isOAuth = true;
sec.flow = oauth2Definition.getFlow();
if (sec.flow == null) {
Expand Down Expand Up @@ -3191,11 +3191,11 @@ public String sanitizeName(String name) {
// encountered so far and hopefully make it easier for others to add more special
// cases in the future.

// better error handling when map/array type is invalid
if (name == null) {
LOGGER.error("String to be sanitized is null. Default to ERROR_UNKNOWN");
return "ERROR_UNKNOWN";
}
// better error handling when map/array type is invalid
if (name == null) {
LOGGER.error("String to be sanitized is null. Default to ERROR_UNKNOWN");
return "ERROR_UNKNOWN";
}

// if the name is just '$', map it to 'value' for the time being.
if ("$".equals(name)) {
Expand Down Expand Up @@ -3416,11 +3416,25 @@ public String addRegularExpressionDelimiter(String pattern) {
public boolean convertPropertyToBooleanAndWriteBack(String propertyKey) {
boolean booleanValue = false;
if (additionalProperties.containsKey(propertyKey)) {
booleanValue = Boolean.valueOf(additionalProperties.get(propertyKey).toString());
booleanValue = convertPropertyToBoolean(propertyKey);
// write back as boolean
additionalProperties.put(propertyKey, booleanValue);
writePropertyBack(propertyKey, booleanValue);
}

return booleanValue;
}


public boolean convertPropertyToBoolean(String propertyKey) {
boolean booleanValue = false;
if (additionalProperties.containsKey(propertyKey)) {
booleanValue = Boolean.valueOf(additionalProperties.get(propertyKey).toString());
}

return booleanValue;
}

public void writePropertyBack(String propertyKey, boolean value) {
additionalProperties.put(propertyKey, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.SupportingFile;
import io.swagger.codegen.languages.features.BeanValidationFeatures;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.properties.Property;
import io.swagger.util.Json;
import org.apache.commons.io.FileUtils;

public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen
{
public JavaJAXRSSpecServerCodegen()
{
public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen implements BeanValidationFeatures
{
protected boolean useBeanValidation = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we keep useBeanValidation to false similar to what we've done for other Java-related generator: https://github.com/swagger-api/swagger-codegen/search?l=Java&q=useBeanValidation&utf8=%E2%9C%93

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as #4506, please advise if the decision is to keep the default to false (current impl of cxf/java) or true (suggestion of @cbornet)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfiala I'll think about this more and get back to you.


public JavaJAXRSSpecServerCodegen()
{
super();
invokerPackage = "io.swagger.api";
artifactId = "swagger-jaxrs-server";
Expand Down Expand Up @@ -67,26 +71,37 @@ public JavaJAXRSSpecServerCodegen()
library.setEnum(supportedLibraries);

cliOptions.add(library);
}

@Override
public void processOpts()
{
super.processOpts();

supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen

cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));
}

@Override
public void processOpts()
{
super.processOpts();

if (additionalProperties.containsKey(USE_BEANVALIDATION)) {
this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION));
}

if (useBeanValidation) {
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
}

supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen
writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));

writeOptional(outputFolder, new SupportingFile("RestApplication.mustache",
(sourceFolder + '/' + invokerPackage).replace(".", "/"), "RestApplication.java"));

}
}


@Override
public String getName()
{
return "jaxrs-spec";
}
@Override
public String getName()
{
return "jaxrs-spec";
}

@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
Expand Down Expand Up @@ -127,21 +142,26 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
model.imports.remove("JsonProperty");
}

@Override
@Override
public void preprocessSwagger(Swagger swagger) {
//copy input swagger to output folder
try {
String swaggerJson = Json.pretty(swagger);
//copy input swagger to output folder
try {
String swaggerJson = Json.pretty(swagger);
FileUtils.writeStringToFile(new File(outputFolder + File.separator + "swagger.json"), swaggerJson);
} catch (IOException e) {
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e.getCause());
}
super.preprocessSwagger(swagger);
}
super.preprocessSwagger(swagger);

}
@Override
public String getHelp()
{
return "Generates a Java JAXRS Server according to JAXRS 2.0 specification.";
}

public void setUseBeanValidation(boolean useBeanValidation) {
this.useBeanValidation = useBeanValidation;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import javax.ws.rs.core.Response;
import io.swagger.annotations.*;

import java.util.List;
{{#useBeanValidation}}
import javax.validation.constraints.*;
{{/useBeanValidation}}

@Path("/{{baseName}}")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{{#required}}
@NotNull
{{/required}}
{{#pattern}}
@Pattern(regexp="{{pattern}}")
{{/pattern}}
{{#minLength}}
{{#maxLength}}
@Size(min={{minLength}},max={{maxLength}})
{{/maxLength}}
{{/minLength}}
{{#minLength}}
{{^maxLength}}
@Size(min={{minLength}})
{{/maxLength}}
{{/minLength}}
{{^minLength}}
{{#maxLength}}
@Size(max={{maxLength}})
{{/maxLength}}
{{/minLength}}
{{#minItems}}
{{#maxItems}}
@Size(min={{minItems}},max={{maxItems}})
{{/maxItems}}
{{/minItems}}
{{#minItems}}
{{^maxItems}}
@Size(min={{minItems}})
{{/maxItems}}
{{/minItems}}
{{^minItems}}
{{#maxItems}}
@Size(max={{maxItems}})
{{/maxItems}}
{{/minItems}}
{{! check for integer / number=decimal type}}
{{#isInteger}}
{{#minimum}}
@Min({{minimum}})
{{/minimum}}
{{#maximum}}
@Max({{maximum}})
{{/maximum}}
{{/isInteger}}
{{^isInteger}}
{{#minimum}}
@DecimalMin("{{minimum}}")
{{/minimum}}
{{#maximum}}
@DecimalMax("{{maximum}}")
{{/maximum}}
{{/isInteger}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{! PathParam is always required, no @NotNull necessary }}{{#pattern}} @Pattern(regexp="{{pattern}}"){{/pattern}}{{#minLength}}{{#maxLength}} @Size(min={{minLength}},max={{maxLength}}){{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} @Size(min={{minLength}}){{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} @Size(max={{maxLength}}){{/maxLength}}{{/minLength}}{{#minItems}}{{#maxItems}} @Size(min={{minItems}},max={{maxItems}}){{/maxItems}}{{/minItems}}{{#minItems}}{{^maxItems}} @Size(min={{minItems}}){{/maxItems}}{{/minItems}}{{^minItems}}{{#maxItems}} @Size(max={{maxItems}}){{/maxItems}}{{/minItems}}{{#minimum}} @Min({{minimum}}){{/minimum}}{{#maximum}} @Max({{maximum}}){{/maximum}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#required}} @NotNull{{/required}}{{#pattern}} @Pattern(regexp="{{pattern}}"){{/pattern}}{{#minLength}}{{#maxLength}} @Size(min={{minLength}},max={{maxLength}}){{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} @Size(min={{minLength}}){{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} @Size(max={{maxLength}}){{/maxLength}}{{/minLength}}{{#minItems}}{{#maxItems}} @Size(min={{minItems}},max={{maxItems}}){{/maxItems}}{{/minItems}}{{#minItems}}{{^maxItems}} @Size(min={{minItems}}){{/maxItems}}{{/minItems}}{{^minItems}}{{#maxItems}} @Size(max={{maxItems}}){{/maxItems}}{{/minItems}}{{#minimum}} @Min({{minimum}}){{/minimum}}{{#maximum}} @Max({{maximum}}){{/maximum}}
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlType;
public enum {{datatypeWithEnum}} {

{{#allowableValues}}
{{#enumVars}}{{name}}({{datatype}}.valueOf({{{value}}})){{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/enumVars}}
{{/allowableValues}}


private {{datatype}} value;

{{datatypeWithEnum}} ({{datatype}} v) {
value = v;
}

@XmlType(name="{{classname}}")
@XmlEnum
public enum {{classname}} {
{{#allowableValues}}{{.}}{{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/allowableValues}}

public String value() {
return name();
return value;
}

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

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

/**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
*/
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
{{#gson}}
{{#allowableValues}}{{#enumVars}}
@SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
{{{name}}}({{{value}}}){{^-last}},
{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}
{{/gson}}
{{^gson}}
{{#allowableValues}}{{#enumVars}}
{{{name}}}({{{value}}}){{^-last}},
{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}
{{/gson}}

private {{{dataType}}} value;

{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}({{{dataType}}} value) {
this.value = value;
}

@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,2 +1,2 @@
{{#isFormParam}}{{#notFile}}@FormParam(value = "{{paramName}}"{{^required}}, required = false{{/required}}) {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}} @FormParam(value = "{{paramName}}"{{^required}}, required = false{{/required}}) InputStream {{paramName}}InputStream,
@FormParam(value = "{{paramName}}" {{^required}}, required = false{{/required}}) Attachment {{paramName}}Detail{{/isFile}}{{/isFormParam}}
{{#isFormParam}}{{#notFile}}@FormParam(value = "{{paramName}}") {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}} @FormParam(value = "{{paramName}}") InputStream {{paramName}}InputStream,
@FormParam(value = "{{paramName}}") Attachment {{paramName}}Detail{{/isFile}}{{/isFormParam}}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}")
{{^hideGenerationTimestamp}}
@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}")
{{/hideGenerationTimestamp}}
Loading