Skip to content

Conversation

@paula-stacho
Copy link
Collaborator

@paula-stacho paula-stacho commented Oct 16, 2025

Description

Integrating the field addition with the other schema update methods, so that it uses the same traversal mechanism.
Additionally fixing the name-fetching mechanism so that it aligns with what we consider a child.

Note:
The main goal at this point is to have a predictable and robust behaviour of adding a field:

  1. when the user clicks a + button on a field in diagram, a single new child will appear
  2. we can do this on any valid MongoDBSchema
  3. the resulting schema will be a valid MongoDBJSONSchema

It's not always clear how exactly the resulting schema will look like (consider a field that has multiple object variants ... if the user clicks + button we don't know where they wanna add it 🤷 ).

With this in mind, we apply these heuristics:

  • we add the field to the first object we encounter
  • we don't go too deep in the search for the object (like if there is a mixed type which contains arrays but not objects we don't look into the arrays to see if some of them contains objects.. simply adding another object variant results in a valid schema)

Checklist

  • New tests and/or benchmarks are included
  • Documentation is changed or added
  • If this change updates the UI, screenshots/videos are added and a design review is requested
  • I have signed the MongoDB Contributor License Agreement (https://www.mongodb.com/legal/contributor-agreement)

Motivation and Context

  • Bugfix
  • New feature
  • Dependency update
  • Misc

Open Questions

Dependents

Types of changes

  • Backport Needed
  • Patch (non-breaking change which fixes an issue)
  • Minor (non-breaking change which adds functionality)
  • Major (fix or feature that would cause existing functionality to change)

@github-actions github-actions bot added the fix label Oct 16, 2025
@paula-stacho paula-stacho added the feature flagged PRs labeled with this label will not be included in the release notes of the next release label Oct 21, 2025
@paula-stacho paula-stacho marked this pull request as ready for review October 21, 2025 14:12
@paula-stacho paula-stacho requested a review from a team as a code owner October 21, 2025 14:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the field addition logic to use the same schema traversal mechanism as other schema update methods, ensuring consistent handling of nested and complex schema structures (arrays, mixed types, tuples).

Key changes:

  • Replaces the recursive addFieldToJSONSchema function with integration into the existing updateSchema traversal system
  • Updates getNewUnusedFieldName to use getFieldFromSchema and getDirectChildren for proper schema traversal
  • Adds comprehensive test coverage for field addition across various schema structures (nested objects, arrays, mixed types, tuples)

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/compass-data-modeling/src/utils/schema.ts Removes addFieldToJSONSchema function and refactors getNewUnusedFieldName to use new traversal utilities
packages/compass-data-modeling/src/utils/schema.spec.ts Removes tests for deleted function and adds tests for getNewUnusedFieldName with various schema structures
packages/compass-data-modeling/src/utils/schema-traversal.tsx Adds addField operation support to updateSchema, implements helper functions for adding fields to different schema types, and adds getDirectChildren utility
packages/compass-data-modeling/src/utils/schema-traversal.spec.tsx Adds comprehensive test coverage for addField operation across flat, nested, array, tuple, and mixed type schemas
packages/compass-data-modeling/src/store/apply-edit.ts Updates field addition to use updateSchema instead of deleted addFieldToJSONSchema function

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Collaborator

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

Looks good!

@paula-stacho paula-stacho merged commit 4fe5a20 into main Oct 22, 2025
84 of 85 checks passed
@paula-stacho paula-stacho deleted the COMPASS-9945 branch October 22, 2025 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature flagged PRs labeled with this label will not be included in the release notes of the next release fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants