Skip to content
Snippets Groups Projects
Commit 5203f556 authored by Martin Vogel's avatar Martin Vogel
Browse files

#414 Add post processor to model generation step

parent 1bd4a5b0
No related branches found
No related tags found
2 merge requests!2#414 Remaining changes from MR,!1planning#414 Methoden Signaturen (Zwischenstand)
......@@ -2,8 +2,8 @@
This module generates model classes from the open-api specification of the sumission api.
Usage: ``mvn compile`` generates all models into the package ``de.fitko.fitconnect.api.domain.model`` under
**src/main/java**.
Usage: ``mvn compile exec:java`` generates all models into the package ``de.fitko.fitconnect.api.domain.model.submission`` under
**target/generated-sources/openapi/gen**.
### Config
......@@ -35,10 +35,14 @@ The relevant config parameters are listed below.
````
### Draft/Todo
### Draft/Todo PostProcessing
The ``de.fitko.fitconnect.api.postprocessing.ModelClassPostProcessor`` is running in a maven execute step and
replaces all comments and unwanted annotations.
Currently there are some @Nullable and @NonNull annotations from ```javax.annotations``` that need to be replaced and
german comments from the api spec.
In a further step the cleaned and post-processed model classes can be updated in the original ``api/domain/model``
package of the ``api`` module
\ No newline at end of file
package of the ``api`` module via the maven resource plugin.
......@@ -36,9 +36,22 @@
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
......@@ -60,18 +73,53 @@
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<generateSupportingFiles>false</generateSupportingFiles>
<generateAliasAsModel>true</generateAliasAsModel>
<library>resttemplate</library>
<modelPackage>de.fitko.fitconnect.api.domain.model.submission</modelPackage>
<configOptions>
<sourceFolder>../../../src/main/java</sourceFolder>
<additionalModelTypeAnnotations>@lombok.Builder
</additionalModelTypeAnnotations>
<sourceFolder>gen</sourceFolder>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>de.fitko.fitconnect.api.postprocessing.ModelClassPostProcessor</mainClass>
<workingDirectory>postprocessing</workingDirectory>
<arguments>
<argument>target/generated-sources/openapi/gen/de/fitko/fitconnect/api/domain/model/submission
</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resource-one</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/../api/src/main/java/de/fitko/fitconnect/api/domain/generated
</outputDirectory>
<resources>
<resource>
<directory>
target/generated-sources/openapi/gen/de/fitko/fitconnect/api/domain/model/submission
</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
......
package de.fitko.fitconnect.api.postprocessing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ModelClassPostProcessor {
private static final Logger logger = LoggerFactory.getLogger(ModelClassPostProcessor.class);
public static void main(final String[] args) {
final ModelClassPostProcessor postProcessor = new ModelClassPostProcessor();
final var dir = args[0]; // -modelPath
postProcessor.readModelsToStrings(dir)
.stream()
.map(ModelClassPostProcessor::replaceContent)
//.peek(pair -> logger.info(pair.content))
.forEach(ModelClassPostProcessor::writeFiles);
}
private static ModelPair replaceContent(final ModelPair pair) {
String content = pair.content;
// Remove all Comments
content = content.replaceAll("\\/\\*([\\S\\s]+?)\\*\\/", "");
content = content.replaceAll("(?s)/\\*.*?\\*/", "");
// Remove unwanted annos
content = content.replaceAll("@ApiModel([\\w]+)((.*))", "");
content = content.replaceAll("@ApiModelProperty([\\w]+)((.*))", "");
content = content.replaceAll("Generated([\\w]+)((.*))", "");
// Replace javax
//content = content.replaceAll("Nonnull([\\w]+)((.*))", "");
//content = content.replaceAll("(@\([\\w]+)\\((.*?)\\)(?:\s|$))", "");
return new ModelPair(pair.originalPath, content);
}
public Set<ModelPair> readModelsToStrings(final String dir) {
return Stream.of(new File(dir).listFiles())
.filter(file -> !file.isDirectory())
.peek(file -> logger.info("Postprocessing model {}", file.getName()))
.map(file -> new ModelPair(file.toPath(), getFileAsString(file)))
.collect(Collectors.toSet());
}
private String getFileAsString(final File file) {
try {
return Files.readString(file.toPath(), StandardCharsets.UTF_8);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
private static void writeFiles(final ModelPair modelPair) {
try {
final File f = new File(modelPair.originalPath.toFile().getAbsolutePath());
final FileWriter fw = new FileWriter( f, false);
fw.write(modelPair.content);
fw.flush();
fw.close();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
private static class ModelPair {
final Path originalPath;
final String content;
ModelPair(final Path originalPath, final String content) {
this.originalPath = originalPath;
this.content = content;
}
@Override
public String toString() {
return content;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment