Repository navigation
Conversation
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>
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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.
The PR review raised that SchemaDiff#keepSourceRefs can overwrite nested item refs because ArraySchemaDiffResult returns its itemsDiff. The items diff does pass through the intermediate flatMap, but the final mapOptional replaces the chain value with the array's own ChangedSchema built by SchemaDiffResult (with the item attached), so the array-level keepSourceRefs only stamps refs on the array's schema. The item keeps the refs captured by its own computeDiffForReal call. Add a test for a referenced array whose referenced item changed: the array reports the array's refs and the item reports the item's refs. Part of OpenAPITools#956 Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>
Part of #956 (item 1 of 4: storing the old/new
$refonChangedSchema; the change-list/builder items remain open).What
ChangedSchemanow keeps the$refvalues the old and new schemas were referenced by before the diff resolved them:getOldRef()/getNewRef()(with fluent setters) onChangedSchema, plus the usual equals/hashCode/toString treatment. The values are inert metadata for downstream consumers; they do not affect change detection.SchemaDiff#computeDiffForReal, the single place every schema pair passes through, right beforeRefPointer#resolveRefdrops 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#compareAdditionalPropertiesbuilds oneChangedSchemaoutside 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-*.yamlcovering the three capture shapes, asserted inSchemaDiffTest#schemaRefsAreKeptOnChangedSchema:$refto a component schema: old and new refs kept on the top-levelChangedSchema$refwhose target changed type: refs kept on the propertyChangedSchema(the deferred-cache path)./mvnw verifypasses: 271 core tests, 13 maven-plugin tests, 0 failures (2 pre-existing skips inSwagger2CompatibilityTest). The coveo format check runs as part of the build.Question on the issue text
The last scope line of #956 says
ruleandresultare "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
ChangedSchemanow keeps the$refvalues the old and new schemas were referenced by before the diff resolved them, so downstream consumers can tell which component schemas were compared.SchemaDiff#computeDiffForReal, the single path every schema pair passes through, covering request-body, property, array, andoneOf-mapped refs plus the deferred-cache re-diff; the additional-properties fallback inSchemaDiffResult#compareAdditionalPropertiescaptures refs separately.nullrefs.Part of #956 (item 1 of 4). Note the issue's last scope line assigns
rule/resultownership to #724, which no longer exists; those fields are untouched here.Written for commit 037144d. Summary will update on new commits.