Skip to content

[JAVA] fix artifactVersion is not taken from specification #2798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/generators/java-msf4j.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ sidebar_label: java-msf4j
|implFolder|folder for generated implementation code| |src/main/java|
|title|a title describing the application| |OpenAPI Server|
|useBeanValidation|Use BeanValidation API annotations| |true|
|serverPort|The port on which the server should be started| |null|
|serverPort|The port on which the server should be started| |8080|
|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|
2 changes: 1 addition & 1 deletion docs/generators/jaxrs-cxf-cdi.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sidebar_label: jaxrs-cxf-cdi
|implFolder|folder for generated implementation code| |src/main/java|
|title|a title describing the application| |OpenAPI Server|
|useBeanValidation|Use BeanValidation API annotations| |true|
|serverPort|The port on which the server should be started| |null|
|serverPort|The port on which the server should be started| |8080|
|library|library template (sub-template)|<dl><dt>**&lt;default&gt;**</dt><dd>JAXRS</dd><dl>|&lt;default&gt;|
|generatePom|Whether to generate pom.xml if the file does not already exist.| |true|
|interfaceOnly|Whether to generate only API interface stubs without the server files.| |false|
Expand Down
2 changes: 1 addition & 1 deletion docs/generators/jaxrs-cxf-extended.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sidebar_label: jaxrs-cxf-extended
|implFolder|folder for generated implementation code| |src/main/java|
|title|a title describing the application| |OpenAPI Server|
|useBeanValidation|Use BeanValidation API annotations| |true|
|serverPort|The port on which the server should be started| |null|
|serverPort|The port on which the server should be started| |8080|
|generateSpringApplication|Generate Spring application| |false|
|useSpringAnnotationConfig|Use Spring Annotation Config| |false|
|useSwaggerFeature|Use Swagger Feature| |false|
Expand Down
2 changes: 1 addition & 1 deletion docs/generators/jaxrs-cxf.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sidebar_label: jaxrs-cxf
|implFolder|folder for generated implementation code| |src/main/java|
|title|a title describing the application| |OpenAPI Server|
|useBeanValidation|Use BeanValidation API annotations| |true|
|serverPort|The port on which the server should be started| |null|
|serverPort|The port on which the server should be started| |8080|
|generateSpringApplication|Generate Spring application| |false|
|useSpringAnnotationConfig|Use Spring Annotation Config| |false|
|useSwaggerFeature|Use Swagger Feature| |false|
Expand Down
2 changes: 1 addition & 1 deletion docs/generators/jaxrs-jersey.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sidebar_label: jaxrs-jersey
|implFolder|folder for generated implementation code| |src/main/java|
|title|a title describing the application| |OpenAPI Server|
|useBeanValidation|Use BeanValidation API annotations| |true|
|serverPort|The port on which the server should be started| |null|
|serverPort|The port on which the server should be started| |8080|
|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|
|supportJava6|Whether to support Java6 with the Jersey1/2 library.| |false|
|useTags|use tags for creating interface and controller classnames| |false|
2 changes: 1 addition & 1 deletion docs/generators/jaxrs-resteasy-eap.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sidebar_label: jaxrs-resteasy-eap
|implFolder|folder for generated implementation code| |src/main/java|
|title|a title describing the application| |OpenAPI Server|
|useBeanValidation|Use BeanValidation API annotations| |true|
|serverPort|The port on which the server should be started| |null|
|serverPort|The port on which the server should be started| |8080|
|useBeanValidation|Use BeanValidation API annotations| |true|
|generateJbossDeploymentDescriptor|Generate Jboss Deployment Descriptor| |true|
|useSwaggerFeature|Use dynamic Swagger generator| |false|
2 changes: 1 addition & 1 deletion docs/generators/jaxrs-resteasy.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ sidebar_label: jaxrs-resteasy
|implFolder|folder for generated implementation code| |src/main/java|
|title|a title describing the application| |OpenAPI Server|
|useBeanValidation|Use BeanValidation API annotations| |true|
|serverPort|The port on which the server should be started| |null|
|serverPort|The port on which the server should be started| |8080|
|generateJbossDeploymentDescriptor|Generate Jboss Deployment Descriptor| |false|
2 changes: 1 addition & 1 deletion docs/generators/jaxrs-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sidebar_label: jaxrs-spec
|implFolder|folder for generated implementation code| |src/main/java|
|title|a title describing the application| |OpenAPI Server|
|useBeanValidation|Use BeanValidation API annotations| |true|
|serverPort|The port on which the server should be started| |null|
|serverPort|The port on which the server should be started| |8080|
|library|library template (sub-template)|<dl><dt>**&lt;default&gt;**</dt><dd>JAXRS</dd><dl>|&lt;default&gt;|
|generatePom|Whether to generate pom.xml if the file does not already exist.| |true|
|interfaceOnly|Whether to generate only API interface stubs without the server files.| |false|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,6 @@ public void processOpts() {
additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId);
}

if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) {
this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION));
} else if (this.getVersionFromSpecification() != null) {
this.setArtifactVersion(this.getVersionFromSpecification());
} else {
//not set, use to be passed to template
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
}

if (additionalProperties.containsKey(CodegenConstants.SNAPSHOT_VERSION)) {
Boolean useSnapshotVersion = Boolean.valueOf((String) additionalProperties.get(CodegenConstants.SNAPSHOT_VERSION));

Expand Down Expand Up @@ -1028,8 +1019,8 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports");
Pattern pattern = Pattern.compile("java\\.util\\.(List|ArrayList|Map|HashMap)");
for (Iterator<Map<String, String>> itr = imports.iterator(); itr.hasNext(); ) {
String _import = itr.next().get("import");
if (pattern.matcher(_import).matches()) {
String itrImport = itr.next().get("import");
if (pattern.matcher(itrImport).matches()) {
itr.remove();
}
}
Expand All @@ -1038,30 +1029,45 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o

@Override
public void preprocessOpenAPI(OpenAPI openAPI) {
if (openAPI == null || openAPI.getPaths() == null) {
if (openAPI == null) {
return;
}
for (String pathname : openAPI.getPaths().keySet()) {
PathItem path = openAPI.getPaths().get(pathname);
if (path.readOperations() == null) {
continue;
}
for (Operation operation : path.readOperations()) {
LOGGER.info("Processing operation " + operation.getOperationId());
if (hasBodyParameter(openAPI, operation) || hasFormParameter(openAPI, operation)) {
String defaultContentType = hasFormParameter(openAPI, operation) ? "application/x-www-form-urlencoded" : "application/json";
List<String> consumes = new ArrayList<String>(getConsumesInfo(openAPI, operation));
String contentType = consumes == null || consumes.isEmpty() ? defaultContentType : consumes.get(0);
operation.addExtension("x-contentType", contentType);
if (openAPI.getPaths() != null) {
for (String pathname : openAPI.getPaths().keySet()) {
PathItem path = openAPI.getPaths().get(pathname);
if (path.readOperations() == null) {
continue;
}
String accepts = getAccept(openAPI, operation);
operation.addExtension("x-accepts", accepts);
for (Operation operation : path.readOperations()) {
LOGGER.info("Processing operation " + operation.getOperationId());
if (hasBodyParameter(openAPI, operation) || hasFormParameter(openAPI, operation)) {
String defaultContentType = hasFormParameter(openAPI, operation) ? "application/x-www-form-urlencoded" : "application/json";
List<String> consumes = new ArrayList<>(getConsumesInfo(openAPI, operation));
String contentType = consumes == null || consumes.isEmpty() ? defaultContentType : consumes.get(0);
operation.addExtension("x-contentType", contentType);
}
String accepts = getAccept(openAPI, operation);
operation.addExtension("x-accepts", accepts);

}
}
}

// If no artifactVersion is provided in additional properties, version from API specification is used.
// If none of them is provided then fallbacks to default version
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) {
this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION));
} else if (openAPI.getInfo() != null && openAPI.getInfo().getVersion() != null) {
this.setArtifactVersion(openAPI.getInfo().getVersion());
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
} else {
//not set, use to be passed to template
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
}

}

protected static String getAccept(OpenAPI openAPI, Operation operation) {
private static String getAccept(OpenAPI openAPI, Operation operation) {
String accepts = null;
String defaultContentType = "application/json";
Set<String> producesInfo = getProducesInfo(openAPI, operation);
Expand Down Expand Up @@ -1431,19 +1437,6 @@ private String deriveInvokerPackageName(String input) {
return sb.toString();
}

/**
* Gets version from API specification.
*
* @return API version
*/
private String getVersionFromSpecification() {
if (this.openAPI != null && this.openAPI.getInfo() != null) {
return this.openAPI.getInfo().getVersion();
} else {
return null;
}
}

/**
* Builds a SNAPSHOT version from a given version.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
protected String implFolder = "src/main/java";
protected String testResourcesFolder = "src/test/resources";
protected String title = "OpenAPI Server";

protected String serverPort = "8080";
protected boolean useBeanValidation = true;

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


Expand Down Expand Up @@ -102,6 +102,7 @@ public void processOpts() {

@Override
public void preprocessOpenAPI(OpenAPI openAPI) {
super.preprocessOpenAPI(openAPI);
/* TODO there should be no need for the following logic
if ("/".equals(swagger.getBasePath())) {
swagger.setBasePath("");
Expand All @@ -111,7 +112,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
if (!this.additionalProperties.containsKey(SERVER_PORT)) {
URL url = URLPathUtils.getServerURL(openAPI);
// 8080 is the default value for a JEE Server:
this.additionalProperties.put(SERVER_PORT, URLPathUtils.getPort(url, 8080));
this.additionalProperties.put(SERVER_PORT, URLPathUtils.getPort(url, serverPort));
}

if (openAPI.getPaths() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,13 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
this.additionalProperties.put("serverPort", URLPathUtils.getPort(url, 8080));

// retrieve api version from swagger file, 1.0.0-SNAPSHOT by default
// set in super.preprocessOpenAPI
/*
if (openAPI.getInfo() != null && openAPI.getInfo().getVersion() != null) {
artifactVersion = apiVersion = openAPI.getInfo().getVersion();
} else {
artifactVersion = apiVersion;
}
}*/

/*
* manage operation & custom serviceId because operationId field is not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;
Expand All @@ -27,6 +28,7 @@ public static OpenAPI parseContent(String jsonOrYaml) {
public static OpenAPI createOpenAPI() {
OpenAPI openAPI = new OpenAPI();
openAPI.setComponents(new Components());
openAPI.setPaths(new Paths());

final Info info = new Info();
info.setDescription("API under test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,28 @@ public void toModelNameShouldUseProvidedMapping() throws Exception {
public void toModelNameUsesPascalCase() throws Exception {
Assert.assertEquals("JsonAnotherclass", fakeJavaCodegen.toModelName("json_anotherclass"));
}

@Test
public void testPreprocessOpenAPI() throws Exception {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/petstore.yaml");
final AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();

codegen.preprocessOpenAPI(openAPI);

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

@Test
public void testPreprocessOpenAPINumVersion() throws Exception {
final OpenAPI openAPIOtherNumVersion = TestUtils.parseSpec("src/test/resources/2_0/duplicateOperationIds.yaml");
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();

codegen.preprocessOpenAPI(openAPIOtherNumVersion);

Assert.assertEquals(codegen.getArtifactVersion(), openAPIOtherNumVersion.getInfo().getVersion());
}

@Test
public void convertVarName() throws Exception {
Assert.assertEquals(fakeJavaCodegen.toVarName("name"), "name");
Expand Down Expand Up @@ -208,9 +219,8 @@ public void openApiversionTest() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();

OpenAPI api = TestUtils.createOpenAPI();
codegen.setOpenAPI(api);

codegen.processOpts();
codegen.preprocessOpenAPI(api);

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

OpenAPI api = TestUtils.createOpenAPI();
codegen.setOpenAPI(api);

codegen.processOpts();
codegen.preprocessOpenAPI(api);

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

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

codegen.processOpts();
Expand Down
Loading