Skip to content

Commit a65e6db

Browse files
committed
Merge pull request #2226 from wing328/model_more_boolean_flag
Add more boolean flags to CodegenProperty
2 parents bd32a6a + 3cfb110 commit a65e6db

File tree

8 files changed

+168
-7
lines changed

8 files changed

+168
-7
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/AbstractGenerator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public Reader getTemplateReader(String name) {
6666
/**
6767
* Get the template file path with template dir prepended, and use the
6868
* library template if exists.
69+
*
70+
* @param config Codegen config
71+
* @param templateFile Template file
6972
*/
7073
public String getFullTemplateFile(CodegenConfig config, String templateFile) {
7174
String library = config.getLibrary();

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ public interface CodegenConfig {
147147

148148
/**
149149
* Library template (sub-template).
150+
*
151+
* @return libray template
150152
*/
151153
String getLibrary();
152154
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
public class CodegenParameter {
99
public Boolean isFormParam, isQueryParam, isPathParam, isHeaderParam,
10-
isCookieParam, isBodyParam, isFile, notFile, hasMore, isContainer,
11-
secondaryParam, isBinary, isCollectionFormatMulti;
10+
isCookieParam, isBodyParam, hasMore, isContainer,
11+
secondaryParam, isCollectionFormatMulti;
1212
public String baseName, paramName, dataType, collectionFormat, description, baseType, defaultValue;
1313
public String jsonSchema;
14+
public Boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime;
15+
public Boolean isListContainer, isMapContainer;
16+
public Boolean isFile, notFile;
1417
public boolean isEnum;
1518
public List<String> _enum;
1619
public Map<String, Object> allowableValues;
@@ -111,6 +114,17 @@ public CodegenParameter copy() {
111114
}
112115
output.vendorExtensions = this.vendorExtensions;
113116
output.isBinary = this.isBinary;
117+
output.isByteArray = this.isByteArray;
118+
output.isString = this.isString;
119+
output.isInteger = this.isInteger;
120+
output.isLong = this.isLong;
121+
output.isDouble = this.isDouble;
122+
output.isFloat = this.isFloat;
123+
output.isBoolean = this.isBoolean;
124+
output.isDate = this.isDate;
125+
output.isDateTime = this.isDateTime;
126+
output.isListContainer = this.isListContainer;
127+
output.isMapContainer = this.isMapContainer;
114128

115129
return output;
116130
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class CodegenProperty {
3333
public Boolean exclusiveMaximum;
3434
public Boolean hasMore, required, secondaryParam;
3535
public Boolean isPrimitiveType, isContainer, isNotContainer;
36+
public Boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime;
37+
public Boolean isListContainer, isMapContainer;
3638
public boolean isEnum;
3739
public Boolean isReadOnly = false;
3840
public List<String> _enum;
@@ -81,6 +83,18 @@ public int hashCode()
8183
result = prime * result + ((setter == null) ? 0 : setter.hashCode());
8284
result = prime * result + ((unescapedDescription == null) ? 0 : unescapedDescription.hashCode());
8385
result = prime * result + ((vendorExtensions == null) ? 0 : vendorExtensions.hashCode());
86+
result = prime * result + ((isString == null) ? 0 : isString.hashCode());
87+
result = prime * result + ((isInteger == null) ? 0 : isInteger.hashCode());
88+
result = prime * result + ((isLong == null) ? 0 : isLong.hashCode());
89+
result = prime * result + ((isFloat == null) ? 0 : isFloat.hashCode());
90+
result = prime * result + ((isDouble == null) ? 0 : isDouble.hashCode());
91+
result = prime * result + ((isByteArray == null) ? 0 : isByteArray.hashCode());
92+
result = prime * result + ((isBinary == null) ? 0 : isBinary.hashCode());
93+
result = prime * result + ((isBoolean == null) ? 0 : isBoolean.hashCode());
94+
result = prime * result + ((isDate == null) ? 0 : isDate.hashCode());
95+
result = prime * result + ((isDateTime == null) ? 0 : isDateTime.hashCode());
96+
result = prime * result + ((isMapContainer == null) ? 0 : isMapContainer.hashCode());
97+
result = prime * result + ((isListContainer == null) ? 0 : isListContainer.hashCode());
8498
return result;
8599
}
86100

@@ -189,6 +203,42 @@ public boolean equals(Object obj) {
189203
if (this.vendorExtensions != other.vendorExtensions && (this.vendorExtensions == null || !this.vendorExtensions.equals(other.vendorExtensions))) {
190204
return false;
191205
}
206+
if (this.isString != other.isString && (this.isString == null || !this.isString.equals(other.isString))) {
207+
return false;
208+
}
209+
if (this.isInteger != other.isInteger && (this.isInteger == null || !this.isInteger.equals(other.isInteger))) {
210+
return false;
211+
}
212+
if (this.isLong != other.isLong && (this.isLong == null || !this.isLong.equals(other.isLong))) {
213+
return false;
214+
}
215+
if (this.isFloat != other.isFloat && (this.isFloat == null || !this.isFloat.equals(other.isFloat))) {
216+
return false;
217+
}
218+
if (this.isDouble != other.isDouble && (this.isDouble == null || !this.isDouble.equals(other.isDouble))) {
219+
return false;
220+
}
221+
if (this.isByteArray != other.isByteArray && (this.isByteArray == null || !this.isByteArray.equals(other.isByteArray))) {
222+
return false;
223+
}
224+
if (this.isBoolean != other.isBoolean && (this.isBoolean == null || !this.isBoolean.equals(other.isBoolean))) {
225+
return false;
226+
}
227+
if (this.isDate != other.isDate && (this.isDate == null || !this.isDate.equals(other.isDate))) {
228+
return false;
229+
}
230+
if (this.isDateTime != other.isDateTime && (this.isDateTime == null || !this.isDateTime.equals(other.isDateTime))) {
231+
return false;
232+
}
233+
if (this.isBinary != other.isBinary && (this.isBinary == null || !this.isBinary.equals(other.isBinary))) {
234+
return false;
235+
}
236+
if (this.isListContainer != other.isListContainer && (this.isListContainer == null || !this.isListContainer.equals(other.isListContainer))) {
237+
return false;
238+
}
239+
if (this.isMapContainer != other.isMapContainer && (this.isMapContainer == null || !this.isMapContainer.equals(other.isMapContainer))) {
240+
return false;
241+
}
192242
return true;
193243
}
194244
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ public CodegenProperty fromProperty(String name, Property p) {
10201020
property.maxLength = sp.getMaxLength();
10211021
property.minLength = sp.getMinLength();
10221022
property.pattern = sp.getPattern();
1023+
property.isString = true;
10231024
if (sp.getEnum() != null) {
10241025
List<String> _enum = sp.getEnum();
10251026
property._enum = _enum;
@@ -1034,6 +1035,7 @@ public CodegenProperty fromProperty(String name, Property p) {
10341035

10351036
if (p instanceof IntegerProperty) {
10361037
IntegerProperty sp = (IntegerProperty) p;
1038+
property.isInteger = true;
10371039
if (sp.getEnum() != null) {
10381040
List<Integer> _enum = sp.getEnum();
10391041
property._enum = new ArrayList<String>();
@@ -1051,6 +1053,7 @@ public CodegenProperty fromProperty(String name, Property p) {
10511053

10521054
if (p instanceof LongProperty) {
10531055
LongProperty sp = (LongProperty) p;
1056+
property.isLong = true;
10541057
if (sp.getEnum() != null) {
10551058
List<Long> _enum = sp.getEnum();
10561059
property._enum = new ArrayList<String>();
@@ -1066,8 +1069,21 @@ public CodegenProperty fromProperty(String name, Property p) {
10661069
}
10671070
}
10681071

1072+
if (p instanceof BooleanProperty) {
1073+
property.isBoolean = true;
1074+
}
1075+
1076+
if (p instanceof BinaryProperty) {
1077+
property.isBinary = true;
1078+
}
1079+
1080+
if (p instanceof ByteArrayProperty) {
1081+
property.isByteArray = true;
1082+
}
1083+
10691084
if (p instanceof DoubleProperty) {
10701085
DoubleProperty sp = (DoubleProperty) p;
1086+
property.isDouble = true;
10711087
if (sp.getEnum() != null) {
10721088
List<Double> _enum = sp.getEnum();
10731089
property._enum = new ArrayList<String>();
@@ -1085,6 +1101,7 @@ public CodegenProperty fromProperty(String name, Property p) {
10851101

10861102
if (p instanceof FloatProperty) {
10871103
FloatProperty sp = (FloatProperty) p;
1104+
property.isFloat = true;
10881105
if (sp.getEnum() != null) {
10891106
List<Float> _enum = sp.getEnum();
10901107
property._enum = new ArrayList<String>();
@@ -1102,6 +1119,7 @@ public CodegenProperty fromProperty(String name, Property p) {
11021119

11031120
if (p instanceof DateProperty) {
11041121
DateProperty sp = (DateProperty) p;
1122+
property.isDate = true;
11051123
if (sp.getEnum() != null) {
11061124
List<String> _enum = sp.getEnum();
11071125
property._enum = new ArrayList<String>();
@@ -1119,6 +1137,7 @@ public CodegenProperty fromProperty(String name, Property p) {
11191137

11201138
if (p instanceof DateTimeProperty) {
11211139
DateTimeProperty sp = (DateTimeProperty) p;
1140+
property.isDateTime = true;
11221141
if (sp.getEnum() != null) {
11231142
List<String> _enum = sp.getEnum();
11241143
property._enum = new ArrayList<String>();
@@ -1146,6 +1165,7 @@ public CodegenProperty fromProperty(String name, Property p) {
11461165

11471166
if (p instanceof ArrayProperty) {
11481167
property.isContainer = true;
1168+
property.isListContainer = true;
11491169
property.containerType = "array";
11501170
ArrayProperty ap = (ArrayProperty) p;
11511171
CodegenProperty cp = fromProperty(property.name, ap.getItems());
@@ -1168,6 +1188,7 @@ public CodegenProperty fromProperty(String name, Property p) {
11681188
}
11691189
} else if (p instanceof MapProperty) {
11701190
property.isContainer = true;
1191+
property.isMapContainer = true;
11711192
property.containerType = "map";
11721193
MapProperty ap = (MapProperty) p;
11731194
CodegenProperty cp = fromProperty("inner", ap.getAdditionalProperties());
@@ -1575,7 +1596,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
15751596
if (null == type) {
15761597
LOGGER.warn("Type is NULL for Serializable Parameter: " + param);
15771598
}
1578-
if ("array".equals(type)) {
1599+
if ("array".equals(type)) { // for array parameter
15791600
Property inner = qp.getItems();
15801601
if (inner == null) {
15811602
LOGGER.warn("warning! No inner type supplied for array parameter \"" + qp.getName() + "\", using String");
@@ -1589,8 +1610,9 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
15891610
CodegenProperty pr = fromProperty("inner", inner);
15901611
p.baseType = pr.datatype;
15911612
p.isContainer = true;
1613+
p.isListContainer = true;
15921614
imports.add(pr.baseType);
1593-
} else if ("object".equals(type)) {
1615+
} else if ("object".equals(type)) { // for map parameter
15941616
Property inner = qp.getItems();
15951617
if (inner == null) {
15961618
LOGGER.warn("warning! No inner type supplied for map parameter \"" + qp.getName() + "\", using String");
@@ -1600,19 +1622,26 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
16001622
collectionFormat = qp.getCollectionFormat();
16011623
CodegenProperty pr = fromProperty("inner", inner);
16021624
p.baseType = pr.datatype;
1625+
p.isContainer = true;
1626+
p.isMapContainer = true;
16031627
imports.add(pr.baseType);
16041628
} else {
16051629
Map<PropertyId, Object> args = new HashMap<PropertyId, Object>();
16061630
String format = qp.getFormat();
16071631
args.put(PropertyId.ENUM, qp.getEnum());
16081632
property = PropertyBuilder.build(type, format, args);
16091633
}
1634+
16101635
if (property == null) {
16111636
LOGGER.warn("warning! Property type \"" + type + "\" not found for parameter \"" + param.getName() + "\", using String");
16121637
property = new StringProperty().description("//TODO automatically added by swagger-codegen. Type was " + type + " but not supported");
16131638
}
16141639
property.setRequired(param.getRequired());
16151640
CodegenProperty model = fromProperty(qp.getName(), property);
1641+
1642+
// set boolean flag (e.g. isString)
1643+
setParameterBooleanFlagWithCodegenProperty(p, model);
1644+
16161645
p.dataType = model.datatype;
16171646
p.isEnum = model.isEnum;
16181647
p._enum = model._enum;
@@ -1663,6 +1692,9 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
16631692
p.dataType = cp.datatype;
16641693
p.isBinary = cp.datatype.toLowerCase().startsWith("byte");
16651694
}
1695+
1696+
// set boolean flag (e.g. isString)
1697+
setParameterBooleanFlagWithCodegenProperty(p, cp);
16661698
}
16671699
} else if (model instanceof ArrayModel) {
16681700
// to use the built-in model parsing, we unwrap the ArrayModel
@@ -1678,6 +1710,10 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
16781710
imports.add(cp.baseType);
16791711
p.dataType = cp.datatype;
16801712
p.isContainer = true;
1713+
p.isListContainer = true;
1714+
1715+
// set boolean flag (e.g. isString)
1716+
setParameterBooleanFlagWithCodegenProperty(p, cp);
16811717
} else {
16821718
Model sub = bp.getSchema();
16831719
if (sub instanceof RefModel) {
@@ -2157,6 +2193,8 @@ public void setLibrary(String library) {
21572193

21582194
/**
21592195
* Library template (sub-template).
2196+
*
2197+
* @return Library template
21602198
*/
21612199
public String getLibrary() {
21622200
return library;
@@ -2221,10 +2259,10 @@ public String sanitizeName(String name) {
22212259
}
22222260

22232261
/**
2224-
* only write if the file doesn't exist
2262+
* Only write if the file doesn't exist
22252263
*
2226-
* @param outputFolder
2227-
* @param supportingFile
2264+
* @param outputFolder Output folder
2265+
* @param supportingFile Supporting file
22282266
*/
22292267
public void writeOptional(String outputFolder, SupportingFile supportingFile) {
22302268
String folder = "";
@@ -2243,4 +2281,46 @@ public void writeOptional(String outputFolder, SupportingFile supportingFile) {
22432281
supportingFiles.add(supportingFile);
22442282
}
22452283
}
2284+
2285+
/**
2286+
* Set CodegenParameter boolean flag using CodegenProperty.
2287+
*
2288+
* @param parameter Codegen Parameter
2289+
* @param property Codegen property
2290+
*/
2291+
public void setParameterBooleanFlagWithCodegenProperty(CodegenParameter parameter, CodegenProperty property) {
2292+
if (parameter == null) {
2293+
LOGGER.error("Codegen Parameter cannnot be null.");
2294+
return;
2295+
}
2296+
2297+
if (property == null) {
2298+
LOGGER.error("Codegen Property cannot be null.");
2299+
return;
2300+
}
2301+
2302+
if (Boolean.TRUE.equals(property.isString)) {
2303+
parameter.isString = true;
2304+
} else if (Boolean.TRUE.equals(property.isBoolean)) {
2305+
parameter.isBoolean = true;
2306+
} else if (Boolean.TRUE.equals(property.isLong)) {
2307+
parameter.isLong = true;
2308+
} else if (Boolean.TRUE.equals(property.isInteger)) {
2309+
parameter.isInteger = true;
2310+
} else if (Boolean.TRUE.equals(property.isDouble)) {
2311+
parameter.isDouble = true;
2312+
} else if (Boolean.TRUE.equals(property.isFloat)) {
2313+
parameter.isFloat = true;
2314+
} else if (Boolean.TRUE.equals(property.isByteArray)) {
2315+
parameter.isByteArray = true;
2316+
} else if (Boolean.TRUE.equals(property.isBinary)) {
2317+
parameter.isByteArray = true;
2318+
} else if (Boolean.TRUE.equals(property.isDate)) {
2319+
parameter.isDate = true;
2320+
} else if (Boolean.TRUE.equals(property.isDateTime)) {
2321+
parameter.isDateTime = true;
2322+
} else {
2323+
LOGGER.debug("Property type is not primitive: " + property.datatype);
2324+
}
2325+
}
22462326
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ public String toDefaultValueWithParam(String name, Property p) {
385385

386386
/**
387387
* Normalize type by wrapping primitive types with single quotes.
388+
*
389+
* @param type Primitive type
390+
* @return Normalized type
388391
*/
389392
public String normalizeType(String type) {
390393
return type.replaceAll("\\b(Boolean|Integer|Number|String|Date)\\b", "'$1'");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ public void setPackageVersion(String packageVersion) {
293293
*
294294
* (PEP 0008) Python packages should also have short, all-lowercase names,
295295
* although the use of underscores is discouraged.
296+
*
297+
* @param packageName Package name
298+
* @return Python package name that conforms to PEP 0008
296299
*/
297300
@SuppressWarnings("static-method")
298301
public String generatePackageName(String packageName) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ public String getHelp() {
211211

212212
/**
213213
* Generate Ruby module name from the gem name, e.g. use "SwaggerClient" for "swagger_client".
214+
*
215+
* @param gemName Ruby gem name
216+
* @return Ruby module naame
214217
*/
215218
@SuppressWarnings("static-method")
216219
public String generateModuleName(String gemName) {
@@ -219,6 +222,9 @@ public String generateModuleName(String gemName) {
219222

220223
/**
221224
* Generate Ruby gem name from the module name, e.g. use "swagger_client" for "SwaggerClient".
225+
*
226+
* @param moduleName Ruby module naame
227+
* @return Ruby gem name
222228
*/
223229
@SuppressWarnings("static-method")
224230
public String generateGemName(String moduleName) {

0 commit comments

Comments
 (0)