Skip to content

Commit 9094847

Browse files
authored
Merge pull request #43 from jmini/patch-20
Allow some values to be use in templates
2 parents 2387350 + 8e5a065 commit 9094847

File tree

7 files changed

+397
-81
lines changed

7 files changed

+397
-81
lines changed

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,30 @@ public void processOpts() {
160160
this.setTemplateDir((String) additionalProperties.get(CodegenConstants.TEMPLATE_DIR));
161161
}
162162

163-
//todo: replace hardcore string for constant on "CodegenConstans" class once publishing issue on codegen be resolved. (02-15-18)
164-
if (additionalProperties.containsKey(/**fixme: CodegenConstants.TEMPLATE_VERSION*/ "templateVersion")) {
165-
this.setTemplateVersion((String) additionalProperties.get(/**fixme: CodegenConstants.TEMPLATE_VERSION*/ "templateVersion"));
163+
if (additionalProperties.containsKey(CodegenConstants.TEMPLATE_VERSION)) {
164+
this.setTemplateVersion((String) additionalProperties.get(CodegenConstants.TEMPLATE_VERSION));
166165
}
167166

168167
if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
169168
this.setModelPackage((String) additionalProperties.get(CodegenConstants.MODEL_PACKAGE));
169+
} else if (StringUtils.isNotEmpty(modelPackage)) {
170+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
171+
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);
170172
}
171173

172174
if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
173175
this.setApiPackage((String) additionalProperties.get(CodegenConstants.API_PACKAGE));
176+
} else if (StringUtils.isNotEmpty(apiPackage)) {
177+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
178+
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);
174179
}
175180

176181
if (additionalProperties.containsKey(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG)) {
177182
this.setSortParamsByRequiredFlag(Boolean.valueOf(additionalProperties
178183
.get(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG).toString()));
184+
} else if (sortParamsByRequiredFlag != null) {
185+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
186+
additionalProperties.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, sortParamsByRequiredFlag);
179187
}
180188

181189
if (additionalProperties.containsKey(CodegenConstants.ENSURE_UNIQUE_PARAMS)) {
@@ -200,6 +208,14 @@ public void processOpts() {
200208
this.setSortParamsByRequiredFlag(Boolean.valueOf(additionalProperties
201209
.get(CodegenConstants.REMOVE_OPERATION_ID_PREFIX).toString()));
202210
}
211+
212+
if (additionalProperties.containsKey(CodegenConstants.HIDE_GENERATION_TIMESTAMP)) {
213+
this.setHideGenerationTimestamp(Boolean.valueOf(additionalProperties
214+
.get(CodegenConstants.HIDE_GENERATION_TIMESTAMP).toString()));
215+
} else if(hideGenerationTimestamp != null) {
216+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
217+
additionalProperties.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, hideGenerationTimestamp);
218+
}
203219
}
204220

205221
public Map<String, Object> postProcessAllModels(Map<String, Object> processedModels) {
@@ -589,6 +605,10 @@ public void setApiPackage(String apiPackage) {
589605
this.apiPackage = apiPackage;
590606
}
591607

608+
public Boolean getSortParamsByRequiredFlag() {
609+
return sortParamsByRequiredFlag;
610+
}
611+
592612
public void setSortParamsByRequiredFlag(Boolean sortParamsByRequiredFlag) {
593613
this.sortParamsByRequiredFlag = sortParamsByRequiredFlag;
594614
}
@@ -3170,6 +3190,24 @@ public String getHttpUserAgent() {
31703190
return httpUserAgent;
31713191
}
31723192

3193+
/**
3194+
* Hide generation timestamp
3195+
*
3196+
* @param hideGenerationTimestamp flag to indicates if the generation timestamp should be hidden or not
3197+
*/
3198+
public void setHideGenerationTimestamp(Boolean hideGenerationTimestamp) {
3199+
this.hideGenerationTimestamp = hideGenerationTimestamp;
3200+
}
3201+
3202+
/**
3203+
* Hide generation timestamp
3204+
*
3205+
* @return if the generation timestamp should be hidden or not
3206+
*/
3207+
public Boolean getHideGenerationTimestamp() {
3208+
return hideGenerationTimestamp;
3209+
}
3210+
31733211
@SuppressWarnings("static-method")
31743212
protected CliOption buildLibraryCliOption(Map<String, String> supportedLibraries) {
31753213
StringBuilder sb = new StringBuilder("library template (sub-template) to use:");

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

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public AbstractJavaCodegen() {
143143
cliOptions.add(CliOption.newBoolean(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, CodegenConstants
144144
.SERIALIZE_BIG_DECIMAL_AS_STRING_DESC));
145145
cliOptions.add(CliOption.newBoolean(FULL_JAVA_UTIL, "whether to use fully qualified name for classes under java.util. This option only works for Java API client"));
146-
cliOptions.add(new CliOption("hideGenerationTimestamp", "hides the timestamp when files were generated"));
146+
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC));
147147
cliOptions.add(CliOption.newBoolean(WITH_XML, "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)"));
148148

149149
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
@@ -167,20 +167,6 @@ public AbstractJavaCodegen() {
167167

168168
@Override
169169
public void processOpts() {
170-
super.processOpts();
171-
172-
modelTemplateFiles.put("model.mustache", ".java");
173-
apiTemplateFiles.put("api.mustache", ".java");
174-
apiTestTemplateFiles.put("api_test.mustache", ".java");
175-
modelDocTemplateFiles.put("model_doc.mustache", ".md");
176-
apiDocTemplateFiles.put("api_doc.mustache", ".md");
177-
178-
if (additionalProperties.containsKey(SUPPORT_JAVA6)) {
179-
this.setSupportJava6(Boolean.valueOf(additionalProperties.get(SUPPORT_JAVA6).toString()));
180-
}
181-
additionalProperties.put(SUPPORT_JAVA6, supportJava6);
182-
183-
184170
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
185171
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
186172
} else if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
@@ -195,95 +181,119 @@ public void processOpts() {
195181
this.additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, derviedInvokerPackage);
196182
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
197183
LOGGER.info("Invoker Package Name, originally not set, is now dervied from model package name: " + derviedInvokerPackage);
198-
} else {
199-
//not set, use default to be passed to template
184+
} else if (StringUtils.isNotEmpty(invokerPackage)) {
185+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
200186
additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
201187
}
202188

189+
super.processOpts();
190+
191+
modelTemplateFiles.put("model.mustache", ".java");
192+
apiTemplateFiles.put("api.mustache", ".java");
193+
apiTestTemplateFiles.put("api_test.mustache", ".java");
194+
modelDocTemplateFiles.put("model_doc.mustache", ".md");
195+
apiDocTemplateFiles.put("api_doc.mustache", ".md");
196+
197+
if (additionalProperties.containsKey(SUPPORT_JAVA6)) {
198+
this.setSupportJava6(Boolean.valueOf(additionalProperties.get(SUPPORT_JAVA6).toString()));
199+
}
200+
additionalProperties.put(SUPPORT_JAVA6, supportJava6);
201+
203202
if (additionalProperties.containsKey(CodegenConstants.GROUP_ID)) {
204203
this.setGroupId((String) additionalProperties.get(CodegenConstants.GROUP_ID));
205-
} else {
206-
//not set, use to be passed to template
204+
} else if(StringUtils.isNotEmpty(groupId)) {
205+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
207206
additionalProperties.put(CodegenConstants.GROUP_ID, groupId);
208207
}
209208

210209
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_ID)) {
211210
this.setArtifactId((String) additionalProperties.get(CodegenConstants.ARTIFACT_ID));
212-
} else {
213-
//not set, use to be passed to template
211+
} else if(StringUtils.isNotEmpty(artifactId)) {
212+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
214213
additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId);
215214
}
216215

217216
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) {
218217
this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION));
219-
} else {
220-
//not set, use to be passed to template
218+
} else if(StringUtils.isNotEmpty(artifactVersion)) {
219+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
221220
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
222221
}
223222

224223
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_URL)) {
225224
this.setArtifactUrl((String) additionalProperties.get(CodegenConstants.ARTIFACT_URL));
226-
} else {
225+
} else if(StringUtils.isNoneEmpty(artifactUrl)) {
226+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
227227
additionalProperties.put(CodegenConstants.ARTIFACT_URL, artifactUrl);
228228
}
229229

230230
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_DESCRIPTION)) {
231231
this.setArtifactDescription((String) additionalProperties.get(CodegenConstants.ARTIFACT_DESCRIPTION));
232-
} else {
232+
} else if(StringUtils.isNoneEmpty(artifactDescription)) {
233+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
233234
additionalProperties.put(CodegenConstants.ARTIFACT_DESCRIPTION, artifactDescription);
234235
}
235236

236237
if (additionalProperties.containsKey(CodegenConstants.SCM_CONNECTION)) {
237238
this.setScmConnection((String) additionalProperties.get(CodegenConstants.SCM_CONNECTION));
238-
} else {
239+
} else if(StringUtils.isNoneEmpty(scmConnection)) {
240+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
239241
additionalProperties.put(CodegenConstants.SCM_CONNECTION, scmConnection);
240242
}
241243

242244
if (additionalProperties.containsKey(CodegenConstants.SCM_DEVELOPER_CONNECTION)) {
243245
this.setScmDeveloperConnection((String) additionalProperties.get(CodegenConstants.SCM_DEVELOPER_CONNECTION));
244-
} else {
246+
} else if(StringUtils.isNoneEmpty(scmDeveloperConnection)) {
247+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
245248
additionalProperties.put(CodegenConstants.SCM_DEVELOPER_CONNECTION, scmDeveloperConnection);
246249
}
247250

248251
if (additionalProperties.containsKey(CodegenConstants.SCM_URL)) {
249252
this.setScmUrl((String) additionalProperties.get(CodegenConstants.SCM_URL));
250-
} else {
253+
} else if(StringUtils.isNoneEmpty(scmUrl)) {
254+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
251255
additionalProperties.put(CodegenConstants.SCM_URL, scmUrl);
252256
}
253257

254258
if (additionalProperties.containsKey(CodegenConstants.DEVELOPER_NAME)) {
255259
this.setDeveloperName((String) additionalProperties.get(CodegenConstants.DEVELOPER_NAME));
256-
} else {
260+
} else if(StringUtils.isNoneEmpty(developerName)) {
261+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
257262
additionalProperties.put(CodegenConstants.DEVELOPER_NAME, developerName);
258263
}
259264

260265
if (additionalProperties.containsKey(CodegenConstants.DEVELOPER_EMAIL)) {
261266
this.setDeveloperEmail((String) additionalProperties.get(CodegenConstants.DEVELOPER_EMAIL));
262-
} else {
267+
} else if(StringUtils.isNoneEmpty(developerEmail)) {
268+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
263269
additionalProperties.put(CodegenConstants.DEVELOPER_EMAIL, developerEmail);
264270
}
265271

266272
if (additionalProperties.containsKey(CodegenConstants.DEVELOPER_ORGANIZATION)) {
267273
this.setDeveloperOrganization((String) additionalProperties.get(CodegenConstants.DEVELOPER_ORGANIZATION));
268-
} else {
274+
} else if(StringUtils.isNoneEmpty(developerOrganization)) {
275+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
269276
additionalProperties.put(CodegenConstants.DEVELOPER_ORGANIZATION, developerOrganization);
270277
}
271278

272279
if (additionalProperties.containsKey(CodegenConstants.DEVELOPER_ORGANIZATION_URL)) {
273280
this.setDeveloperOrganizationUrl((String) additionalProperties.get(CodegenConstants.DEVELOPER_ORGANIZATION_URL));
274-
} else {
281+
} else if(StringUtils.isNoneEmpty(developerOrganizationUrl)) {
282+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
275283
additionalProperties.put(CodegenConstants.DEVELOPER_ORGANIZATION_URL, developerOrganizationUrl);
276284
}
277285

278286
if (additionalProperties.containsKey(CodegenConstants.LICENSE_NAME)) {
279287
this.setLicenseName((String) additionalProperties.get(CodegenConstants.LICENSE_NAME));
280-
} else {
288+
} else if(StringUtils.isNoneEmpty(licenseName)) {
289+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
281290
additionalProperties.put(CodegenConstants.LICENSE_NAME, licenseName);
282291
}
283292

284293
if (additionalProperties.containsKey(CodegenConstants.LICENSE_URL)) {
285294
this.setLicenseUrl((String) additionalProperties.get(CodegenConstants.LICENSE_URL));
286-
} else {
295+
} else if(StringUtils.isNoneEmpty(licenseUrl)) {
296+
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
287297
additionalProperties.put(CodegenConstants.LICENSE_URL, licenseUrl);
288298
}
289299

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue;
55
import static java.util.Collections.sort;
66

7-
import com.fasterxml.jackson.databind.JsonNode;
87
import io.swagger.codegen.CliOption;
9-
import io.swagger.codegen.CodegenArgument;
108
import io.swagger.codegen.CodegenConstants;
119
import io.swagger.codegen.CodegenModel;
1210
import io.swagger.codegen.CodegenOperation;
@@ -18,23 +16,25 @@
1816
import io.swagger.codegen.languages.features.GzipFeatures;
1917
import io.swagger.codegen.languages.features.PerformBeanValidationFeatures;
2018

21-
import io.swagger.v3.core.util.Yaml;
22-
import org.apache.commons.io.IOUtils;
2319
import org.apache.commons.lang3.BooleanUtils;
2420
import org.apache.commons.lang3.StringUtils;
2521
import org.slf4j.Logger;
2622
import org.slf4j.LoggerFactory;
2723

2824
import java.io.File;
29-
import java.io.IOException;
30-
import java.io.InputStream;
31-
import java.util.*;
25+
import java.util.ArrayList;
26+
import java.util.Comparator;
27+
import java.util.HashMap;
28+
import java.util.Iterator;
29+
import java.util.LinkedHashMap;
30+
import java.util.LinkedList;
31+
import java.util.List;
32+
import java.util.Map;
3233
import java.util.regex.Pattern;
3334

3435
public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, PerformBeanValidationFeatures, GzipFeatures {
3536
static final String MEDIA_TYPE = "mediaType";
3637

37-
@SuppressWarnings("hiding")
3838
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
3939

4040
public static final String USE_RX_JAVA = "useRxJava";

0 commit comments

Comments
 (0)