Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,21 @@ protected DeferredChanged<ChangedSchema> computeDeferredDiff(
CacheKey key = new CacheKey(getSchemaRef(left), getSchemaRef(right), context);
if (key.getLeft() != null && key.getRight() != null) {
return openApiDiff.getDeferredSchemaCache().getOrAddSchema(refSet, key, left, right);
} else {
}
// A pair without a reference on both sides skips the cache guard above. When
// resolveComposedSchema merges an allOf member into the wrapping schema, the cycle can lead
// back into the same pair with no reference left to check, so guard the descent on the
// identity of the pair instead.
if (left == null || right == null) {
return computeDiffForReal(refSet, left, right, context);
}
if (!refSet.enter(left, right)) {
return DeferredChanged.empty();
}
try {
return computeDiffForReal(refSet, left, right, context);
} finally {
refSet.leave(left, right);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.openapitools.openapidiff.core.model.deferred;

import io.swagger.v3.oas.models.media.Schema;
import java.util.HashSet;
import org.openapitools.openapidiff.core.compare.CacheKey;

public class RecursiveSchemaSet {
HashSet<String> leftKeys = new HashSet<>();
HashSet<String> rightKeys = new HashSet<>();
private final HashSet<SchemaPair> schemaPath = new HashSet<>();

public HashSet<String> getLeftKeys() {
return leftKeys;
Expand All @@ -23,4 +25,48 @@ public void put(CacheKey key) {
leftKeys.add(key.getLeft());
rightKeys.add(key.getRight());
}

/**
* Marks the start of comparing a schema pair on the current path. Schemas compared without a
* reference on both sides never reach the guards in {@code DeferredSchemaCache}, and a cycle
* among them (for example through an allOf member whose target got merged into the wrapping
* schema) would otherwise be followed until the stack overflows. Returns false when the pair is
* already being compared higher up the path, in which case the descent must stop.
*/
public boolean enter(Schema<?> left, Schema<?> right) {
return schemaPath.add(new SchemaPair(left, right));
}

/** Marks the end of comparing a schema pair, allowing sibling occurrences to be diffed again. */
public void leave(Schema<?> left, Schema<?> right) {
schemaPath.remove(new SchemaPair(left, right));
}

/** Identity-based pair, since merged schemas may not carry a reference to compare by. */
private static final class SchemaPair {
private final Schema<?> left;
private final Schema<?> right;

private SchemaPair(Schema<?> left, Schema<?> right) {
this.left = left;
this.right = right;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SchemaPair)) {
return false;
}
SchemaPair other = (SchemaPair) o;
return left == other.left && right == other.right;
}

@Override
public int hashCode() {
return 31 * System.identityHashCode(left) + System.identityHashCode(right);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.openapitools.openapidiff.core;
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved

import static org.assertj.core.api.Assertions.assertThat;
import static org.openapitools.openapidiff.core.TestUtils.assertOpenApiAreEquals;
import static org.openapitools.openapidiff.core.TestUtils.assertOpenApiBackwardIncompatible;
import static org.openapitools.openapidiff.core.TestUtils.assertSpecUnchanged;

import java.io.ByteArrayOutputStream;
import org.junit.jupiter.api.Test;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
import org.openapitools.openapidiff.core.output.ConsoleRender;

public class CyclicComposedSchemaTest {
private final String CYCLIC_OLD = "cyclic_composed_1.yaml";
private final String CYCLIC_NEW = "cyclic_composed_2.yaml";
private final String DEEP_OLD = "cyclic_composed_deep_1.yaml";
private final String DEEP_NEW = "cyclic_composed_deep_2.yaml";
private final String SIBLINGS_OLD = "cyclic_composed_siblings_1.yaml";
private final String SIBLINGS_NEW = "cyclic_composed_siblings_2.yaml";

@Test
public void testCyclicComposedSchemaAgainstItself() {
assertOpenApiAreEquals(CYCLIC_OLD, CYCLIC_OLD);
}

@Test
public void testChangeInsideCyclicComposedSchema() {
assertOpenApiBackwardIncompatible(CYCLIC_OLD, CYCLIC_NEW);
}

@Test
public void testDeepAcyclicComposedChainUnchanged() {
assertSpecUnchanged(DEEP_OLD, DEEP_OLD);
}

@Test
public void testChangeAtBottomOfDeepAcyclicComposedChain() {
assertOpenApiBackwardIncompatible(DEEP_OLD, DEEP_NEW);
}

@Test
public void testSiblingBranchesRevisitingSameMergedPair() {
assertOpenApiBackwardIncompatible(SIBLINGS_OLD, SIBLINGS_NEW);
}

@Test
public void testChangeDetectedInSecondSiblingBranch() {
ChangedOpenApi diff = OpenApiCompare.fromLocations(SIBLINGS_OLD, SIBLINGS_NEW);
assertThat(diff.isCompatible()).isFalse();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
new ConsoleRender().render(diff, new java.io.OutputStreamWriter(outputStream));
// The same merged pair is revisited by the `second` sibling property; a
// globally retained pair cache would suppress the change reported there.
assertThat(outputStream.toString())
.contains("Changed property type: first.")
.contains("Changed property type: second.")
.contains("Changed property type");
}
}
40 changes: 40 additions & 0 deletions core/src/test/resources/cyclic_composed_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
openapi: 3.0.1
info:
title: cyclic composed schema test
version: '1.0'
servers:
- url: 'http://localhost:8000/'
paths:
/ping:
get:
operationId: ping
responses:
'200':
description: OK
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/Entry'
components:
schemas:
Entry:
type: object
required:
- id
properties:
id:
type: string
child:
title: Child
allOf:
- $ref: '#/components/schemas/Child'
Child:
type: object
properties:
name:
type: string
parent:
title: Parent
allOf:
- $ref: '#/components/schemas/Entry'
40 changes: 40 additions & 0 deletions core/src/test/resources/cyclic_composed_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
openapi: 3.0.1
info:
title: cyclic composed schema test
version: '1.0'
servers:
- url: 'http://localhost:8000/'
paths:
/ping:
get:
operationId: ping
responses:
'200':
description: OK
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/Entry'
components:
schemas:
Entry:
type: object
required:
- id
properties:
id:
type: string
child:
title: Child
allOf:
- $ref: '#/components/schemas/Child'
Child:
type: object
properties:
name:
type: integer
parent:
title: Parent
allOf:
- $ref: '#/components/schemas/Entry'
88 changes: 88 additions & 0 deletions core/src/test/resources/cyclic_composed_deep_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
openapi: 3.0.1
info:
title: deep acyclic composed schema test
version: '1.0'
servers:
- url: 'http://localhost:8000/'
paths:
/ping:
get:
operationId: ping
responses:
'200':
description: OK
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/Level1'
components:
schemas:
Level1:
type: object
properties:
tag:
type: string
next:
title: Level2
allOf:
- $ref: '#/components/schemas/Level2'
Level2:
type: object
properties:
tag:
type: string
next:
title: Level3
allOf:
- $ref: '#/components/schemas/Level3'
Level3:
type: object
properties:
tag:
type: string
next:
title: Level4
allOf:
- $ref: '#/components/schemas/Level4'
Level4:
type: object
properties:
tag:
type: string
next:
title: Level5
allOf:
- $ref: '#/components/schemas/Level5'
Level5:
type: object
properties:
tag:
type: string
next:
title: Level6
allOf:
- $ref: '#/components/schemas/Level6'
Level6:
type: object
properties:
tag:
type: string
next:
title: Level7
allOf:
- $ref: '#/components/schemas/Level7'
Level7:
type: object
properties:
tag:
type: string
next:
title: Level8
allOf:
- $ref: '#/components/schemas/Level8'
Level8:
type: object
properties:
value:
type: string
Loading