Skip to content

Store the old/new $ref on ChangedSchema - #962

Open
Inf1mum wants to merge 1 commit into
OpenAPITools:masterfrom
Inf1mum:feat/956-changed-schema-ref
Open

Inf1mum wants to merge 1 commit into
OpenAPITools:masterfrom
Inf1mum:feat/956-changed-schema-ref

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 16, 2026 •

Copy link
Copy Markdown

Part of #956 (item 1 of 4: storing the old/new $ref on ChangedSchema; the change-list/builder items remain open).

What

ChangedSchema now keeps the $ref values the old and new schemas were referenced by before the diff resolved them:

  • getOldRef() / getNewRef() (with fluent setters) on ChangedSchema, plus the usual equals/hashCode/toString treatment. The values are inert metadata for downstream consumers; they do not affect change detection.
  • They are captured in SchemaDiff#computeDiffForReal, the single place every schema pair passes through, right before RefPointer#resolveRef drops the refs. This covers the direct path, the deferred schema cache (the raw schemas are re-diffed there later with their refs intact), property refs, array items, oneOf mappings, and the type-changed result.
  • SchemaDiffResult#compareAdditionalProperties builds one ChangedSchema outside that path (the added/removed additional-properties fallback), so it stores the refs from the raw schemas there too.

No renderer or rule changes; nothing consumes the new fields yet. This builds on the ref-following model from #938 and gives the later R0 change-list slices the location information they need.

Tests

New fixture pair schemaDiff/schema-ref-handling-*.yaml covering the three capture shapes, asserted in SchemaDiffTest#schemaRefsAreKeptOnChangedSchema:

  • request body $ref to a component schema: old and new refs kept on the top-level ChangedSchema
  • a property that is itself a $ref whose target changed type: refs kept on the property ChangedSchema (the deferred-cache path)
  • an inline property change: refs stay null

./mvnw verify passes: 271 core tests, 13 maven-plugin tests, 0 failures (2 pre-existing skips in Swagger2CompatibilityTest). The coveo format check runs as part of the build.

Question on the issue text

The last scope line of #956 says rule and result are "owned by #724", but that issue no longer exists (404). Those fields are untouched here either way; asking in case the ownership moved somewhere this branch of work should reference.


Summary by cubic

ChangedSchema now records the $ref values the old and new schemas were referenced by before the diff resolves them, so downstream consumers can tell which component schemas were compared.

  • Ref capture happens in SchemaDiff#computeDiffForReal, the single path every schema pair passes through, covering request-body, property, array, and oneOf-mapped refs plus the deferred-cache re-diff; the additional-properties fallback in SchemaDiffResult#compareAdditionalProperties captures refs separately.
  • The stored refs are inert metadata; they don't affect change detection, and no renderer or rule consumes them yet.
  • New tests verify refs are kept for a request-body ref and a property ref whose target changed type, and that inline properties keep null refs.

Part of #956 (item 1 of 4). Note the issue's last scope line assigns rule/result ownership to #724, which no longer exists; those fields are untouched here.

Written for commit 80f81c7. Summary will update on new commits.

View guided diff

SchemaDiff resolves $ref schemas against the components before
comparing them, so the fact that a schema was reached through a
reference is lost in the diff result: ChangedSchema only keeps the
resolved schemas.

Keep the original $ref values on ChangedSchema before resolution.
They are captured in SchemaDiff#computeDiffForReal, the single place
where every schema pair passes through, and in the additional
properties fallback of SchemaDiffResult, which builds its schema
without going through that path. The refs are inert metadata for
downstream consumers and do not affect change detection.

Part of OpenAPITools#956

Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1 issue found across 6 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. 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. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="core/src/main/java/org/openapitools/openapidiff/core/compare/SchemaDiff.java">

<violation number="1" location="core/src/main/java/org/openapitools/openapidiff/core/compare/SchemaDiff.java:362">
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.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

// 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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant