Repository navigation
Fix nested object properties in intersections - #2563
Open
2836048681 wants to merge 1 commit into
Open
2836048681 wants to merge 1 commit into
2836048681 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟡 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 on lines
+33
to
+34
| if (elementA.type === "object" && elementB.type === "object") { | ||
| output[key] = mergeObjectDefinitions(structuredClone(elementB), elementA); |
Comment on lines
+33
to
+34
| if (elementA.type === "object" && elementB.type === "object") { | ||
| output[key] = mergeObjectDefinitions(structuredClone(elementB), elementA); |
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], | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


An intersection such as
{ a: { b: any } } & { a: { c: any } }currently emits onlybundera, losingcand 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:
git diff --check: passed.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.