Skip to content

Fix nested object properties in intersections - #2563

Open
2836048681 wants to merge 1 commit into
vega:nextfrom
2836048681:fix/1029-nested-intersection-properties
Open

2836048681 wants to merge 1 commit into
vega:nextfrom
2836048681:fix/1029-nested-intersection-properties

Conversation

@2836048681

Copy link
Copy Markdown

An intersection such as { a: { b: any } } & { a: { c: any } } currently emits only b under a, losing c and its required entry. Recursively combine inline object property definitions so the generated schema contains and requires both fields.

Extract the existing object-definition merge rules from the formatter reducer and reuse them for nested definitions, preserving required-key, additional-property, and enum handling without mutating the merge inputs. Add the issue reproduction with AJV sample validation, plus unit coverage for deeper nesting, optional fields, enum narrowing, input preservation, and open objects.

Fixes #1029.

Validation on Node.js 24.22.1 and TypeScript 6.0.2:

  • All intersection fixtures and merge unit tests: 22 passed, 0 failed (type checking enabled).
  • New regressions on the unmodified base: 3 failed, demonstrating the lost fields/required keys and overwritten openness; existing merge tests passed.
  • TypeScript build: passed.
  • Repository ESLint: passed, 0 errors / 49 warnings.
  • Changed-file Prettier and git diff --check: passed.
  • Built CLI: generated schema matches the expected fixture; AJV accepts both fields and rejects five invalid samples.

The full repository suite was not completed. Both the regular full run and the FAST_TEST full run were interrupted during their initial passing config tests because of slow execution. Focused coverage includes all intersection fixtures, recursive interfaces, enum/primitive unions, arrays, and additional-property cases.

Copilot AI 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.

🟡 Changes recommended

The recursive merge can generate an invalid empty-enum schema that AJV cannot compile, throw on nested hasOwnProperty fields, and weaken constrained index signatures.

3 open issues
What changed in this PR

This PR addresses #1029 by recursively merging inline object properties in intersection schemas, preserving nested fields and required keys.

Changes:

  • Extract shared object-definition merging from the intersection formatter.
  • Recursively combine nested properties using the existing merge rules.
  • Add fixture validation and unit coverage for nesting, required keys, enum narrowing, input preservation, and open objects.
File Description
test/​valid-data/​type-intersection-nested-properties/​schema.json Expected schema containing both nested fields.
test/​valid-data/​type-intersection-nested-properties/​main.ts Reproduction of the nested intersection issue.
test/​valid-data/​type-intersection-nested-properties/​index.test.ts Valid and invalid sample validation.
test/​unit/​deepMerge.test.ts Recursive merge and input-preservation coverage.
src/​Utils/​deepMerge.ts Recursive merging and shared object-definition helper.
src/​Utils/​allOfDefinition.ts Delegate object merging to the shared helper.

Review effort: Balanced

Comment thread src/Utils/deepMerge.ts
Comment on lines +33 to +34
if (elementA.type === "object" && elementB.type === "object") {
output[key] = mergeObjectDefinitions(structuredClone(elementB), elementA);
Comment thread src/Utils/deepMerge.ts
Comment on lines +33 to +34
if (elementA.type === "object" && elementB.type === "object") {
output[key] = mergeObjectDefinitions(structuredClone(elementB), elementA);
Comment thread src/Utils/deepMerge.ts
Comment on lines +85 to +98
if (addProps.anyOf) {
for (const prop of addProps.anyOf as Definition[]) {
if (prop.type) {
additionalTypes = additionalTypes.concat(
Array.isArray(prop.type) ? prop.type : [prop.type],
);
} else {
additionalProps.push(prop);
}
}
} else if (addProps.type) {
additionalTypes = additionalTypes.concat(
Array.isArray(addProps.type) ? addProps.type : [addProps.type],
);
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.

Incorrect output for intersection of records with the same property

2 participants