Skip to content

feat: add a maven plugin #278

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
merged 5 commits into from
Nov 14, 2021
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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,35 @@ usage: openapi-diff <old> <new>
--warn Print warning information
```

## Maven Plugin

Add openapi-diff to your POM to show diffs when you test your Maven project. You may opt to throw an error if you have broken backwards compatibility or if your API has changed.

```xml
<plugin>
<groupId>org.openapitools.openapidiff</groupId>
<artifactId>openapi-diff-maven</artifactId>
<version>${openapi-diff-version}</version>
<executions>
<execution>
<goals>
<goal>diff</goal>
</goals>
<configuration>
<!-- Reference specification (perhaps your prod schema) -->
<oldSpec>https://petstore3.swagger.io/api/v3/openapi.json</oldSpec>
<!-- Specification generated by your project in the compile phase -->
<newSpec>${project.basedir}/target/openapi.yaml</newSpec>
<!-- Fail only if API changes broke backward compatibility (default: false) -->
<failOnIncompatible>true</failOnIncompatible>
<!-- Fail if API changed (default: false) -->
<failOnChanged>true</failOnIncompatible>
</configuration>
</execution>
</executions>
</plugin>
```

## Direct Invocation

```java
Expand Down
64 changes: 64 additions & 0 deletions maven/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openapitools.openapidiff</groupId>
<artifactId>openapi-diff-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>openapi-diff-maven</artifactId>
<packaging>jar</packaging>

<name>openapi-diff-maven</name>
<description>Maven plugin for openapi-diff</description>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openapitools.openapidiff</groupId>
<artifactId>openapi-diff-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.openapitools.openapidiff.maven;

import org.apache.maven.plugin.MojoFailureException;

public class ApiChangedException extends MojoFailureException {
public ApiChangedException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.openapitools.openapidiff.maven;

import org.apache.maven.plugin.MojoFailureException;

class BackwardIncompatibilityException extends MojoFailureException {
public BackwardIncompatibilityException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.openapitools.openapidiff.maven;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.openapitools.openapidiff.core.OpenApiCompare;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
import org.openapitools.openapidiff.core.output.ConsoleRender;

/** A Maven Mojo that diffs two OpenAPI specifications and reports on differences. */
@Mojo(name = "diff", defaultPhase = LifecyclePhase.TEST)
public class OpenApiDiffMojo extends AbstractMojo {
@Parameter(property = "oldSpec")
String oldSpec;

@Parameter(property = "newSpec")
String newSpec;

@Parameter(property = "failOnIncompatible", defaultValue = "false")
Boolean failOnIncompatible = false;

@Parameter(property = "failOnChanged", defaultValue = "false")
Boolean failOnChanged = false;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
final ChangedOpenApi diff = OpenApiCompare.fromLocations(oldSpec, newSpec);
getLog().info(new ConsoleRender().render(diff));

if (failOnIncompatible && diff.isIncompatible()) {
throw new BackwardIncompatibilityException("The API changes broke backward compatibility");
}

if (failOnChanged && diff.isDifferent()) {
throw new ApiChangedException("The API changed");
}
} catch (RuntimeException e) {
throw new MojoExecutionException("Unexpected error", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.openapitools.openapidiff.maven;

import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import org.apache.maven.plugin.MojoExecutionException;
import org.junit.jupiter.api.Test;

class OpenApiDiffMojoTest {
@Test
void Should_NotThrow_When_SpecHasNoChanges() {
final String oldSpec = new File("src/test/resources/oldspec.yaml").getAbsolutePath();

final OpenApiDiffMojo mojo = new OpenApiDiffMojo();
mojo.oldSpec = oldSpec;
mojo.newSpec = oldSpec;
mojo.failOnIncompatible = true;

assertDoesNotThrow(mojo::execute);
}

@Test
void Should_NotThrow_When_SpecIsCompatible() {
final OpenApiDiffMojo mojo = new OpenApiDiffMojo();
mojo.oldSpec = new File("src/test/resources/oldspec.yaml").getAbsolutePath();
mojo.newSpec = new File("src/test/resources/newspec.yaml").getAbsolutePath();
mojo.failOnIncompatible = true;

assertDoesNotThrow(mojo::execute);
}

@Test
void Should_Throw_When_SpecIsDifferent() {
final OpenApiDiffMojo mojo = new OpenApiDiffMojo();
mojo.oldSpec = new File("src/test/resources/oldspec.yaml").getAbsolutePath();
mojo.newSpec = new File("src/test/resources/newspec.yaml").getAbsolutePath();
mojo.failOnChanged = true;

assertThrows(ApiChangedException.class, mojo::execute);
}

@Test
void Should_MojoExecutionException_When_MissingOldSpec() {
final OpenApiDiffMojo mojo = new OpenApiDiffMojo();
mojo.oldSpec = new File("DOES_NOT_EXIST").getAbsolutePath();
mojo.newSpec = new File("src/test/resources/newspec.yaml").getAbsolutePath();

assertThrows(MojoExecutionException.class, mojo::execute);
}

@Test
void Should_MojoExecutionException_When_MissingNewSpec() {
final OpenApiDiffMojo mojo = new OpenApiDiffMojo();
mojo.oldSpec = new File("src/test/resources/oldspec.yaml").getAbsolutePath();
mojo.newSpec = new File("DOES_NOT_EXIST").getAbsolutePath();

assertThrows(MojoExecutionException.class, mojo::execute);
}

@Test
void Should_NotThrow_When_DefaultsAndSpecIsIncompatible() {
final OpenApiDiffMojo mojo = new OpenApiDiffMojo();
mojo.oldSpec = new File("src/test/resources/newspec.yaml").getAbsolutePath();
mojo.newSpec = new File("src/test/resources/oldspec.yaml").getAbsolutePath();

assertDoesNotThrow(mojo::execute);
}

@Test
void Should_BackwardIncompatibilityException_When_WantsExceptionAndSpecIsIncompatible() {
final OpenApiDiffMojo mojo = new OpenApiDiffMojo();
mojo.oldSpec = new File("src/test/resources/newspec.yaml").getAbsolutePath();
mojo.newSpec = new File("src/test/resources/oldspec.yaml").getAbsolutePath();
mojo.failOnIncompatible = true;

assertThrows(BackwardIncompatibilityException.class, mojo::execute);
}
}
15 changes: 15 additions & 0 deletions maven/src/test/resources/newspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
openapi: 3.0.3
info:
title: Generated API
version: "1.0"
paths:
/hello:
get:
responses:
"200":
description: OK
content:
text/plain:
schema:
type: string
6 changes: 6 additions & 0 deletions maven/src/test/resources/oldspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
openapi: 3.0.3
info:
title: Generated API
version: "1.0"
paths: {}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<modules>
<module>core</module>
<module>cli</module>
<module>maven</module>
</modules>

<groupId>org.openapitools.openapidiff</groupId>
Expand Down Expand Up @@ -31,6 +32,11 @@
<email>[email protected]</email>
<url>https://github.com/joschi</url>
</developer>
<developer>
<name>Josh Kellendonk</name>
<email>[email protected]</email>
<url>https://github.com/misterjoshua</url>
</developer>
</developers>

<issueManagement>
Expand Down