Skip to content

Commit

Permalink
#67 Add clientPackage to FileSpec object and check that the client pa…
Browse files Browse the repository at this point in the history
…ckages do not match
  • Loading branch information
jmejutovazquez committed Jun 17, 2022
1 parent 107e64d commit 5b8924e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ As commented above, they both could be used at the same time, setting a double
<plugin>
<groupId>net.coru</groupId>
<artifactId>scs-multiapi-maven-plugin</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
<executions>
<execution>
<id>asyncapi</id>
Expand Down Expand Up @@ -111,7 +111,7 @@ which the plugin is designed.
<plugin>
<groupId>net.coru</groupId>
<artifactId>scs-multiapi-maven-plugin</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
Expand Down Expand Up @@ -464,7 +464,7 @@ file. Here is an example of a basic configuration:
<plugin>
<groupId>net.coru</groupId>
<artifactId>scs-multiapi-maven-plugin</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
<executions>
<execution>
<goals>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>net.coru</groupId>
<artifactId>scs-multiapi-maven-plugin</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
<packaging>maven-plugin</packaging>

<name>AsyncApi - OpenApi Code Generator Maven Plugin</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public class OpenapiMultiFileMojo extends AbstractMojo {
@Parameter(property = "fileSpecs")
public List<FileSpec> fileSpecs;

@Parameter(name = "clientPackage", property = "clientPackage")
private String clientPackage;

@Parameter(name = "overwriteModel", property = "overwriteModel", defaultValue = "true")
private Boolean overwriteModel;

Expand All @@ -84,7 +81,6 @@ public void execute() throws MojoExecutionException {
templateFactory = new TemplateFactory();
if (null != fileSpecs && !fileSpecs.isEmpty()) {
processFileSpec(fileSpecs);
createClients();
} else {
throw new MojoExecutionException("Code generation failed. Not exists FileSpec configuration to generate package and class");
}
Expand All @@ -99,7 +95,7 @@ private void processFileSpec(List<FileSpec> fileSpecsList) throws MojoExecutionE
String filePathToSave = processPath(fileSpec.getApiPackage(), false);
project.addCompileSourceRoot(filePathToSave);
processFile(fileSpec, filePathToSave);

createClients(fileSpec);
} catch (Exception e) {
getLog().error(e);
throw new MojoExecutionException("Code generation failed. See above for the full exception.");
Expand All @@ -112,12 +108,13 @@ private void processFile(FileSpec fileSpec, String filePathToSave) throws MojoEx
try {

OpenAPI openAPI = OpenApiUtil.getPojoFromSwagger(fileSpec);
String clientPackage = fileSpec.getClientPackage();

if (fileSpec.getCallMode()) {
templateFactory.setWebClientPackageName(StringUtils.isNotBlank(clientPackage) ? clientPackage : DEFAULT_OPENAPI_CLIENT_PACKAGE);
templateFactory.setAuthPackageName((StringUtils.isNotBlank(clientPackage) ? clientPackage : DEFAULT_OPENAPI_CLIENT_PACKAGE) + ".auth");
isWebClient = fileSpec.getIsReactive();
isRestClient = fileSpec.getIsReactive() ? false : true;
isRestClient = !fileSpec.getIsReactive();
}

createApiTemplate(fileSpec, filePathToSave, openAPI);
Expand All @@ -131,7 +128,8 @@ private void processFile(FileSpec fileSpec, String filePathToSave) throws MojoEx

}

private void createClients() {
private void createClients(final FileSpec fileSpec) {
String clientPackage = fileSpec.getClientPackage();

if (isWebClient || isRestClient) {
String clientPath = processPath(StringUtils.isNotBlank(clientPackage) ? clientPackage : DEFAULT_OPENAPI_CLIENT_PACKAGE, false);
Expand All @@ -145,15 +143,16 @@ private void createClients() {
templateFactory.fillTemplateRestClient(clientPath);
}

createAuthTemplates();
createAuthTemplates(fileSpec);
} catch (Exception e) {
e.printStackTrace();
}

}
}

private void createAuthTemplates() throws TemplateException, IOException {
private void createAuthTemplates(final FileSpec fileSpec) throws TemplateException, IOException {
String clientPackage = fileSpec.getClientPackage();

var authFileRoot = (StringUtils.isNotBlank(clientPackage) ? clientPackage : DEFAULT_OPENAPI_CLIENT_PACKAGE) + ".auth";
String authFileToSave = processPath(authFileRoot, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class FileSpec {
@Parameter(name = "modelNameSuffix", property = "modelNameSuffix")
private String modelNameSuffix;

@Parameter(name = "clientPackage", property = "clientPackage")
private String clientPackage;

@Parameter(name = "callMode", property = "callMode")
private Boolean callMode = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import net.coru.api.generator.plugin.exception.SCSMultiApiMavenPluginException;
import net.coru.api.generator.plugin.openapi.parameter.FileSpec;
import net.coru.api.generator.plugin.openapi.model.AuthObject;
import net.coru.api.generator.plugin.openapi.model.PathObject;
Expand Down Expand Up @@ -120,9 +123,13 @@ public void fillTemplate(
private void writeTemplateToFile(String templateName, Map<String, Object> root, String path) throws IOException, TemplateException {
Template template = cfg.getTemplate(templateName);

FileWriter writer = new FileWriter(path);
template.process(root, writer);
writer.close();
if (!Files.exists(Path.of(path))) {
FileWriter writer = new FileWriter(path);
template.process(root, writer);
writer.close();
} else {
throw new SCSMultiApiMavenPluginException("The fileSpecs packets cannot be the same.");
}
}

public void setPackageName(String packageName) {
Expand Down

0 comments on commit 5b8924e

Please sign in to comment.