Skip to content

Commit ad8aa7d

Browse files
pablolazarowing328
authored andcommitted
[Java] adds snapshotVersion CLI option and uses API version as artifactVersion by default (#2033)
* [Java]: adds snapshotVersion CLI option and uses API version as artifactVersion by default * fix some typos * fix naming diff between branches * ensure-up-to-date * update samples
1 parent 8d5b600 commit ad8aa7d

File tree

19 files changed

+107
-1
lines changed

19 files changed

+107
-1
lines changed

docs/generators/groovy.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ sidebar_label: groovy
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|configPackage|configuration package for generated code| |null|

docs/generators/java-inflector.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ sidebar_label: java-inflector
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|

docs/generators/java-msf4j.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: java-msf4j
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|implFolder|folder for generated implementation code| |null|
4546
|title|a title describing the application| |null|
4647
|useBeanValidation|Use BeanValidation API annotations| |true|

docs/generators/java-pkmst.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: java-pkmst
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|groupId|groupId in generated pom.xml| |null|
4546
|artifactId|artifactId in generated pom.xml| |null|
4647
|artifactVersion|artifact version in generated pom.xml| |null|

docs/generators/java-play-framework.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: java-play-framework
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|title|server title name or client service name| |null|
4546
|configPackage|configuration package for generated code| |null|
4647
|basePackage|base package for generated code| |null|

docs/generators/java-undertow-server.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ sidebar_label: java-undertow-server
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|

docs/generators/java-vertx.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ sidebar_label: java-vertx
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|rxInterface|When specified, API interfaces are generated with RX and methods return Single<> and Comparable.| |false|
4546
|vertxSwaggerRouterVersion|Specify the version of the swagger router library| |null|

docs/generators/java.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: java
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|useRxJava|Whether to use the RxJava adapter with the retrofit2 library.| |false|
4546
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library.| |false|
4647
|parcelableModel|Whether to generate models for Android that implement Parcelable with the okhttp-gson library.| |false|

docs/generators/jaxrs-cxf-cdi.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: jaxrs-cxf-cdi
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|implFolder|folder for generated implementation code| |null|
4546
|title|a title describing the application| |null|
4647
|useBeanValidation|Use BeanValidation API annotations| |true|

docs/generators/jaxrs-cxf-client.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: jaxrs-cxf-client
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|useBeanValidation|Use BeanValidation API annotations| |false|
4546
|useGzipFeatureForTests|Use Gzip Feature for tests| |false|
4647
|useLoggingFeatureForTests|Use Logging Feature for tests| |false|

docs/generators/jaxrs-cxf.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: jaxrs-cxf
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|implFolder|folder for generated implementation code| |null|
4546
|title|a title describing the application| |null|
4647
|useBeanValidation|Use BeanValidation API annotations| |true|

docs/generators/jaxrs-jersey.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: jaxrs-jersey
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|implFolder|folder for generated implementation code| |null|
4546
|title|a title describing the application| |null|
4647
|useBeanValidation|Use BeanValidation API annotations| |true|

docs/generators/jaxrs-resteasy-eap.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: jaxrs-resteasy-eap
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|implFolder|folder for generated implementation code| |null|
4546
|title|a title describing the application| |null|
4647
|useBeanValidation|Use BeanValidation API annotations| |true|

docs/generators/jaxrs-resteasy.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: jaxrs-resteasy
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|implFolder|folder for generated implementation code| |null|
4546
|title|a title describing the application| |null|
4647
|useBeanValidation|Use BeanValidation API annotations| |true|

docs/generators/jaxrs-spec.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: jaxrs-spec
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|implFolder|folder for generated implementation code| |null|
4546
|title|a title describing the application| |null|
4647
|useBeanValidation|Use BeanValidation API annotations| |true|

docs/generators/spring.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sidebar_label: spring
4141
|parentGroupId|parent groupId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4242
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
4343
|parentVersion|parent version in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
44+
|snapshotVersion|Uses a SNAPSHOT version.| |null|
4445
|title|server title name or client service name| |null|
4546
|configPackage|configuration package for generated code| |null|
4647
|basePackage|base package (invokerPackage) for generated code| |null|

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,14 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
283283
public static final String ENABLE_POST_PROCESS_FILE_DESC = "Enable post-processing file using environment variables.";
284284

285285
public static final String OPEN_API_SPEC_NAME = "openAPISpecName";
286-
286+
287287
public static final String GENERATE_ALIAS_AS_MODEL = "generateAliasAsModel";
288288
public static final String GENERATE_ALIAS_AS_MODEL_DESC = "Generate alias to map, array as models";
289289

290290
public static final String USE_COMPARE_NET_OBJECTS = "useCompareNetObjects";
291291
public static final String USE_COMPARE_NET_OBJECTS_DESC = "Use KellermanSoftware.CompareNetObjects for deep recursive object comparison. WARNING: this option incurs potential performance impact.";
292+
293+
public static final String SNAPSHOT_VERSION = "snapshotVersion";
294+
public static final String SNAPSHOT_VERSION_DESC = "Uses a SNAPSHOT version.";
295+
292296
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public AbstractJavaCodegen() {
191191
cliOptions.add(CliOption.newString(CodegenConstants.PARENT_GROUP_ID, CodegenConstants.PARENT_GROUP_ID_DESC));
192192
cliOptions.add(CliOption.newString(CodegenConstants.PARENT_ARTIFACT_ID, CodegenConstants.PARENT_ARTIFACT_ID_DESC));
193193
cliOptions.add(CliOption.newString(CodegenConstants.PARENT_VERSION, CodegenConstants.PARENT_VERSION_DESC));
194+
cliOptions.add(CliOption.newString(CodegenConstants.SNAPSHOT_VERSION, CodegenConstants.SNAPSHOT_VERSION_DESC));
194195
}
195196

196197
@Override
@@ -264,11 +265,21 @@ public void processOpts() {
264265

265266
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) {
266267
this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION));
268+
} else if (this.getVersionFromSpecification() != null) {
269+
this.setArtifactVersion(this.getVersionFromSpecification());
267270
} else {
268271
//not set, use to be passed to template
269272
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
270273
}
271274

275+
if (additionalProperties.containsKey(CodegenConstants.SNAPSHOT_VERSION)) {
276+
Boolean useSnapshotVersion = Boolean.valueOf((String) additionalProperties.get(CodegenConstants.SNAPSHOT_VERSION));
277+
278+
if (useSnapshotVersion) {
279+
this.setArtifactVersion(this.buildSnapshotVersion(this.artifactVersion));
280+
}
281+
}
282+
272283
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_URL)) {
273284
this.setArtifactUrl((String) additionalProperties.get(CodegenConstants.ARTIFACT_URL));
274285
} else {
@@ -1340,6 +1351,29 @@ private String deriveInvokerPackageName(String input) {
13401351
return sb.toString();
13411352
}
13421353

1354+
/**
1355+
* Gets version from API specification.
1356+
*
1357+
* @return API version
1358+
*/
1359+
private String getVersionFromSpecification () {
1360+
if (this.openAPI != null && this.openAPI.getInfo() != null) {
1361+
return this.openAPI.getInfo().getVersion();
1362+
} else {
1363+
return null;
1364+
}
1365+
}
1366+
1367+
/**
1368+
* Builds a SNAPSHOT version from a given version.
1369+
*
1370+
* @param version
1371+
* @return SNAPSHOT version
1372+
*/
1373+
private String buildSnapshotVersion (String version) {
1374+
return version + "-" + "SNAPSHOT";
1375+
}
1376+
13431377
public void setSupportJava6(boolean value) {
13441378
this.supportJava6 = value;
13451379
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,52 @@ public void apiDocFileFolder() {
198198
codegen.setOutputDir("/User/open.api.tools");
199199
Assert.assertEquals(codegen.apiDocFileFolder(), "/User/open.api.tools/docs/".replace('/', File.separatorChar));
200200
}
201+
202+
@Test(description = "tests if API version specification is used if no version is provided in additional properties")
203+
public void openApiversionTest() {
204+
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
205+
206+
OpenAPI api = TestUtils.createOpenAPI();
207+
codegen.setOpenAPI(api);
208+
209+
codegen.processOpts();
210+
211+
Assert.assertEquals(codegen.getArtifactVersion(), "1.0.7");
212+
}
213+
214+
@Test(description = "tests if artifactVersion additional property is used")
215+
public void additionalPropertyArtifactVersionTest() {
216+
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
217+
218+
codegen.additionalProperties().put("artifactVersion", "1.1.1");
219+
220+
OpenAPI api = TestUtils.createOpenAPI();
221+
codegen.setOpenAPI(api);
222+
223+
codegen.processOpts();
224+
225+
Assert.assertEquals(codegen.getArtifactVersion(), "1.1.1");
226+
}
227+
228+
@Test(description = "tests if default version is used when neither OpenAPI version nor artifactVersion additional property has been provided")
229+
public void defautlVersionTest() {
230+
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
231+
232+
codegen.processOpts();
233+
234+
Assert.assertEquals(codegen.getArtifactVersion(), "1.0.0");
235+
}
236+
237+
@Test(description = "tests if default version is used when neither OpenAPI version nor artifactVersion additional property has been provided")
238+
public void snapshotVersionTest() {
239+
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
240+
241+
codegen.additionalProperties().put("snapshotVersion", "true");
242+
243+
codegen.processOpts();
244+
245+
Assert.assertEquals(codegen.getArtifactVersion(), "1.0.0-SNAPSHOT");
246+
}
201247

202248
private static class P_AbstractJavaCodegen extends AbstractJavaCodegen {
203249
@Override
@@ -214,5 +260,11 @@ public String getName() {
214260
public String getHelp() {
215261
return null;
216262
}
263+
264+
/**
265+
* Gets artifact version.
266+
* Only for testing purposes.
267+
*/
268+
public String getArtifactVersion () { return this.artifactVersion; }
217269
}
218270
}

0 commit comments

Comments
 (0)