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>

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,34 @@ 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 // 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