Skip to content

Commit

Permalink
Merge pull request #10924 from swagger-api/generator-updates
Browse files Browse the repository at this point in the history
Generator updates
  • Loading branch information
HugoMario authored Mar 2, 2021
2 parents 35adbd5 + bc9691e commit 067d980
Show file tree
Hide file tree
Showing 1,184 changed files with 10,864 additions and 6,898 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.regex.Pattern;

import io.swagger.models.properties.UntypedProperty;
import io.swagger.util.Yaml;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -401,6 +400,15 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj

// override to post-process any model properties
@SuppressWarnings("unused")
public void postProcessModelProperties(CodegenModel model){
if (model.vars == null || model.vars.isEmpty()) {
return;
}
for(CodegenProperty codegenProperty : model.vars) {
postProcessModelProperty(model, codegenProperty);
}
}

public void postProcessModelProperty(CodegenModel model, CodegenProperty property){
}

Expand Down Expand Up @@ -1546,11 +1554,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
addVars(m, impl.getProperties(), impl.getRequired(), allDefinitions);
}

if (m.vars != null) {
for(CodegenProperty prop : m.vars) {
postProcessModelProperty(m, prop);
}
}
postProcessModelProperties(m);
return m;
}

Expand Down Expand Up @@ -2784,26 +2788,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
} else {
Model sub = bp.getSchema();
if (sub instanceof RefModel) {
String name = ((RefModel) sub).getSimpleRef();
try {
name = URLDecoder.decode(name, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
LOGGER.error("Could not decoded string: " + name, e);
}
name = getAlias(name);
if (typeMapping.containsKey(name)) {
name = typeMapping.get(name);
p.baseType = name;
} else {
name = toModelName(name);
p.baseType = name;
if (defaultIncludes.contains(name)) {
imports.add(name);
}
imports.add(name);
name = getTypeDeclaration(name);
}
p.dataType = name;
readRefModelParameter((RefModel) model, p, imports);
} else {
if (sub instanceof ComposedModel) {
p.dataType = "object";
Expand Down Expand Up @@ -2913,6 +2898,29 @@ public boolean isDataTypeFile(String dataType) {
}
}

protected void readRefModelParameter(RefModel refModel, CodegenParameter codegenParameter, Set<String> imports) {
String name = refModel.getSimpleRef();
try {
name = URLDecoder.decode(name, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
LOGGER.error("Could not decoded string: " + name, e);
}
name = getAlias(name);
if (typeMapping.containsKey(name)) {
name = typeMapping.get(name);
codegenParameter.baseType = name;
} else {
name = toModelName(name);
codegenParameter.baseType = name;
if (defaultIncludes.contains(name)) {
imports.add(name);
}
imports.add(name);
name = getTypeDeclaration(name);
}
codegenParameter.dataType = name;
}

/**
* Convert map of Swagger SecuritySchemeDefinition objects to a list of Codegen Security objects
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,13 @@ public Map<String, String> createMapping(String key, String value) {

@Override
public String toEnumValue(String value, String datatype) {
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
if ("int".equals(datatype) || "int32".equals(datatype) || "int64".equals(datatype)
|| "uint".equals(datatype) || "uint32".equals(datatype) || "uint64".equals(datatype)
|| "float".equals(datatype) || "float32".equals(datatype) || "float64".equals(datatype)
|| "boolean".equals(datatype) || "double".equals(datatype)) {
return value;
} else if ("string".equals(datatype)) {
return String.format("\"%s\"", value);
} else {
return escapeText(value);
}
Expand All @@ -477,11 +482,16 @@ public String toEnumVarName(String name, String datatype) {
}

// number
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
if ("int".equals(datatype) || "int32".equals(datatype) || "int64".equals(datatype)
|| "uint".equals(datatype) || "uint32".equals(datatype) || "uint64".equals(datatype)
|| "float".equals(datatype) || "float32".equals(datatype) || "float64".equals(datatype)) {
String varName = name;
varName = varName.replaceAll("-", "MINUS_");
varName = varName.replaceAll("\\+", "PLUS_");
varName = varName.replaceAll("\\.", "_DOT_");
if (varName.matches("\\d.*")) {
return "_" + varName;
}
return varName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.codegen.*;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.RefModel;
import io.swagger.models.Swagger;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.Property;
Expand All @@ -15,6 +16,9 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

import static org.apache.commons.lang3.StringUtils.isEmpty;
Expand Down Expand Up @@ -583,6 +587,28 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
return codegenModel;
}

@Override
protected void readRefModelParameter(RefModel refModel, CodegenParameter codegenParameter, Set<String> imports) {
String name = getSwaggerType(new RefProperty(refModel.get$ref()));

try {
name = URLDecoder.decode(name, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
LOGGER.error("Could not decoded string: " + name, e);
}
name = getAlias(name);
if (typeMapping.containsKey(name)) {
name = typeMapping.get(name);
} else {
if (defaultIncludes.contains(name)) {
imports.add(name);
}
}
codegenParameter.baseType = name;
codegenParameter.dataType = name;
}


public void setOptionalProjectFileFlag(boolean flag) {
this.optionalProjectFileFlag = flag;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package io.swagger.codegen.languages;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
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.CodegenParameter;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;
import io.swagger.util.Json;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen
{
Expand Down Expand Up @@ -57,6 +59,7 @@ public JavaJAXRSSpecServerCodegen()
typeMapping.put("date", "LocalDate");

importMapping.put("LocalDate", "org.joda.time.LocalDate");
importMapping.put("InputStream", "java.io.InputStream");

super.embeddedTemplateDir = templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "spec";

Expand Down Expand Up @@ -162,6 +165,33 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
model.imports.remove("JsonProperty");
}

@Override
public void postProcessModelProperties(CodegenModel model){
if (model.vars == null || model.vars.isEmpty()) {
return;
}
final boolean useJackson = Boolean.valueOf(String.valueOf(additionalProperties.get("jackson")));
boolean hasEnumProperty = false;
for(CodegenProperty codegenProperty : model.vars) {
postProcessModelProperty(model, codegenProperty);
if (codegenProperty.isEnum) {
hasEnumProperty = true;
}
}
if (useJackson && hasEnumProperty) {
model.imports.add("JsonValue");
model.imports.add("JsonCreator");
}
}

public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
final CodegenParameter parameter = super.fromParameter(param, imports);
if (parameter.isFile) {
imports.add("InputStream");
}
return parameter;
}

@Override
public void preprocessSwagger(Swagger swagger) {
//copy input swagger to output folder
Expand All @@ -175,8 +205,7 @@ public void preprocessSwagger(Swagger swagger) {

}
@Override
public String getHelp()
{
public String getHelp() {
return "Generates a Java JAXRS Server according to JAXRS 2.0 specification.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("LocalDateProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateProvider.java"));
}
writeOptional(outputFolder, new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,12 @@ public class ApiClient {
builder = httpClient.resource(url).accept(accept);
}

for (Entry<String, String> keyValue : headerParams.entrySet()) {
builder = builder.header(keyValue.getKey(), keyValue.getValue());
for (String key : headerParams.keySet()) {
builder = builder.header(key, headerParams.get(key));
}
for (Map.Entry<String,String> keyValue : defaultHeaderMap.entrySet()) {
if (!headerParams.containsKey(keyValue.getKey())) {
builder = builder.header(keyValue.getKey(), keyValue.getValue());
for (String key : defaultHeaderMap.keySet()) {
if (!headerParams.containsKey(key)) {
builder = builder.header(key, defaultHeaderMap.get(key));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum

@Override
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
{{{dataType}}} value = {{#isNumber}}new BigDecimal(jsonReader.nextDouble()){{/isNumber}}{{^isNumber}}jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}}{{/isNumber}};
{{#isNumber}}BigDecimal value = new BigDecimal(jsonReader.nextDouble()){{/isNumber}}{{^isNumber}}{{#isInteger}}Integer value {{/isInteger}}{{^isInteger}}String value {{/isInteger}}= jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}nextString(){{/isInteger}}{{/isNumber}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.fromValue(String.valueOf(value));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@

@Override
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
{{{dataType}}} value = {{#isNumber}}new BigDecimal(jsonReader.nextDouble()){{/isNumber}}{{^isNumber}}jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}}{{/isNumber}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.fromValue(String.valueOf(value));
{{#isNumber}}Object value = new BigDecimal(jsonReader.nextDouble()){{/isNumber}}{{^isNumber}}{{#isInteger}}int value {{/isInteger}}{{^isInteger}}String value {{/isInteger}}= jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}nextString(){{/isInteger}}{{/isNumber}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.fromValue(String.valueOf(value));
}
}
{{/gson}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package {{package}};
{{/imports}}
{{#useBeanValidation}}
import javax.validation.constraints.*;
import javax.validation.Valid;
{{/useBeanValidation}}
{{#models}}
{{#model}}{{#description}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<!-- Version is just a guess -->
<!-- IBM WebSphere Application Server Liberty Core v8.5.5.6 uses v3.0.2, source https://www.ibm.com/support/knowledgecenter/SSD28V_8.5.5/com.ibm.websphere.wlp.core.doc/ae/rwlp_jaxrs_secure.html -->
<version>3.0.2</version>
<!-- TOMCAT EE JaxRS v8.0.6 uses v3.0.2, https://www.apache.org/dyn/closer.cgi/tomee/tomee-8.0.6/ -->
<version>3.3.8</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM jboss/wildfly:21.0.2.Final

ADD target/{{artifactId}}-{{artifactVersion}}.war /opt/jboss/wildfly/standalone/deployments/

EXPOSE 8080 9990 8009

CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0", "-c", "standalone.xml"]
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ is an example of building a swagger-enabled JAX-RS server.

This example uses the [JAX-RS](https://jax-rs-spec.java.net/) framework.

To run the server, please execute the following:
To run with docker, please execute the following:

```
mvn clean package jetty:run
mvn package

docker build -q --rm --tag=swagger-codegen-resteasy .

docker run -d -p 8080:8080 -p 9990:9990 -p 8009:8009 --name swagger-resteasy -it swagger-codegen-resteasy
```

You can then view the swagger listing here:

```
http://localhost:{{serverPort}}{{contextPath}}/swagger.json
```

Note that if you have configured the `host` to be something other than localhost, the calls through
swagger-ui will be directed to that host and not localhost!
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,6 @@
<version>${resteasy-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>${resteasy-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-validator-provider-11</artifactId>
<version>${resteasy-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
Expand Down Expand Up @@ -192,7 +180,7 @@
<properties>
<swagger-core-version>1.5.24</swagger-core-version>
<jetty-version>9.3.27.v20190418</jetty-version>
<resteasy-version>3.0.11.Final</resteasy-version>
<resteasy-version>3.13.2.Final</resteasy-version>
<slf4j-version>1.6.3</slf4j-version>
<junit-version>4.13.1</junit-version>
<servlet-api-version>2.5</servlet-api-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum {{datatypeWithEnum}} {
value = v;
}

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

Expand Down
Loading

0 comments on commit 067d980

Please sign in to comment.