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

Backwardcompatibility #35

Open
wants to merge 20 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
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/
\.DS_Store
*.html
*.swp
.idea
swagger-diff.iml
testDiff.html
testDiff.md
testDeprecatedApi.html
testNewApi.html

*.iml
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
![Build Status](https://travis-ci.org/Sayi/swagger-diff.svg?branch=master) ![jdk1.8+](https://img.shields.io/badge/jdk-1.8%2B-orange.svg) [![Coverage Status](https://coveralls.io/repos/github/Sayi/swagger-diff/badge.svg)](https://coveralls.io/github/Sayi/swagger-diff) [![Maven](https://maven-badges.herokuapp.com/maven-central/com.deepoove/swagger-diff/badge.svg?style=plastic)](https://maven-badges.herokuapp.com/maven-central/com.deepoove/swagger-diff)

Compare two swagger API specifications(1.x or v2.0) and render the difference to html file or markdown file.
You can also see the non-backwards compatible changes.

## :black_large_square: Command line interface (CLI)

Expand Down Expand Up @@ -73,11 +74,22 @@ v2.0
SwaggerDiff.compareV2("petstore_v2_1.json", "petstore_v2_2.json");
```

## Check Backward comptibility
```java
SwaggerDiff diff = SwaggerDiff.compareV2("petstore_v2_1.json", "petstore_v2_2.json");
boolean isBackwardCompatible = diff.isBackwardsCompatible();
```

## Render difference
#### HTML
```java
String html = new HtmlRender("Changelog",
"http://deepoove.com/swagger-diff/stylesheets/demo.css")
List<String> css = new ArrayList<>();
css.add("https://use.fontawesome.com/releases/v5.0.6/css/all.css");
css.add("http://deepoove.com/swagger-diff/stylesheets/demo.css");
List<String> scripts = new ArrayList<String>();

String html = new HtmlRender("Changelog", css, scripts)
.withBackwardsIncompatibilities()
.render(diff);

try {
Expand Down
Binary file modified changelog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -93,6 +93,11 @@
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
</dependencies>

<profiles>
Expand Down
99 changes: 48 additions & 51 deletions src/main/java/com/deepoove/swagger/diff/SwaggerDiff.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.deepoove.swagger.diff;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.deepoove.swagger.diff.compare.SpecificationDiff;
import com.deepoove.swagger.diff.model.ChangedEndpoint;
import com.deepoove.swagger.diff.model.Endpoint;
import com.fasterxml.jackson.databind.JsonNode;

import io.swagger.models.Swagger;
import io.swagger.models.auth.AuthorizationValue;
import io.swagger.parser.SwaggerCompatConverter;
import io.swagger.parser.SwaggerParser;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

@Data
public class SwaggerDiff {

public static final String SWAGGER_VERSION_V2 = "2.0";
Expand All @@ -31,29 +31,24 @@ public class SwaggerDiff {

/**
* compare two swagger 1.x doc
*
* @param oldSpec
* old api-doc location:Json or Http
* @param newSpec
* new api-doc location:Json or Http
*
* @param oldSpec old api-doc location:Json or Http
* @param newSpec new api-doc location:Json or Http
*/
public static SwaggerDiff compareV1(String oldSpec, String newSpec) {
public static SwaggerDiff compareV1(final String oldSpec, final String newSpec) {
return compare(oldSpec, newSpec, null, null);
}

/**
* compare two swagger v2.0 doc
*
* @param oldSpec
* old api-doc location:Json or Http
* @param newSpec
* new api-doc location:Json or Http
*
* @param oldSpec old api-doc location:Json or Http
* @param newSpec new api-doc location:Json or Http
*/
public static SwaggerDiff compareV2(String oldSpec, String newSpec) {
public static SwaggerDiff compareV2(final String oldSpec, final String newSpec) {
return compare(oldSpec, newSpec, null, SWAGGER_VERSION_V2);
}


/**
* compare two swagger v2.0 Sring
*
Expand All @@ -64,24 +59,11 @@ public static SwaggerDiff compareV2Raw(String oldSpec, String newSpec) {
return new SwaggerDiff(oldSpec, newSpec).compare();
}

/**
* Compare two swagger v2.0 docs by JsonNode
*
* @param oldSpec
* old Swagger specification document in v2.0 format as a JsonNode
* @param newSpec
* new Swagger specification document in v2.0 format as a JsonNode
*/
public static SwaggerDiff compareV2(JsonNode oldSpec, JsonNode newSpec) {
return new SwaggerDiff(oldSpec, newSpec).compare();
}

public static SwaggerDiff compare(String oldSpec, String newSpec,
List<AuthorizationValue> auths, String version) {
List<AuthorizationValue> auths, String version) {
return new SwaggerDiff(oldSpec, newSpec, auths, version).compare();
}


/**
* @param rawOldSpec
* @param rawNewSpec
Expand All @@ -102,8 +84,8 @@ private SwaggerDiff(String rawOldSpec, String rawNewSpec) {
* @param auths
* @param version
*/
private SwaggerDiff(String oldSpec, String newSpec, List<AuthorizationValue> auths,
String version) {
private SwaggerDiff(final String oldSpec, final String newSpec, final List<AuthorizationValue> auths,
final String version) {
if (SWAGGER_VERSION_V2.equals(version)) {
SwaggerParser swaggerParser = new SwaggerParser();
oldSpecSwagger = swaggerParser.read(oldSpec, auths, true);
Expand All @@ -118,36 +100,51 @@ private SwaggerDiff(String oldSpec, String newSpec, List<AuthorizationValue> aut
return;
}
}
if (null == oldSpecSwagger || null == newSpecSwagger) { throw new RuntimeException(
"cannot read api-doc from spec."); }
if (null == oldSpecSwagger || null == newSpecSwagger) {
throw new RuntimeException(
"cannot read api-doc from spec.");
}
}

/**
* Compare two swagger v2.0 docs by JsonNode
*
* @param oldSpec old Swagger specification document in v2.0 format as a JsonNode
* @param newSpec new Swagger specification document in v2.0 format as a JsonNode
*/
public static SwaggerDiff compareV2(JsonNode oldSpec, JsonNode newSpec) {
return new SwaggerDiff(oldSpec, newSpec).compare();
}

private SwaggerDiff(JsonNode oldSpec, JsonNode newSpec) {
SwaggerParser swaggerParser = new SwaggerParser();
oldSpecSwagger = swaggerParser.read(oldSpec, true);
newSpecSwagger = swaggerParser.read(newSpec, true);
if (null == oldSpecSwagger || null == newSpecSwagger) { throw new RuntimeException(
"cannot read api-doc from spec."); }
if (null == oldSpecSwagger || null == newSpecSwagger) {
throw new RuntimeException(
"cannot read api-doc from spec.");
}
}

private SwaggerDiff compare() {
SpecificationDiff diff = SpecificationDiff.diff(oldSpecSwagger, newSpecSwagger);
SpecificationDiff diff = SpecificationDiff.diff(oldSpecSwagger, newSpecSwagger);
this.newEndpoints = diff.getNewEndpoints();
this.missingEndpoints = diff.getMissingEndpoints();
this.changedEndpoints = diff.getChangedEndpoints();
return this;
}

public List<Endpoint> getNewEndpoints() {
return newEndpoints;
}

public List<Endpoint> getMissingEndpoints() {
return missingEndpoints;
}

public List<ChangedEndpoint> getChangedEndpoints() {
return changedEndpoints;
public boolean isBackwardsCompatible() {
if (!getMissingEndpoints().isEmpty()) {
return false;
} else {
for (ChangedEndpoint changedEndpoint : getChangedEndpoints()) {
if (!changedEndpoint.isBackwardsCompatible()) {
return false;
}
}
}
return true;
}

public String getOldVersion() {
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/com/deepoove/swagger/diff/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@

/**
* $java -jar swagger-diff.jar -old http://www.petstore.com/swagger.json \n
* -new http://www.petstore.com/swagger_new.json \n
* -v 2.0 \n
* -output-mode markdown \n
* -new http://www.petstore.com/swagger_new.json \n
* -v 2.0 \n
* -output-mode markdown \n
*
* @author Sayi
* @version
*/
public class CLI {

Expand All @@ -28,7 +27,7 @@ public class CLI {
@Parameter(names = "-new", description = "new api-doc location:Json file path or Http url", required = true, order = 1)
private String newSpec;

@Parameter(names = "-v", description = "swagger version:1.0 or 2.0", validateWith= RegexValidator.class, order = 2)
@Parameter(names = "-v", description = "swagger version:1.0 or 2.0", validateWith = RegexValidator.class, order = 2)
@Regex("(2\\.0|1\\.0)")
private String version = SwaggerDiff.SWAGGER_VERSION_V2;

Expand All @@ -45,14 +44,14 @@ public class CLI {
public static void main(String[] args) {
CLI cli = new CLI();
JCommander jCommander = JCommander.newBuilder()
.addObject(cli)
.build();
.addObject(cli)
.build();
jCommander.parse(args);
cli.run(jCommander);
}

public void run(JCommander jCommander) {
if (help){
if (help) {
jCommander.setProgramName("java -jar swagger-diff.jar");
jCommander.usage();
return;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/deepoove/swagger/diff/cli/Regex.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.deepoove.swagger.diff.cli;

import static java.lang.annotation.ElementType.FIELD;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ FIELD })
@Target({FIELD})
public @interface Regex {

String value() default "";
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/deepoove/swagger/diff/cli/RegexValidator.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.deepoove.swagger.diff.cli;

import java.lang.reflect.Field;
import java.util.regex.Pattern;

import com.beust.jcommander.IParameterValidator2;
import com.beust.jcommander.ParameterDescription;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameterized;

import java.lang.reflect.Field;
import java.util.regex.Pattern;

public class RegexValidator implements IParameterValidator2 {

private static final String PARAMETERIZED_FIELD_NAME = "field";
Expand All @@ -29,8 +29,10 @@ public void validate(String name, String value, ParameterDescription pd)
Regex regex = paramField.getAnnotation(Regex.class);
if (null == regex) return;
String regexStr = regex.value();
if (!Pattern.matches(regexStr, value)) { throw new ParameterException(
"Parameter " + name + " should match " + regexStr + " (found " + value + ")"); }
if (!Pattern.matches(regexStr, value)) {
throw new ParameterException(
"Parameter " + name + " should match " + regexStr + " (found " + value + ")");
}
} catch (NoSuchFieldException e) {
return;
} catch (IllegalArgumentException e) {
Expand Down
28 changes: 6 additions & 22 deletions src/main/java/com/deepoove/swagger/diff/compare/ListDiff.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.deepoove.swagger.diff.compare;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.function.BiFunction;

import com.google.common.collect.Lists;
import lombok.Getter;

import java.util.*;
import java.util.function.BiFunction;

/**
* compare two Lists
*
* @author Sayi
* @version
*/
@Getter
public class ListDiff<K> {

private List<K> increased;
Expand All @@ -33,8 +30,7 @@ public static <K> ListDiff<K> diff(List<K> left, List<K> right) {
*
* @param left
* @param right
* @param biFunc
* if right List contains left element
* @param biFunc if right List contains left element
* @return
*/
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -71,16 +67,4 @@ public static <K> ListDiff<K> diff(List<K> left, List<K> right, BiFunction<List<
return instance;
}

public List<K> getIncreased() {
return increased;
}

public List<K> getMissing() {
return missing;
}

public Map<K, K> getShared() {
return shared;
}

}
Loading