Skip to content

Commit 4d8afe9

Browse files
committed
added handlebars helper classes
1 parent f6baef5 commit 4d8afe9

File tree

10 files changed

+305
-8
lines changed

10 files changed

+305
-8
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.swagger.codegen.handlebars.helpers;
2+
3+
import com.github.jknack.handlebars.Helper;
4+
import com.github.jknack.handlebars.Options;
5+
import com.github.jknack.handlebars.Options.Buffer;
6+
import io.swagger.codegen.VendorExtendable;
7+
8+
import java.io.IOException;
9+
import java.util.Map;
10+
11+
public abstract class ExtensionHelper implements Helper<VendorExtendable> {
12+
13+
public abstract String getPreffix();
14+
15+
@Override
16+
public Object apply(VendorExtendable vendor, Options options) throws IOException {
17+
final Buffer buffer = options.buffer();
18+
19+
if (vendor == null) {
20+
buffer.append(options.inverse());
21+
return buffer;
22+
}
23+
final String param = options.param(0);
24+
String extension = getPreffix() + param;
25+
26+
if (!getBooleanValue(vendor, extension)) {
27+
buffer.append(options.inverse());
28+
} else {
29+
buffer.append(options.fn());
30+
}
31+
return buffer;
32+
}
33+
34+
public static boolean getBooleanValue(VendorExtendable vendorExtendable, String extensionKey) {
35+
if (vendorExtendable == null) {
36+
return false;
37+
}
38+
Map<String, Object> vendorExtensions = vendorExtendable.getVendorExtensions();
39+
if (vendorExtensions.get(extensionKey) == null) {
40+
return false;
41+
}
42+
return Boolean.parseBoolean(vendorExtensions.get(extensionKey).toString());
43+
}
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.swagger.codegen.handlebars.helpers;
2+
3+
import static io.swagger.codegen.VendorExtendable.PREFIX_HAS;
4+
5+
public class HasHelper extends ExtensionHelper {
6+
7+
public static final String NAME = "has";
8+
9+
@Override
10+
public String getPreffix() {
11+
return PREFIX_HAS;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.swagger.codegen.handlebars.helpers;
2+
3+
import io.swagger.codegen.VendorExtendable;
4+
5+
public class HasNotHelper extends NoneExtensionHelper {
6+
7+
public static final String NAME = "hasNot";
8+
9+
@Override
10+
public String getPreffix() {
11+
return VendorExtendable.PREFIX_HAS;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.swagger.codegen.handlebars.helpers;
2+
3+
import static io.swagger.codegen.VendorExtendable.PREFIX_IS;
4+
5+
public class IsHelper extends ExtensionHelper {
6+
7+
public static final String NAME = "is";
8+
9+
@Override
10+
public String getPreffix() {
11+
return PREFIX_IS;
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.swagger.codegen.handlebars.helpers;
2+
3+
import io.swagger.codegen.VendorExtendable;
4+
5+
public class IsNotHelper extends NoneExtensionHelper {
6+
7+
public static final String NAME = "isNot";
8+
9+
10+
@Override
11+
public String getPreffix() {
12+
return VendorExtendable.PREFIX_IS;
13+
}
14+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package io.swagger.codegen.handlebars.helpers;
2+
3+
import com.github.jknack.handlebars.Options;
4+
import io.swagger.codegen.CodegenConstants;
5+
import io.swagger.codegen.CodegenModel;
6+
import io.swagger.codegen.CodegenProperty;
7+
import io.swagger.codegen.languages.JavaClientCodegen;
8+
import org.apache.commons.lang3.StringUtils;
9+
10+
import java.io.IOException;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue;
15+
16+
public class JavaHelper {
17+
18+
public CharSequence getClassDefinition(CodegenModel codegenModel, Options options) throws IOException {
19+
final Boolean serializableModel = options.get(CodegenConstants.SERIALIZABLE_MODEL) != null ? options.get(CodegenConstants.SERIALIZABLE_MODEL) : Boolean.FALSE;
20+
final Boolean parceableModel = options.get(JavaClientCodegen.PARCELABLE_MODEL) != null ? options.get(JavaClientCodegen.PARCELABLE_MODEL) : Boolean.FALSE;
21+
final StringBuilder builder = new StringBuilder();
22+
builder.append(codegenModel.classname);
23+
if (StringUtils.isNotBlank(codegenModel.parent)) {
24+
builder.append(StringUtils.SPACE);
25+
builder.append("extends ");
26+
builder.append(codegenModel.parent);
27+
}
28+
if (parceableModel && serializableModel) {
29+
builder.append(" implements Parcelable, Serializable");
30+
} else {
31+
if (serializableModel) {
32+
builder.append(" implements Serializable");
33+
}
34+
}
35+
return builder.toString();
36+
}
37+
38+
public CharSequence getJavaProperty(CodegenProperty codegenProperty, Options options) throws IOException {
39+
final StringBuilder builder = new StringBuilder();
40+
if (getBooleanValue(codegenProperty, CodegenConstants.IS_CONTAINER_EXT_NAME)) {
41+
builder.append(codegenProperty.getDatatypeWithEnum());
42+
builder.append(StringUtils.SPACE);
43+
builder.append(codegenProperty.getName());
44+
builder.append(" = ");
45+
if (codegenProperty.getRequired()) {
46+
builder.append(codegenProperty.getDefaultValue());
47+
} else {
48+
builder.append("null");
49+
}
50+
return builder.toString();
51+
}
52+
return String.format("%s %s = %s", codegenProperty.getDatatypeWithEnum(), codegenProperty.getName(), codegenProperty.getDefaultValue());
53+
}
54+
55+
public CharSequence getModelImports(Map<String, Object> templateData, Options options) throws IOException {
56+
if (options == null) {
57+
return null;
58+
}
59+
final List<Map<String, String>> imports = options.get("imports");
60+
if (imports == null || imports.isEmpty()) {
61+
return null;
62+
}
63+
final StringBuilder builder = new StringBuilder();
64+
65+
boolean supportJava6 = Boolean.valueOf(String.valueOf(templateData.get("supportJava6")));
66+
if (supportJava6) {
67+
builder.append("import org.apache.commons.lang3.ObjectUtils;\n");
68+
} else {
69+
builder.append("import java.util.Objects;\n");
70+
}
71+
72+
for (Map<String, String> importMap : imports) {
73+
builder.append("import ");
74+
builder.append(importMap.get("import"));
75+
builder.append(";\n");
76+
}
77+
boolean serializableMode = Boolean.valueOf(String.valueOf(templateData.get("serializableModel")));
78+
boolean jackson = Boolean.valueOf(String.valueOf(templateData.get("jackson")));
79+
boolean withXml = Boolean.valueOf(String.valueOf(templateData.get("withXml")));
80+
boolean parcelableModel = Boolean.valueOf(String.valueOf(templateData.get("parcelableModel")));
81+
boolean useBeanValidation = Boolean.valueOf(String.valueOf(templateData.get("useBeanValidation")));
82+
if (serializableMode) {
83+
builder.append("import java.io.Serializable;\n");
84+
}
85+
if (jackson && withXml) {
86+
builder.append("import com.fasterxml.jackson.dataformat.xml.annotation.*;\n");
87+
}
88+
if (withXml) {
89+
builder.append("import javax.xml.bind.annotation.*;\n");
90+
}
91+
if (parcelableModel) {
92+
builder.append("import android.os.Parcelable;\n");
93+
builder.append("import android.os.Parcel;\n");
94+
}
95+
if (useBeanValidation) {
96+
builder.append("import javax.validation.constraints.*;\n");
97+
builder.append("import javax.validation.Valid;\n");
98+
}
99+
return builder.toString();
100+
}
101+
102+
public CharSequence getXmlAttributeName(String xmlName, String baseName) {
103+
if (StringUtils.isNotBlank(xmlName)) {
104+
return xmlName;
105+
}
106+
return baseName;
107+
}
108+
109+
public CharSequence getXmlElementName(String xmlNamespace, String xmlName, String baseName) {
110+
StringBuilder builder = new StringBuilder();
111+
if (StringUtils.isNotBlank(xmlNamespace)) {
112+
builder.append("namespace=\"");
113+
builder.append(xmlNamespace);
114+
builder.append("\", ");
115+
}
116+
builder.append("name=\"");
117+
if (StringUtils.isNotBlank(xmlName)) {
118+
builder.append(xmlName);
119+
} else {
120+
builder.append(baseName);
121+
}
122+
builder.append("\"");
123+
return builder.toString();
124+
}
125+
126+
public CharSequence getJacksonXmlProperty(CodegenProperty codegenProperty) {
127+
final boolean isXmlAttribute = getBooleanValue(codegenProperty, CodegenConstants.IS_XML_ATTRIBUTE_EXT_NAME);
128+
StringBuilder builder = new StringBuilder();
129+
if (isXmlAttribute) {
130+
builder.append("isAttribute = true, ");
131+
}
132+
if (StringUtils.isNotBlank(codegenProperty.getXmlNamespace())) {
133+
builder.append("namespace=\"");
134+
builder.append(codegenProperty.getXmlNamespace());
135+
builder.append("\", ");
136+
}
137+
builder.append("localName = \"");
138+
if (StringUtils.isNotBlank(codegenProperty.getXmlName())) {
139+
builder.append(codegenProperty.getXmlNamespace());
140+
} else {
141+
builder.append(codegenProperty.getBaseName());
142+
}
143+
builder.append("\"");
144+
return builder.toString();
145+
}
146+
147+
public CharSequence getJacksonXmlElementWrapper(CodegenProperty codegenProperty) {
148+
final boolean isXmlWrapped = getBooleanValue(codegenProperty, CodegenConstants.IS_XML_WRAPPED_EXT_NAME);
149+
StringBuilder builder = new StringBuilder();
150+
builder.append("useWrapping = ");
151+
builder.append(isXmlWrapped);
152+
builder.append(", ");
153+
if (StringUtils.isNotBlank(codegenProperty.getXmlNamespace())) {
154+
builder.append("namespace=\"");
155+
builder.append(codegenProperty.getXmlNamespace());
156+
builder.append("\", ");
157+
}
158+
builder.append("localName = \"");
159+
if (StringUtils.isNotBlank(codegenProperty.getXmlName())) {
160+
builder.append(codegenProperty.getXmlNamespace());
161+
} else {
162+
builder.append(codegenProperty.getBaseName());
163+
}
164+
builder.append("\"");
165+
return builder.toString();
166+
}
167+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.swagger.codegen.handlebars.helpers;
2+
3+
import com.github.jknack.handlebars.Helper;
4+
import com.github.jknack.handlebars.Options;
5+
import io.swagger.codegen.VendorExtendable;
6+
7+
import java.io.IOException;
8+
9+
import static io.swagger.codegen.VendorExtendable.PREFIX_IS;
10+
import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue;
11+
12+
public abstract class NoneExtensionHelper implements Helper<VendorExtendable> {
13+
14+
public abstract String getPreffix();
15+
16+
@Override
17+
public Object apply(VendorExtendable vendor, Options options) throws IOException {
18+
final Options.Buffer buffer = options.buffer();
19+
if (vendor == null) {
20+
buffer.append(options.fn());
21+
return buffer;
22+
}
23+
final String param = options.param(0);
24+
String extension = getPreffix() + param;
25+
26+
if (!getBooleanValue(vendor, extension)) {
27+
buffer.append(options.fn());
28+
} else {
29+
buffer.append(options.inverse());
30+
}
31+
return buffer;
32+
}
33+
}

src/main/java/io/swagger/codegen/languages/DefaultCodegenConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
import io.swagger.codegen.CodegenResponse;
1616
import io.swagger.codegen.CodegenSecurity;
1717
import io.swagger.codegen.SupportingFile;
18-
import io.swagger.codegen.languages.helpers.HasHelper;
19-
import io.swagger.codegen.languages.helpers.HasNotHelper;
20-
import io.swagger.codegen.languages.helpers.IsHelper;
21-
import io.swagger.codegen.languages.helpers.IsNotHelper;
18+
import io.swagger.codegen.handlebars.helpers.HasHelper;
19+
import io.swagger.codegen.handlebars.helpers.HasNotHelper;
20+
import io.swagger.codegen.handlebars.helpers.IsHelper;
21+
import io.swagger.codegen.handlebars.helpers.IsNotHelper;
2222
import io.swagger.codegen.utils.ModelUtils;
2323
import io.swagger.v3.core.util.Json;
2424
import io.swagger.v3.core.util.Yaml;
@@ -89,7 +89,7 @@
8989
import static io.swagger.codegen.languages.CodegenHelper.getImportMappings;
9090
import static io.swagger.codegen.languages.CodegenHelper.getTypeMappings;
9191
import static io.swagger.codegen.languages.CodegenHelper.initalizeSpecialCharacterMapping;
92-
import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue;
92+
import static io.swagger.codegen.handlebars.helpers.ExtensionHelper.getBooleanValue;
9393
import static io.swagger.codegen.utils.ModelUtils.processCodegenModels;
9494

9595
public abstract class DefaultCodegenConfig implements CodegenConfig {

src/main/java/io/swagger/codegen/languages/java/AbstractJavaCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import io.swagger.codegen.CodegenParameter;
99
import io.swagger.codegen.CodegenProperty;
1010
import io.swagger.codegen.languages.DefaultCodegenConfig;
11-
import io.swagger.codegen.languages.helpers.JavaHelper;
11+
import io.swagger.codegen.handlebars.helpers.JavaHelper;
1212
import io.swagger.v3.oas.models.OpenAPI;
1313
import io.swagger.v3.oas.models.Operation;
1414
import io.swagger.v3.oas.models.PathItem;
@@ -37,7 +37,7 @@
3737

3838
import static io.swagger.codegen.CodegenConstants.HAS_ENUMS_EXT_NAME;
3939
import static io.swagger.codegen.CodegenConstants.IS_ENUM_EXT_NAME;
40-
import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue;
40+
import static io.swagger.codegen.handlebars.helpers.ExtensionHelper.getBooleanValue;
4141

4242
public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
4343
static Logger LOGGER = LoggerFactory.getLogger(AbstractJavaCodegen.class);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import static io.swagger.codegen.CodegenConstants.HAS_ENUMS_EXT_NAME;
2525
import static io.swagger.codegen.CodegenConstants.IS_ENUM_EXT_NAME;
26-
import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue;
26+
import static io.swagger.codegen.handlebars.helpers.ExtensionHelper.getBooleanValue;
2727

2828
public class JavaInflectorServerCodegen extends AbstractJavaCodegen {
2929

0 commit comments

Comments
 (0)