-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenAPI compatibility checker (#5106)
* add openapi compatibility checker * formatting * fix native build * formatting --------- Co-authored-by: simon.wagner <[email protected]>
- Loading branch information
1 parent
6155ced
commit c5acb15
Showing
6 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...i/src/main/java/io/apicurio/registry/rules/compatibility/OpenApiCompatibilityChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.apicurio.registry.rules.compatibility; | ||
|
||
import io.apicurio.registry.content.TypedContent; | ||
import org.openapitools.openapidiff.core.OpenApiCompare; | ||
import org.openapitools.openapidiff.core.model.Changed; | ||
import org.openapitools.openapidiff.core.model.ChangedOpenApi; | ||
import org.openapitools.openapidiff.core.model.ChangedOperation; | ||
import org.openapitools.openapidiff.core.model.ChangedSchema; | ||
import org.openapitools.openapidiff.core.model.Endpoint; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class OpenApiCompatibilityChecker extends AbstractCompatibilityChecker<Object> { | ||
|
||
@Override | ||
protected Set<Object> isBackwardsCompatibleWith(final String existing, final String proposed, | ||
final Map<String, TypedContent> resolvedReferences) { | ||
ChangedOpenApi diff = OpenApiCompare.fromContents(existing, proposed); | ||
|
||
List<ChangedOperation> incompatibleOperations = diff.getChangedOperations().stream() | ||
.filter(Objects::nonNull).filter(Changed::isIncompatible).toList(); | ||
|
||
List<ChangedSchema> incompatibleSchemas = diff.getChangedSchemas().stream().filter(Objects::nonNull) | ||
.filter(Changed::isIncompatible).toList(); | ||
|
||
List<Endpoint> missingEndpoints = diff.getMissingEndpoints().stream().filter(Objects::nonNull) | ||
.toList(); | ||
|
||
return Stream.of(incompatibleOperations, incompatibleSchemas, missingEndpoints).flatMap(List::stream) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
protected CompatibilityDifference transform(final Object original) { | ||
if (original instanceof ChangedOperation) { | ||
return new SimpleCompatibilityDifference("Incompatible operation", | ||
((ChangedOperation) original).getPathUrl()); | ||
} else if (original instanceof ChangedSchema) { | ||
return new SimpleCompatibilityDifference("Incompatible schema", | ||
((ChangedSchema) original).getNewSchema().getName()); | ||
} else if (original instanceof Endpoint) { | ||
return new SimpleCompatibilityDifference("Missing endpoint", ((Endpoint) original).getPathUrl()); | ||
} | ||
|
||
throw new IllegalArgumentException("Unsupported type: " + original.getClass().getName()); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...c/test/java/io/apicurio/registry/rules/compatibility/OpenApiCompatibilityCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package io.apicurio.registry.rules.compatibility; | ||
|
||
import io.apicurio.registry.content.ContentHandle; | ||
import io.apicurio.registry.content.TypedContent; | ||
import io.apicurio.registry.types.ContentTypes; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collections; | ||
|
||
class OpenApiCompatibilityCheckerTest { | ||
private TypedContent toTypedContent(String content) { | ||
return TypedContent.create(ContentHandle.create(content), ContentTypes.APPLICATION_JSON); | ||
} | ||
|
||
private static final String BEFORE = """ | ||
{ | ||
"openapi": "3.0.0", | ||
"info": { | ||
"title": "Test API", | ||
"version": "1.0.0" | ||
}, | ||
"paths": { | ||
"/test": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"description": "OK" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
"""; | ||
private static final String AFTER_VALID = """ | ||
{ | ||
"openapi": "3.0.0", | ||
"info": { | ||
"title": "Test API", | ||
"version": "1.0.0" | ||
}, | ||
"paths": { | ||
"/test": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"description": "OK" | ||
} | ||
} | ||
} | ||
}, | ||
"/test2": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"description": "OK" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
"""; | ||
private static final String AFTER_INVALID = """ | ||
{ | ||
"openapi": "3.0.0", | ||
"info": { | ||
"title": "Test API", | ||
"version": "1.0.0" | ||
}, | ||
"paths": {} | ||
} | ||
"""; | ||
|
||
@Test | ||
void givenBackwardCompatibleChange_whenCheckingCompatibility_thenReturnCompatibleResult() { | ||
OpenApiCompatibilityChecker checker = new OpenApiCompatibilityChecker(); | ||
TypedContent existing = toTypedContent(BEFORE); | ||
TypedContent proposed = toTypedContent(AFTER_VALID); | ||
CompatibilityExecutionResult result = checker.testCompatibility(CompatibilityLevel.BACKWARD, | ||
Collections.singletonList(existing), proposed, Collections.emptyMap()); | ||
|
||
Assertions.assertTrue(result.isCompatible()); | ||
} | ||
|
||
@Test | ||
void givenIncompatibleChange_whenCheckingCompatibility_thenReturnIncompatibleResult() { | ||
OpenApiCompatibilityChecker checker = new OpenApiCompatibilityChecker(); | ||
TypedContent existing = toTypedContent(BEFORE); | ||
TypedContent proposed = toTypedContent(AFTER_INVALID); | ||
CompatibilityExecutionResult result = checker.testCompatibility(CompatibilityLevel.BACKWARD, | ||
Collections.singletonList(existing), proposed, Collections.emptyMap()); | ||
|
||
Assertions.assertFalse(result.isCompatible()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters