Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/
/target/
/.classpath
/.project
/.settings
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>

<io.swagger.version>1.5.16</io.swagger.version>
<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.29</io.swagger.parser.version>
<io.swagger.version>1.5.16</io.swagger.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>
42 changes: 23 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",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not
private static final String[] MINIMUM = {...} and
private static final String[] STANDARD = {...}?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Youre right, why not - it is a part fo the latest commit, thank you for pointing to that.

"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,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
);
}

private void ApplySpecificHeadersAndResponses(Swagger api, Set<String> codes) {
private String findFileFormat(String fileAndPathName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be realized with streams? Not sure whether its more readable or not but you avoid the branching. The extensions could maybe also be added to the OutputFormat enumeration since this seems to be somewhat related to the extension - actually looking at the method name findFileFormat I might have expected it to return a FileFormat - it is actually returning a file name - isn't it? :-)

    private String findFileFormat(final String fileAndPathName) {
        return Arrays.asList(".json", ".yaml", ".yml").stream()
            .filter(ext -> Files.exists(Paths.get(fileAndPathName + ext)))
            .findAny()
            .orElse(fileAndPathName);
    }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is part of the latest commit - thanks for suggesting that.

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";
}
return 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