Skip to content

Commit

Permalink
Merge pull request #43 from quen2404/oneof-full-support
Browse files Browse the repository at this point in the history
Oneof full support
  • Loading branch information
quen2404 authored Apr 24, 2018
2 parents 0712198 + 3c7657b commit eb2c794
Show file tree
Hide file tree
Showing 64 changed files with 992 additions and 444 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>com.qdesrame</groupId>
<artifactId>openapi-diff</artifactId>
<version>1.2.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>openapi-diff</name>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/qdesrame/openapi/diff/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ public static void main(String... args) {
}
}
if (line.hasOption("state")) {
System.out.println(result.isDiff() ? result.isDiffBackwardCompatible() ? "compatible" : "incompatible" : "no_changes");
System.out.println(result.isChanged().getValue());
System.exit(0);
} else {
System.exit(result.isDiff() ? 1 : 0);
System.exit(result.isUnchanged() ? 0 : 1);
}
} catch (ParseException e) {
// oops, something went wrong
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.qdesrame.openapi.diff.model.ChangedApiResponse;
import com.qdesrame.openapi.diff.model.ChangedResponse;
import com.qdesrame.openapi.diff.model.DiffContext;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;

Expand All @@ -10,6 +11,8 @@
import java.util.Map;
import java.util.Optional;

import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;

/**
* Created by adarsh.sharma on 04/01/18.
*/
Expand All @@ -20,19 +23,18 @@ public ApiResponseDiff(OpenApiDiff openApiDiff) {
this.openApiDiff = openApiDiff;
}

public Optional<ChangedApiResponse> diff(ApiResponses left, ApiResponses right) {
public Optional<ChangedApiResponse> diff(ApiResponses left, ApiResponses right, DiffContext context) {
MapKeyDiff<String, ApiResponse> responseMapKeyDiff = MapKeyDiff.diff(left, right);
ChangedApiResponse changedApiResponse = new ChangedApiResponse(left, right);
ChangedApiResponse changedApiResponse = new ChangedApiResponse(left, right, context);
changedApiResponse.setAddResponses(responseMapKeyDiff.getIncreased());
changedApiResponse.setMissingResponses(responseMapKeyDiff.getMissing());
List<String> sharedResponseCodes = responseMapKeyDiff.getSharedKey();

Map<String, ChangedResponse> resps = new HashMap<>();
for (String responseCode : sharedResponseCodes) {
openApiDiff.getResponseDiff().diff(left.get(responseCode), right.get(responseCode))
openApiDiff.getResponseDiff().diff(left.get(responseCode), right.get(responseCode), context)
.ifPresent(changedResponse -> resps.put(responseCode, changedResponse));
}
changedApiResponse.setChangedResponses(resps);
return changedApiResponse.isDiff() ? Optional.of(changedApiResponse) : Optional.empty();
return isChanged(changedApiResponse);
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/qdesrame/openapi/diff/compare/CacheKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.qdesrame.openapi.diff.compare;

import com.qdesrame.openapi.diff.model.DiffContext;
import lombok.Getter;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

@Getter
public final class CacheKey {
private final String left;
private final String right;
private final DiffContext context;

public CacheKey(final String left, final String right, final DiffContext context) {
this.left = left;
this.right = right;
this.context = context;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

CacheKey cacheKey = (CacheKey) o;

return new EqualsBuilder()
.append(left, cacheKey.left)
.append(right, cacheKey.right)
.append(context, cacheKey.context)
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(left)
.append(right)
.append(context)
.toHashCode();
}
}
16 changes: 10 additions & 6 deletions src/main/java/com/qdesrame/openapi/diff/compare/ContentDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import com.qdesrame.openapi.diff.model.ChangedContent;
import com.qdesrame.openapi.diff.model.ChangedMediaType;
import com.qdesrame.openapi.diff.model.DiffContext;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;

import java.util.*;

import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;
import static com.qdesrame.openapi.diff.utils.ChangedUtils.isUnchanged;

public class ContentDiff implements Comparable<Content> {

private OpenApiDiff openApiDiff;
Expand All @@ -20,8 +24,8 @@ public boolean compare(Content left, Content right) {
return false;
}

public Optional<ChangedContent> diff(Content left, Content right) {
ChangedContent changedContent = new ChangedContent(left, right);
public Optional<ChangedContent> diff(Content left, Content right, DiffContext context) {
ChangedContent changedContent = new ChangedContent(left, right, context);

MapKeyDiff<String, MediaType> mediaTypeDiff = MapKeyDiff.diff(left, right);
changedContent.setIncreased(mediaTypeDiff.getIncreased());
Expand All @@ -31,13 +35,13 @@ public Optional<ChangedContent> diff(Content left, Content right) {
for (String mediaTypeKey : sharedMediaTypes) {
MediaType oldMediaType = left.get(mediaTypeKey);
MediaType newMediaType = right.get(mediaTypeKey);
ChangedMediaType changedMediaType = new ChangedMediaType(oldMediaType.getSchema(), newMediaType.getSchema());
openApiDiff.getSchemaDiff().diff(new HashSet<>(), oldMediaType.getSchema(), newMediaType.getSchema()).ifPresent(changedMediaType::setChangedSchema);
if (changedMediaType.isDiff()) {
ChangedMediaType changedMediaType = new ChangedMediaType(oldMediaType.getSchema(), newMediaType.getSchema(), context);
openApiDiff.getSchemaDiff().diff(new HashSet<>(), oldMediaType.getSchema(), newMediaType.getSchema(), context).ifPresent(changedMediaType::setChangedSchema);
if (!isUnchanged(changedMediaType)) {
changedMediaTypes.put(mediaTypeKey, changedMediaType);
}
}
changedContent.setChanged(changedMediaTypes);
return changedContent.isDiff() ? Optional.of(changedContent) : Optional.empty();
return isChanged(changedContent);
}
}
17 changes: 10 additions & 7 deletions src/main/java/com/qdesrame/openapi/diff/compare/HeaderDiff.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.qdesrame.openapi.diff.compare;

import com.qdesrame.openapi.diff.model.ChangedHeader;
import com.qdesrame.openapi.diff.model.DiffContext;
import com.qdesrame.openapi.diff.utils.RefPointer;
import com.qdesrame.openapi.diff.utils.RefType;
import io.swagger.v3.oas.models.Components;
Expand All @@ -10,6 +11,8 @@
import java.util.Objects;
import java.util.Optional;

import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;

/**
* Created by adarsh.sharma on 28/12/17.
*/
Expand All @@ -25,26 +28,26 @@ public HeaderDiff(OpenApiDiff openApiDiff) {
this.rightComponents = openApiDiff.getNewSpecOpenApi() != null ? openApiDiff.getNewSpecOpenApi().getComponents() : null;
}

public Optional<ChangedHeader> diff(Header left, Header right) {
return cachedDiff(new HashSet<>(), left, right, left.get$ref(), right.get$ref());
public Optional<ChangedHeader> diff(Header left, Header right, DiffContext context) {
return cachedDiff(new HashSet<>(), left, right, left.get$ref(), right.get$ref(), context);
}

@Override
protected Optional<ChangedHeader> computeDiff(HashSet<String> refSet, Header left, Header right) {
protected Optional<ChangedHeader> computeDiff(HashSet<String> refSet, Header left, Header right, DiffContext context) {
left = refPointer.resolveRef(leftComponents, left, left.get$ref());
right = refPointer.resolveRef(rightComponents, right, right.get$ref());

ChangedHeader changedHeader = new ChangedHeader(left, right);
ChangedHeader changedHeader = new ChangedHeader(left, right, context);

changedHeader.setChangeDescription(!Objects.equals(left.getDescription(), right.getDescription()));
changedHeader.setChangeRequired(getBooleanDiff(left.getRequired(), right.getRequired()));
changedHeader.setChangeDeprecated(!Boolean.TRUE.equals(left.getDeprecated()) && Boolean.TRUE.equals(right.getDeprecated()));
changedHeader.setChangeStyle(!Objects.equals(left.getStyle(), right.getStyle()));
changedHeader.setChangeExplode(getBooleanDiff(left.getExplode(), right.getExplode()));
openApiDiff.getSchemaDiff().diff(new HashSet<>(), left.getSchema(), right.getSchema()).ifPresent(changedHeader::setChangedSchema);
openApiDiff.getContentDiff().diff(left.getContent(), right.getContent()).ifPresent(changedHeader::setChangedContent);
openApiDiff.getSchemaDiff().diff(new HashSet<>(), left.getSchema(), right.getSchema(), context).ifPresent(changedHeader::setChangedSchema);
openApiDiff.getContentDiff().diff(left.getContent(), right.getContent(), context).ifPresent(changedHeader::setChangedContent);

return changedHeader.isDiff() ? Optional.of(changedHeader) : Optional.empty();
return isChanged(changedHeader);
}

private boolean getBooleanDiff(Boolean left, Boolean right) {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/qdesrame/openapi/diff/compare/HeadersDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import com.qdesrame.openapi.diff.model.ChangedHeader;
import com.qdesrame.openapi.diff.model.ChangedHeaders;
import com.qdesrame.openapi.diff.model.DiffContext;
import io.swagger.v3.oas.models.headers.Header;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;

/**
* Created by adarsh.sharma on 28/12/17.
*/
Expand All @@ -19,8 +22,8 @@ public HeadersDiff(OpenApiDiff openApiDiff) {
this.openApiDiff = openApiDiff;
}

public Optional<ChangedHeaders> diff(Map<String, Header> left, Map<String, Header> right) {
ChangedHeaders changedHeaders = new ChangedHeaders(left, right);
public Optional<ChangedHeaders> diff(Map<String, Header> left, Map<String, Header> right, DiffContext context) {
ChangedHeaders changedHeaders = new ChangedHeaders(left, right, context);
MapKeyDiff<String, Header> headerMapDiff = MapKeyDiff.diff(left, right);
changedHeaders.setIncreased(headerMapDiff.getIncreased());
changedHeaders.setMissing(headerMapDiff.getMissing());
Expand All @@ -30,11 +33,11 @@ public Optional<ChangedHeaders> diff(Map<String, Header> left, Map<String, Heade
for (String headerKey : sharedHeaderKeys) {
Header oldHeader = left.get(headerKey);
Header newHeader = right.get(headerKey);
openApiDiff.getHeaderDiff().diff(oldHeader, newHeader)
openApiDiff.getHeaderDiff().diff(oldHeader, newHeader, context)
.ifPresent(changedHeader -> changed.put(headerKey, changedHeader));
}
changedHeaders.setChanged(changed);

return changedHeaders.isDiff() ? Optional.of(changedHeaders) : Optional.empty();
return isChanged(changedHeaders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Objects;
import java.util.Optional;

import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;

/**
* Created by adarsh.sharma on 12/01/18.
*/
Expand All @@ -24,6 +26,6 @@ public Optional<ChangedOAuthFlow> diff(OAuthFlow left, OAuthFlow right) {
changedOAuthFlow.setChangedRefreshUrl(!Objects.equals(left.getRefreshUrl(), right.getRefreshUrl()));
}

return changedOAuthFlow.isDiff() ? Optional.of(changedOAuthFlow) : Optional.empty();
return isChanged(changedOAuthFlow);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.util.Optional;

import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;

/**
* Created by adarsh.sharma on 12/01/18.
*/
Expand All @@ -23,6 +25,6 @@ public Optional<ChangedOAuthFlows> diff(OAuthFlows left, OAuthFlows right) {
openApiDiff.getoAuthFlowDiff().diff(left.getClientCredentials(), right.getClientCredentials()).ifPresent(changedOAuthFlows::setChangedClientCredentialOAuthFlow);
openApiDiff.getoAuthFlowDiff().diff(left.getAuthorizationCode(), right.getAuthorizationCode()).ifPresent(changedOAuthFlows::setChangedAuthorizationCodeOAuthFlow);
}
return changedOAuthFlows.isDiff() ? Optional.of(changedOAuthFlows) : Optional.empty();
return isChanged(changedOAuthFlows);
}
}
20 changes: 11 additions & 9 deletions src/main/java/com/qdesrame/openapi/diff/compare/OperationDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import com.qdesrame.openapi.diff.model.ChangedOperation;
import com.qdesrame.openapi.diff.model.ChangedParameters;
import com.qdesrame.openapi.diff.model.DiffContext;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.parameters.Parameter;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;

/**
* Created by adarsh.sharma on 04/01/18.
*/
Expand All @@ -20,34 +22,34 @@ public OperationDiff(OpenApiDiff openApiDiff) {
this.openApiDiff = openApiDiff;
}

public Optional<ChangedOperation> diff(String pathUrl, PathItem.HttpMethod method, Map<String, String> pathParameters, Operation oldOperation, Operation newOperation) {
ChangedOperation changedOperation = new ChangedOperation(pathUrl, method, oldOperation, newOperation);
public Optional<ChangedOperation> diff(Operation oldOperation, Operation newOperation, DiffContext context) {
ChangedOperation changedOperation = new ChangedOperation(context.getUrl(), context.getMethod(), oldOperation, newOperation);

changedOperation.setSummary(newOperation.getSummary());
changedOperation.setDeprecated(!Boolean.TRUE.equals(oldOperation.getDeprecated()) && Boolean.TRUE.equals(newOperation.getDeprecated()));

if (oldOperation.getRequestBody() != null || newOperation.getRequestBody() != null) {
openApiDiff.getRequestBodyDiff().diff(oldOperation.getRequestBody(), newOperation.getRequestBody())
openApiDiff.getRequestBodyDiff().diff(oldOperation.getRequestBody(), newOperation.getRequestBody(), context.copyAsRequest())
.ifPresent(changedOperation::setChangedRequestBody);
}

openApiDiff.getParametersDiff().diff(oldOperation.getParameters(), newOperation.getParameters())
openApiDiff.getParametersDiff().diff(oldOperation.getParameters(), newOperation.getParameters(), context)
.ifPresent(params -> {
removePathParameters(pathParameters, params);
removePathParameters(context.getParameters(), params);
changedOperation.setChangedParameters(params);
});

if (oldOperation.getResponses() != null || newOperation.getResponses() != null) {
openApiDiff.getApiResponseDiff().diff(oldOperation.getResponses(), newOperation.getResponses())
openApiDiff.getApiResponseDiff().diff(oldOperation.getResponses(), newOperation.getResponses(), context.copyAsResponse())
.ifPresent(changedOperation::setChangedApiResponse);
}

if (oldOperation.getSecurity() != null || newOperation.getSecurity() != null) {
openApiDiff.getSecurityRequirementsDiff().diff(oldOperation.getSecurity(), newOperation.getSecurity())
openApiDiff.getSecurityRequirementsDiff().diff(oldOperation.getSecurity(), newOperation.getSecurity(), context)
.ifPresent(changedOperation::setChangedSecurityRequirements);
}

return changedOperation.isDiff() ? Optional.of(changedOperation) : Optional.empty();
return isChanged(changedOperation);
}

public void removePathParameters(Map<String, String> pathParameters, ChangedParameters params) {
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/com/qdesrame/openapi/diff/compare/ParameterDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.qdesrame.openapi.diff.model.ChangedParameter;
import com.qdesrame.openapi.diff.model.ChangedSchema;
import com.qdesrame.openapi.diff.model.DiffContext;
import com.qdesrame.openapi.diff.utils.RefPointer;
import com.qdesrame.openapi.diff.utils.RefType;
import io.swagger.v3.oas.models.Components;
Expand All @@ -11,6 +12,8 @@
import java.util.Objects;
import java.util.Optional;

import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;

public class ParameterDiff extends ReferenceDiffCache<Parameter, ChangedParameter> {

private Components leftComponents;
Expand All @@ -24,13 +27,13 @@ public ParameterDiff(OpenApiDiff openApiDiff) {
this.rightComponents = openApiDiff.getNewSpecOpenApi() != null ? openApiDiff.getNewSpecOpenApi().getComponents() : null;
}

public Optional<ChangedParameter> diff(Parameter left, Parameter right) {
return cachedDiff(new HashSet<>(), left, right, left.get$ref(), right.get$ref());
public Optional<ChangedParameter> diff(Parameter left, Parameter right, DiffContext context) {
return cachedDiff(new HashSet<>(), left, right, left.get$ref(), right.get$ref(), context);
}

@Override
protected Optional<ChangedParameter> computeDiff(HashSet<String> refSet, Parameter left, Parameter right) {
ChangedParameter changedParameter = new ChangedParameter(right.getName(), right.getIn());
protected Optional<ChangedParameter> computeDiff(HashSet<String> refSet, Parameter left, Parameter right, DiffContext context) {
ChangedParameter changedParameter = new ChangedParameter(right.getName(), right.getIn(), context);
left = refPointer.resolveRef(this.leftComponents, left, left.get$ref());
right = refPointer.resolveRef(this.rightComponents, right, right.get$ref());

Expand All @@ -43,14 +46,14 @@ protected Optional<ChangedParameter> computeDiff(HashSet<String> refSet, Paramet
changedParameter.setChangeAllowEmptyValue(getBooleanDiff(left.getAllowEmptyValue(), right.getAllowEmptyValue()));
changedParameter.setChangeStyle(!Objects.equals(left.getStyle(), right.getStyle()));
changedParameter.setChangeExplode(getBooleanDiff(left.getExplode(), right.getExplode()));
Optional<ChangedSchema> changedSchema = openApiDiff.getSchemaDiff().diff(refSet, left.getSchema(), right.getSchema());
Optional<ChangedSchema> changedSchema = openApiDiff.getSchemaDiff().diff(refSet, left.getSchema(), right.getSchema(), context);
if (changedSchema.isPresent()) {
changedParameter.setChangedSchema(changedSchema.get());
}
openApiDiff.getContentDiff().diff(left.getContent(), right.getContent())
openApiDiff.getContentDiff().diff(left.getContent(), right.getContent(), context)
.ifPresent(changedParameter::setChangedContent);

return changedParameter.isDiff() ? Optional.of(changedParameter) : Optional.empty();
return isChanged(changedParameter);
}

private boolean getBooleanDiff(Boolean left, Boolean right) {
Expand Down
Loading

0 comments on commit eb2c794

Please sign in to comment.