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 @@ -340,8 +340,10 @@ protected DeferredChanged<ChangedSchema> computeDeferredDiff(
public DeferredChanged<ChangedSchema> computeDiffForReal(
RecursiveSchemaSet refSet, Schema left, Schema right, DiffContext context) {

left = refPointer.resolveRef(this.leftComponents, left, getSchemaRef(left));
right = refPointer.resolveRef(this.rightComponents, right, getSchemaRef(right));
String leftRef = getSchemaRef(left);
String rightRef = getSchemaRef(right);
left = refPointer.resolveRef(this.leftComponents, left, leftRef);
right = refPointer.resolveRef(this.rightComponents, right, rightRef);

left = resolveComposedSchema(leftComponents, left, refSet.getLeftKeys());
right = resolveComposedSchema(rightComponents, right, refSet.getRightKeys());
Expand All @@ -352,11 +354,28 @@ public DeferredChanged<ChangedSchema> computeDiffForReal(
if ((left == null || right == null)
|| !Objects.equals(left.getType(), right.getType())
|| !Objects.equals(left.getFormat(), right.getFormat())) {
return getTypeChangedSchema(left, right, context);
return keepSourceRefs(getTypeChangedSchema(left, right, context), leftRef, rightRef);
}

// If schema type is same then get specific SchemaDiffResult and compare the properties
SchemaDiffResult result = SchemaDiff.getSchemaDiffResult(right.getClass(), openApiDiff);
return result.diff(refSet, leftComponents, rightComponents, left, right, context);
return keepSourceRefs(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: keepSourceRefs can overwrite nested item refs because array diffs return their itemsDiff as the result. A referenced array containing a changed referenced item therefore reports the array's $ref on the item's ChangedSchema; apply refs to the actual owning schema or preserve the child metadata instead of unconditionally overwriting it.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At core/src/main/java/org/openapitools/openapidiff/core/compare/SchemaDiff.java, line 362:

<comment>`keepSourceRefs` can overwrite nested item refs because array diffs return their `itemsDiff` as the result. A referenced array containing a changed referenced item therefore reports the array's `$ref` on the item's `ChangedSchema`; apply refs to the actual owning schema or preserve the child metadata instead of unconditionally overwriting it.</comment>

<file context>
@@ -352,11 +354,28 @@ public DeferredChanged<ChangedSchema> computeDiffForReal(
     // If schema type is same then get specific SchemaDiffResult and compare the properties
     SchemaDiffResult result = SchemaDiff.getSchemaDiffResult(right.getClass(), openApiDiff);
-    return result.diff(refSet, leftComponents, rightComponents, left, right, context);
+    return keepSourceRefs(
+        result.diff(refSet, leftComponents, rightComponents, left, right, context),
+        leftRef,
</file context>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked this against the code and with a targeted test, and I can't reproduce the overwrite: the item's ChangedSchema keeps the refs it was referenced by, and the array's refs land only on the array's own ChangedSchema.

Why the mechanism doesn't hold: ArraySchemaDiffResult#diff does return the itemsDiff through its intermediate flatMap, but the final mapOptional(schemaOptional -> isApplicable(context)) replaces the chain value with the array's own ChangedSchema — the one SchemaDiffResult#diff built in its changedSchema field, with the item already attached via setItems. So by the time the array-level SchemaDiff#keepSourceRefs runs, the value flowing through is the array's schema, not the item's; the item's oldRef/newRef were set by the nested computeDiffForReal call and are never touched again. The same holds for ComposedSchemaDiffResult (oneOf children go into changedMapping, the chain returns super.diff's own value), and every other result type returns either its own changedSchema or a fresh object from getTypeChangedSchema, so no child schema ever reaches an outer keepSourceRefs call.

Test: SchemaDiffTest#schemaRefsAreKeptOnArrayItemChangedSchema with the new schemaDiff/schema-ref-handling-array-*.yaml fixtures (request body $ref → Pets array, items.$ref → Cat, Cat.age type changed between revisions) added in 037144d. It asserts the array reports #/components/schemas/Pets and the item under getItems() reports #/components/schemas/Cat on both sides, with the nested age change still reported on the item — this passes on the previous head too, so it pins existing behavior rather than fixing anything. Full SchemaDiffTest class: 19 tests, 0 failures; core module verify: 272 tests, 0 failures, 2 pre-existing skips.

No change needed; keeping the refs unconditional in keepSourceRefs is safe because a child's refs are always applied by the child's own capture and the outer call only sees the owning schema.

result.diff(refSet, leftComponents, rightComponents, left, right, context),
leftRef,
rightRef);
}

/**
* Keeps the `$ref` values the schemas were referenced by on the resulting {@link ChangedSchema};
* reference resolution drops them, and downstream consumers need to know which component schemas
* were compared.
*/
private static DeferredChanged<ChangedSchema> keepSourceRefs(
DeferredChanged<ChangedSchema> diff, String oldRef, String newRef) {
return diff.mapOptional(
changedSchema -> {
changedSchema.ifPresent(schema -> schema.setOldRef(oldRef).setNewRef(newRef));
return changedSchema;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ private void compareAdditionalProperties(
new ChangedSchema()
.setContext(context)
.setOldSchema(leftAdditionalSchema)
.setNewSchema(rightAdditionalSchema);
.setNewSchema(rightAdditionalSchema)
.setOldRef(ofNullable(leftAdditionalSchema).map(Schema::get$ref).orElse(null))
.setNewRef(ofNullable(rightAdditionalSchema).map(Schema::get$ref).orElse(null));
if (left != null && right != null) {
DeferredChanged<ChangedSchema> addPropChangedSchemaOP =
openApiDiff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class ChangedSchema implements ComposedChanged {
protected DiffContext context;
protected Schema<?> oldSchema;
protected Schema<?> newSchema;
// The `$ref` values the old/new schemas were referenced by before the diff resolved them; null
// when the schema was inline. Metadata for downstream consumers only; it does not affect
// change detection.
protected String oldRef;
protected String newRef;
protected String type;
protected Map<String, ChangedSchema> changedProperties;
protected Map<String, Schema<?>> increasedProperties;
Expand Down Expand Up @@ -238,6 +243,14 @@ public Schema<?> getNewSchema() {
return this.newSchema;
}

public String getOldRef() {
return this.oldRef;
}

public String getNewRef() {
return this.newRef;
}

public String getType() {
return this.type;
}
Expand Down Expand Up @@ -381,6 +394,16 @@ public ChangedSchema setNewSchema(final Schema<?> newSchema) {
return this;
}

public ChangedSchema setOldRef(final String oldRef) {
this.oldRef = oldRef;
return this;
}

public ChangedSchema setNewRef(final String newRef) {
this.newRef = newRef;
return this;
}

public ChangedSchema setType(final String type) {
this.type = type;
return this;
Expand Down Expand Up @@ -586,6 +609,8 @@ public boolean equals(Object o) {
&& Objects.equals(context, that.context)
&& Objects.equals(oldSchema, that.oldSchema)
&& Objects.equals(newSchema, that.newSchema)
&& Objects.equals(oldRef, that.oldRef)
&& Objects.equals(newRef, that.newRef)
&& Objects.equals(type, that.type)
&& Objects.equals(changedProperties, that.changedProperties)
&& Objects.equals(increasedProperties, that.increasedProperties)
Expand Down Expand Up @@ -620,6 +645,8 @@ public int hashCode() {
context,
oldSchema,
newSchema,
oldRef,
newRef,
type,
changedProperties,
increasedProperties,
Expand Down Expand Up @@ -662,6 +689,10 @@ public java.lang.String toString() {
+ this.getOldSchema()
+ ", newSchema="
+ this.getNewSchema()
+ ", oldRef="
+ this.getOldRef()
+ ", newRef="
+ this.getNewRef()
+ ", type="
+ this.getType()
+ ", changedProperties="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openapitools.openapidiff.core.ChangesResolver.getRequestBodyChangedSchema;
import static org.openapitools.openapidiff.core.ChangesResolver.getResponseBodyChangedSchema;
Expand Down Expand Up @@ -52,6 +53,58 @@ public void schemaPropertyTypeChanged() {
assertTrue(changedSchema.getChangedProperties().get("field1").isChangedType());
}

@Test // issue #956
public void schemaRefsAreKeptOnChangedSchema() {
ChangedOpenApi changedOpenApi =
OpenApiCompare.fromLocations(
"schemaDiff/schema-ref-handling-1.yaml", "schemaDiff/schema-ref-handling-2.yaml");

ChangedSchema requestBodySchema =
getRequestBodyChangedSchema(changedOpenApi, POST, "/widgets", "application/json");
assertNotNull(requestBodySchema);
// The request body schema is a reference to a component schema on both sides
assertEquals("#/components/schemas/Widget", requestBodySchema.getOldRef());
assertEquals("#/components/schemas/Widget", requestBodySchema.getNewRef());

// A property that is itself a reference keeps the refs it was reached through
ChangedSchema weight = requestBodySchema.getChangedProperties().get("weight");
assertNotNull(weight);
assertTrue(weight.isChangedType());
assertEquals("#/components/schemas/Weight", weight.getOldRef());
assertEquals("#/components/schemas/Weight", weight.getNewRef());

// An inline property has no refs to keep
ChangedSchema label = requestBodySchema.getChangedProperties().get("label");
assertNotNull(label);
assertTrue(label.isChangedType());
assertNull(label.getOldRef());
assertNull(label.getNewRef());
}

@Test // PR #962 review: a referenced array with a changed referenced item
public void schemaRefsAreKeptOnArrayItemChangedSchema() {
ChangedOpenApi changedOpenApi =
OpenApiCompare.fromLocations(
"schemaDiff/schema-ref-handling-array-1.yaml",
"schemaDiff/schema-ref-handling-array-2.yaml");

ChangedSchema arraySchema =
getRequestBodyChangedSchema(changedOpenApi, POST, "/pets", "application/json");
assertNotNull(arraySchema);
// The request body schema is a reference to the array component on both sides
assertEquals("#/components/schemas/Pets", arraySchema.getOldRef());
assertEquals("#/components/schemas/Pets", arraySchema.getNewRef());

// The referenced item changed; the array diff reports it under items, and the item keeps
// the refs IT was referenced by, not the array's refs
ChangedSchema item = arraySchema.getItems();
assertNotNull(item);
assertEquals("#/components/schemas/Cat", item.getOldRef());
assertEquals("#/components/schemas/Cat", item.getNewRef());
// the nested change inside the referenced item is still reported
assertTrue(item.getChangedProperties().get("age").isChangedType());
}

@Test // issue #485
public void schemaBecomesDeprecatedTest() {
ChangedOpenApi changedOpenApi =
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/resources/schemaDiff/schema-ref-handling-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: 3.0.0
info:
title: Schema Ref Handling Test - Before
version: 1.0.0
paths:
/widgets:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Widget'
responses:
'200':
description: OK
components:
schemas:
Widget:
type: object
properties:
weight:
$ref: '#/components/schemas/Weight'
label:
type: string
Weight:
type: integer
26 changes: 26 additions & 0 deletions core/src/test/resources/schemaDiff/schema-ref-handling-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: 3.0.0
info:
title: Schema Ref Handling Test - After
version: 1.0.0
paths:
/widgets:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Widget'
responses:
'200':
description: OK
components:
schemas:
Widget:
type: object
properties:
weight:
$ref: '#/components/schemas/Weight'
label:
type: integer # Changed from string to integer
Weight:
type: string # Changed from integer to string
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: 3.0.0
info:
title: Schema Ref Handling Array Test - Before
version: 1.0.0
paths:
/pets:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Pets'
responses:
'200':
description: OK
components:
schemas:
Pets:
type: array
items:
$ref: '#/components/schemas/Cat'
Cat:
type: object
properties:
name:
type: string
age:
type: integer
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: 3.0.0
info:
title: Schema Ref Handling Array Test - After
version: 1.0.0
paths:
/pets:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Pets'
responses:
'200':
description: OK
components:
schemas:
Pets:
type: array
items:
$ref: '#/components/schemas/Cat'
Cat:
type: object
properties:
name:
type: string
age:
type: string # Changed from integer to string