Skip to content

Commit 70108b7

Browse files
karismannwing328
authored andcommitted
[JAVA] fix artifactVersion is not taken from specification (#2798)
* [JAVA] fix artifactVersion is not taken from specification when not provided by generator option * update jaxrs-spec samples * update docs generator jaxrs
1 parent 0ef5793 commit 70108b7

File tree

15 files changed

+186
-59
lines changed

15 files changed

+186
-59
lines changed

docs/generators/java-msf4j.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ sidebar_label: java-msf4j
4545
|implFolder|folder for generated implementation code| |src/main/java|
4646
|title|a title describing the application| |OpenAPI Server|
4747
|useBeanValidation|Use BeanValidation API annotations| |true|
48-
|serverPort|The port on which the server should be started| |null|
48+
|serverPort|The port on which the server should be started| |8080|
4949
|library|library template (sub-template)|<dl><dt>**jersey1**</dt><dd>Jersey core 1.x</dd><dt>**jersey2**</dt><dd>Jersey core 2.x</dd><dl>|jersey2|

docs/generators/jaxrs-cxf-cdi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sidebar_label: jaxrs-cxf-cdi
4545
|implFolder|folder for generated implementation code| |src/main/java|
4646
|title|a title describing the application| |OpenAPI Server|
4747
|useBeanValidation|Use BeanValidation API annotations| |true|
48-
|serverPort|The port on which the server should be started| |null|
48+
|serverPort|The port on which the server should be started| |8080|
4949
|library|library template (sub-template)|<dl><dt>**&lt;default&gt;**</dt><dd>JAXRS</dd><dl>|&lt;default&gt;|
5050
|generatePom|Whether to generate pom.xml if the file does not already exist.| |true|
5151
|interfaceOnly|Whether to generate only API interface stubs without the server files.| |false|

docs/generators/jaxrs-cxf-extended.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sidebar_label: jaxrs-cxf-extended
4545
|implFolder|folder for generated implementation code| |src/main/java|
4646
|title|a title describing the application| |OpenAPI Server|
4747
|useBeanValidation|Use BeanValidation API annotations| |true|
48-
|serverPort|The port on which the server should be started| |null|
48+
|serverPort|The port on which the server should be started| |8080|
4949
|generateSpringApplication|Generate Spring application| |false|
5050
|useSpringAnnotationConfig|Use Spring Annotation Config| |false|
5151
|useSwaggerFeature|Use Swagger Feature| |false|

docs/generators/jaxrs-cxf.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sidebar_label: jaxrs-cxf
4545
|implFolder|folder for generated implementation code| |src/main/java|
4646
|title|a title describing the application| |OpenAPI Server|
4747
|useBeanValidation|Use BeanValidation API annotations| |true|
48-
|serverPort|The port on which the server should be started| |null|
48+
|serverPort|The port on which the server should be started| |8080|
4949
|generateSpringApplication|Generate Spring application| |false|
5050
|useSpringAnnotationConfig|Use Spring Annotation Config| |false|
5151
|useSwaggerFeature|Use Swagger Feature| |false|

docs/generators/jaxrs-jersey.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sidebar_label: jaxrs-jersey
4545
|implFolder|folder for generated implementation code| |src/main/java|
4646
|title|a title describing the application| |OpenAPI Server|
4747
|useBeanValidation|Use BeanValidation API annotations| |true|
48-
|serverPort|The port on which the server should be started| |null|
48+
|serverPort|The port on which the server should be started| |8080|
4949
|library|library template (sub-template)|<dl><dt>**jersey1**</dt><dd>Jersey core 1.x</dd><dt>**jersey2**</dt><dd>Jersey core 2.x</dd><dl>|jersey2|
5050
|supportJava6|Whether to support Java6 with the Jersey1/2 library.| |false|
5151
|useTags|use tags for creating interface and controller classnames| |false|

docs/generators/jaxrs-resteasy-eap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sidebar_label: jaxrs-resteasy-eap
4545
|implFolder|folder for generated implementation code| |src/main/java|
4646
|title|a title describing the application| |OpenAPI Server|
4747
|useBeanValidation|Use BeanValidation API annotations| |true|
48-
|serverPort|The port on which the server should be started| |null|
48+
|serverPort|The port on which the server should be started| |8080|
4949
|useBeanValidation|Use BeanValidation API annotations| |true|
5050
|generateJbossDeploymentDescriptor|Generate Jboss Deployment Descriptor| |true|
5151
|useSwaggerFeature|Use dynamic Swagger generator| |false|

docs/generators/jaxrs-resteasy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ sidebar_label: jaxrs-resteasy
4545
|implFolder|folder for generated implementation code| |src/main/java|
4646
|title|a title describing the application| |OpenAPI Server|
4747
|useBeanValidation|Use BeanValidation API annotations| |true|
48-
|serverPort|The port on which the server should be started| |null|
48+
|serverPort|The port on which the server should be started| |8080|
4949
|generateJbossDeploymentDescriptor|Generate Jboss Deployment Descriptor| |false|

docs/generators/jaxrs-spec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sidebar_label: jaxrs-spec
4545
|implFolder|folder for generated implementation code| |src/main/java|
4646
|title|a title describing the application| |OpenAPI Server|
4747
|useBeanValidation|Use BeanValidation API annotations| |true|
48-
|serverPort|The port on which the server should be started| |null|
48+
|serverPort|The port on which the server should be started| |8080|
4949
|library|library template (sub-template)|<dl><dt>**&lt;default&gt;**</dt><dd>JAXRS</dd><dl>|&lt;default&gt;|
5050
|generatePom|Whether to generate pom.xml if the file does not already exist.| |true|
5151
|interfaceOnly|Whether to generate only API interface stubs without the server files.| |false|

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

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,6 @@ public void processOpts() {
266266
additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId);
267267
}
268268

269-
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) {
270-
this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION));
271-
} else if (this.getVersionFromSpecification() != null) {
272-
this.setArtifactVersion(this.getVersionFromSpecification());
273-
} else {
274-
//not set, use to be passed to template
275-
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
276-
}
277-
278269
if (additionalProperties.containsKey(CodegenConstants.SNAPSHOT_VERSION)) {
279270
Boolean useSnapshotVersion = Boolean.valueOf((String) additionalProperties.get(CodegenConstants.SNAPSHOT_VERSION));
280271

@@ -1020,8 +1011,8 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
10201011
List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports");
10211012
Pattern pattern = Pattern.compile("java\\.util\\.(List|ArrayList|Map|HashMap)");
10221013
for (Iterator<Map<String, String>> itr = imports.iterator(); itr.hasNext(); ) {
1023-
String _import = itr.next().get("import");
1024-
if (pattern.matcher(_import).matches()) {
1014+
String itrImport = itr.next().get("import");
1015+
if (pattern.matcher(itrImport).matches()) {
10251016
itr.remove();
10261017
}
10271018
}
@@ -1030,30 +1021,45 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
10301021

10311022
@Override
10321023
public void preprocessOpenAPI(OpenAPI openAPI) {
1033-
if (openAPI == null || openAPI.getPaths() == null) {
1024+
if (openAPI == null) {
10341025
return;
10351026
}
1036-
for (String pathname : openAPI.getPaths().keySet()) {
1037-
PathItem path = openAPI.getPaths().get(pathname);
1038-
if (path.readOperations() == null) {
1039-
continue;
1040-
}
1041-
for (Operation operation : path.readOperations()) {
1042-
LOGGER.info("Processing operation " + operation.getOperationId());
1043-
if (hasBodyParameter(openAPI, operation) || hasFormParameter(openAPI, operation)) {
1044-
String defaultContentType = hasFormParameter(openAPI, operation) ? "application/x-www-form-urlencoded" : "application/json";
1045-
List<String> consumes = new ArrayList<String>(getConsumesInfo(openAPI, operation));
1046-
String contentType = consumes == null || consumes.isEmpty() ? defaultContentType : consumes.get(0);
1047-
operation.addExtension("x-contentType", contentType);
1027+
if (openAPI.getPaths() != null) {
1028+
for (String pathname : openAPI.getPaths().keySet()) {
1029+
PathItem path = openAPI.getPaths().get(pathname);
1030+
if (path.readOperations() == null) {
1031+
continue;
10481032
}
1049-
String accepts = getAccept(openAPI, operation);
1050-
operation.addExtension("x-accepts", accepts);
1033+
for (Operation operation : path.readOperations()) {
1034+
LOGGER.info("Processing operation " + operation.getOperationId());
1035+
if (hasBodyParameter(openAPI, operation) || hasFormParameter(openAPI, operation)) {
1036+
String defaultContentType = hasFormParameter(openAPI, operation) ? "application/x-www-form-urlencoded" : "application/json";
1037+
List<String> consumes = new ArrayList<>(getConsumesInfo(openAPI, operation));
1038+
String contentType = consumes == null || consumes.isEmpty() ? defaultContentType : consumes.get(0);
1039+
operation.addExtension("x-contentType", contentType);
1040+
}
1041+
String accepts = getAccept(openAPI, operation);
1042+
operation.addExtension("x-accepts", accepts);
10511043

1044+
}
10521045
}
10531046
}
1047+
1048+
// If no artifactVersion is provided in additional properties, version from API specification is used.
1049+
// If none of them is provided then fallbacks to default version
1050+
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) {
1051+
this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION));
1052+
} else if (openAPI.getInfo() != null && openAPI.getInfo().getVersion() != null) {
1053+
this.setArtifactVersion(openAPI.getInfo().getVersion());
1054+
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
1055+
} else {
1056+
//not set, use to be passed to template
1057+
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
1058+
}
1059+
10541060
}
10551061

1056-
protected static String getAccept(OpenAPI openAPI, Operation operation) {
1062+
private static String getAccept(OpenAPI openAPI, Operation operation) {
10571063
String accepts = null;
10581064
String defaultContentType = "application/json";
10591065
Set<String> producesInfo = getProducesInfo(openAPI, operation);
@@ -1423,19 +1429,6 @@ private String deriveInvokerPackageName(String input) {
14231429
return sb.toString();
14241430
}
14251431

1426-
/**
1427-
* Gets version from API specification.
1428-
*
1429-
* @return API version
1430-
*/
1431-
private String getVersionFromSpecification() {
1432-
if (this.openAPI != null && this.openAPI.getInfo() != null) {
1433-
return this.openAPI.getInfo().getVersion();
1434-
} else {
1435-
return null;
1436-
}
1437-
}
1438-
14391432
/**
14401433
* Builds a SNAPSHOT version from a given version.
14411434
*

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
4242
protected String implFolder = "src/main/java";
4343
protected String testResourcesFolder = "src/test/resources";
4444
protected String title = "OpenAPI Server";
45-
45+
protected String serverPort = "8080";
4646
protected boolean useBeanValidation = true;
4747

4848
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractJavaJAXRSServerCodegen.class);
@@ -71,7 +71,7 @@ public AbstractJavaJAXRSServerCodegen() {
7171
cliOptions.add(new CliOption(CodegenConstants.IMPL_FOLDER, CodegenConstants.IMPL_FOLDER_DESC).defaultValue(implFolder));
7272
cliOptions.add(new CliOption("title", "a title describing the application").defaultValue(title));
7373
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations",useBeanValidation));
74-
cliOptions.add(new CliOption(SERVER_PORT, "The port on which the server should be started"));
74+
cliOptions.add(new CliOption(SERVER_PORT, "The port on which the server should be started").defaultValue(serverPort));
7575
}
7676

7777

@@ -102,6 +102,7 @@ public void processOpts() {
102102

103103
@Override
104104
public void preprocessOpenAPI(OpenAPI openAPI) {
105+
super.preprocessOpenAPI(openAPI);
105106
/* TODO there should be no need for the following logic
106107
if ("/".equals(swagger.getBasePath())) {
107108
swagger.setBasePath("");
@@ -111,7 +112,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
111112
if (!this.additionalProperties.containsKey(SERVER_PORT)) {
112113
URL url = URLPathUtils.getServerURL(openAPI);
113114
// 8080 is the default value for a JEE Server:
114-
this.additionalProperties.put(SERVER_PORT, URLPathUtils.getPort(url, 8080));
115+
this.additionalProperties.put(SERVER_PORT, URLPathUtils.getPort(url, serverPort));
115116
}
116117

117118
if (openAPI.getPaths() != null) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,13 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
221221
this.additionalProperties.put("serverPort", URLPathUtils.getPort(url, 8080));
222222

223223
// retrieve api version from swagger file, 1.0.0-SNAPSHOT by default
224+
// set in super.preprocessOpenAPI
225+
/*
224226
if (openAPI.getInfo() != null && openAPI.getInfo().getVersion() != null) {
225227
artifactVersion = apiVersion = openAPI.getInfo().getVersion();
226228
} else {
227229
artifactVersion = apiVersion;
228-
}
230+
}*/
229231

230232
/*
231233
* manage operation & custom serviceId because operationId field is not

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.swagger.parser.OpenAPIParser;
44
import io.swagger.v3.oas.models.Components;
55
import io.swagger.v3.oas.models.OpenAPI;
6+
import io.swagger.v3.oas.models.Paths;
67
import io.swagger.v3.oas.models.info.Info;
78
import io.swagger.v3.oas.models.media.Schema;
89
import io.swagger.v3.oas.models.servers.Server;
@@ -27,6 +28,7 @@ public static OpenAPI parseContent(String jsonOrYaml) {
2728
public static OpenAPI createOpenAPI() {
2829
OpenAPI openAPI = new OpenAPI();
2930
openAPI.setComponents(new Components());
31+
openAPI.setPaths(new Paths());
3032

3133
final Info info = new Info();
3234
info.setDescription("API under test");

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,28 @@ public void toModelNameShouldUseProvidedMapping() throws Exception {
5555
public void toModelNameUsesPascalCase() throws Exception {
5656
Assert.assertEquals("JsonAnotherclass", fakeJavaCodegen.toModelName("json_anotherclass"));
5757
}
58-
58+
5959
@Test
6060
public void testPreprocessOpenAPI() throws Exception {
6161
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/petstore.yaml");
62-
final AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
62+
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
6363

6464
codegen.preprocessOpenAPI(openAPI);
6565

66+
Assert.assertEquals(codegen.getArtifactVersion(), openAPI.getInfo().getVersion());
6667
Assert.assertEquals(openAPI.getPaths().get("/pet").getPost().getExtensions().get("x-accepts"), "application/json");
6768
}
6869

70+
@Test
71+
public void testPreprocessOpenAPINumVersion() throws Exception {
72+
final OpenAPI openAPIOtherNumVersion = TestUtils.parseSpec("src/test/resources/2_0/duplicateOperationIds.yaml");
73+
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
74+
75+
codegen.preprocessOpenAPI(openAPIOtherNumVersion);
76+
77+
Assert.assertEquals(codegen.getArtifactVersion(), openAPIOtherNumVersion.getInfo().getVersion());
78+
}
79+
6980
@Test
7081
public void convertVarName() throws Exception {
7182
Assert.assertEquals(fakeJavaCodegen.toVarName("name"), "name");
@@ -208,9 +219,8 @@ public void openApiversionTest() {
208219
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
209220

210221
OpenAPI api = TestUtils.createOpenAPI();
211-
codegen.setOpenAPI(api);
212-
213222
codegen.processOpts();
223+
codegen.preprocessOpenAPI(api);
214224

215225
Assert.assertEquals(codegen.getArtifactVersion(), "1.0.7");
216226
}
@@ -222,15 +232,14 @@ public void additionalPropertyArtifactVersionTest() {
222232
codegen.additionalProperties().put("artifactVersion", "1.1.1");
223233

224234
OpenAPI api = TestUtils.createOpenAPI();
225-
codegen.setOpenAPI(api);
226-
227235
codegen.processOpts();
236+
codegen.preprocessOpenAPI(api);
228237

229238
Assert.assertEquals(codegen.getArtifactVersion(), "1.1.1");
230239
}
231240

232241
@Test(description = "tests if default version is used when neither OpenAPI version nor artifactVersion additional property has been provided")
233-
public void defautlVersionTest() {
242+
public void defaultVersionTest() {
234243
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
235244

236245
codegen.processOpts();

0 commit comments

Comments
 (0)