Skip to content
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

Code quality tooling added #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/
/.classpath
/.project
/.settings

# IntelliJ IDEA
.idea
Expand Down
123 changes: 116 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,24 @@
</distributionManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<checkstyle.plugin.version>2.17</checkstyle.plugin.version>
<checkstyle.version>7.8.2</checkstyle.version>
<findbugs.version>3.0.4</findbugs.version>
<io.openapitools.jackson.dataformat.version>1.0.4</io.openapitools.jackson.dataformat.version>
<io.swagger.parser.version>1.0.34</io.swagger.parser.version>
<io.swagger.version>1.5.16</io.swagger.version>
<io.swagger.parser.version>1.0.29</io.swagger.parser.version>
<jacoco.version>0.7.9</jacoco.version>
<javax.xml.bin.jaxb-api.version>2.3.0</javax.xml.bin.jaxb-api.version>
<junit.version>4.12</junit.version>
<maven.jxr.version>2.5</maven.jxr.version>
<org.mockito.version>2.11.0</org.mockito.version>
<org.slf4j.version>1.7.25</org.slf4j.version>
<org.apache.maven.maven-plugin-annotations.version>3.5</org.apache.maven.maven-plugin-annotations.version>
<org.apache.maven.version>3.5.0</org.apache.maven.version>
<io.openapitools.jackson.dataformat.version>1.0.4</io.openapitools.jackson.dataformat.version>
<org.slf4j.version>1.7.25</org.slf4j.version>
<org.apache.maven.plugin-testing.version>3.3.0</org.apache.maven.plugin-testing.version>
<javax.xml.bin.jaxb-api.version>2.3.0</javax.xml.bin.jaxb-api.version>
<pmd.plugin.version>3.8</pmd.plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
Expand Down Expand Up @@ -147,6 +152,33 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle.plugin.version}</version>
<configuration>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
<includes>**/io/openapitools/**/*.java</includes>
<configLocation>https://raw.githubusercontent.com/openapi-tools/code-style/master/checkstyle/rules.xml</configLocation>
<includeTestResources>false</includeTestResources>
</configuration>
<executions>
<execution>
<id>checkstyle</id>
<phase>package</phase>
<goals>
<goal>checkstyle</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
Expand Down Expand Up @@ -222,6 +254,74 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<excludes>
</excludes>
</configuration>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.plugin.version}</version>
<configuration>
<rulesets>
<ruleset>https://raw.githubusercontent.com/openapi-tools/code-style/master/pmd/rules.xml</ruleset>
</rulesets>
<targetJdk>${jdk.version}</targetJdk>
<includes>
<include>**/io/openapitools/**/*.java</include>
</includes>
<excludes>
</excludes>
<excludeRoots>
</excludeRoots>
</configuration>
<executions>
<execution>
<id>pmd</id>
<phase>package</phase>
<goals>
<goal>pmd</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${findbugs.version}</version>
<configuration>
<onlyAnalyze>io.openapitools.-</onlyAnalyze>
</configuration>
<executions>
<execution>
<id>findbugs</id>
<phase>package</phase>
<goals>
<goal>findbugs</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
Expand All @@ -232,5 +332,14 @@
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>${maven.jxr.version}</version>
</plugin>
</plugins>
</reporting>

</project>
38 changes: 19 additions & 19 deletions src/main/java/io/openapitools/api/pp/APIPostProcessorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import io.swagger.models.Path;
import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -87,24 +86,18 @@ public class APIPostProcessorMojo extends AbstractMojo {
@Component
private MavenProjectHelper projectHelper;

String[] minimum = {"200", "202", "204", "301", "400", "404", "415", "500"};
String[] standard = {"200", "201", "202", "203", "204", "301", "304", "307",
private final String[] MINIMUM = {"200", "202", "204", "301", "400", "404", "415", "500"};
private final String[] STANDARD = {"200", "201", "202", "203", "204", "301", "304", "307",
"400", "401", "403", "404", "406", "409", "410", "412", "415", "422", "429",
"500", "501", "503", "505"};
"500", "501", "503", "505"};


@Override
public void execute() throws MojoExecutionException, MojoFailureException {

SwaggerParser swaggerParser = new SwaggerParser();
String fileAndPathName = inputDirectory + "/" + inputFilename;
if (Files.exists(Paths.get(fileAndPathName + ".json"))) {
fileAndPathName = fileAndPathName + ".json";
} else if (Files.exists(Paths.get(fileAndPathName + ".yaml"))) {
fileAndPathName = fileAndPathName + ".yaml";
} else if (Files.exists(Paths.get(fileAndPathName + ".yml"))) {
fileAndPathName = fileAndPathName + ".yml";
}
fileAndPathName = findFileFormat(fileAndPathName);
Swagger api = swaggerParser.read(fileAndPathName);

if (api == null) {
Expand All @@ -113,14 +106,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

if (null == packages && null == codes) {
ApplySpecificHeadersAndResponses(api, new HashSet(Arrays.asList(standard)));
applySpecificHeadersAndResponses(api, new HashSet<>(Arrays.asList(STANDARD)));
} else {
if (packages.contains("standard")) {
ApplySpecificHeadersAndResponses(api, new HashSet(Arrays.asList(standard)));
} else if (packages.contains("minimal")) {
ApplySpecificHeadersAndResponses(api, new HashSet(Arrays.asList(minimum)));
} else if (codes.size() > 0) {
ApplySpecificHeadersAndResponses(api, codes);
if (null != packages && packages.contains("standard")) {
applySpecificHeadersAndResponses(api, new HashSet<>(Arrays.asList(STANDARD)));
} else if (null != packages && packages.contains("minimal")) {
applySpecificHeadersAndResponses(api, new HashSet<>(Arrays.asList(MINIMUM)));
} else if (null != codes && codes.size() > 0) {
applySpecificHeadersAndResponses(api, codes);
}
}

Expand All @@ -143,7 +136,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
);
}

private void ApplySpecificHeadersAndResponses(Swagger api, Set<String> codes) {
private String findFileFormat(final String fileAndPathName) {
return Arrays.asList(fileAndPathName + ".json", fileAndPathName + ".yaml", fileAndPathName + ".yml").stream()
.filter(ffn -> Files.exists(Paths.get(ffn)))
.findAny()
.orElse(fileAndPathName);
}

private void applySpecificHeadersAndResponses(Swagger api, Set<String> codes) {
Map<String, Path> paths = api.getPaths();
paths.forEach((k, p) -> {
List<Operation> operations = p.getOperations();
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/io/openapitools/api/pp/Responses.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ private Responses() {
// intentionally empty
}


public static void addResponseCodes(Operation operation, Set codes ) {
public static void addResponseCodes(Operation operation, Set codes) {
if (codes.contains("200")) addOKResponse200(operation);
if (codes.contains("400")) addBadRequestResponse400(operation);
if (codes.contains("401")) addNotAuthorizedResponse401(operation);
Expand All @@ -44,7 +43,7 @@ public static void addVerbSpecificHeaders(Path p, Set<String> codes) {
if (null != p.getDelete()) addDeleteResponses(p.getDelete(), codes);
}

private static void addGetResponses(Operation getOperation, Set<String> codes ) {
private static void addGetResponses(Operation getOperation, Set<String> codes) {
if (codes.contains("202")) addAcceptedResponse202(getOperation);
if (codes.contains("203")) addNonAuthoritativeInformationResponse203(getOperation);
if (codes.contains("301")) addPermanentlyMovedResponse301(getOperation);
Expand Down Expand Up @@ -91,7 +90,7 @@ private static void addDeleteResponses(Operation deleteOperation, Set<String> co

private static void addPatchResponses(Operation patchOperation, Set<String> codes) {
Headers.addPatchHeaders(patchOperation);
if (codes.contains("422")) addUnprocessableRequestResponse422(patchOperation);
if (codes.contains("422")) addUnprocessableRequestResponse422(patchOperation);
}

private static void addOKResponse200(Operation operation) {
Expand Down