fix(backend): fix upsert on non multiple refs (#17511) - #17518
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes confidence-based upsert behavior for single-reference fields (e.g., createdBy) to prevent “data cleaning” (clearing an existing ref when the incoming ref is empty), and it exports generateRefsInputsForUpsert to enable direct unit testing.
Changes:
- Exported
generateRefsInputsForUpsertfromupsert-utils.js. - Tightened single-ref upsert conditions so updates only occur when the input actually contains data (unless in full synchronization mode).
- Added unit tests covering single vs multiple ref upsert scenarios, including sync mode and confidence/protection cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| opencti-platform/opencti-graphql/src/utils/upsert-utils.js | Exports generateRefsInputsForUpsert and fixes the single-ref “updatable” condition to avoid null/empty overwrites outside sync mode. |
| opencti-platform/opencti-graphql/tests/01-unit/database/middleware-test.js | Adds unit tests for generateRefsInputsForUpsert across single/multiple refs and sync mode behaviors. |
Suppressed comments (6)
opencti-platform/opencti-graphql/tests/01-unit/database/middleware-test.js:433
- Same as above: to reflect actual resolved store shape,
resolvedElement['created-by']should be the author internal_id string, not the full object.
const resolvedElement = { ...baseIndicator, 'created-by': authorA };
opencti-platform/opencti-graphql/tests/01-unit/database/middleware-test.js:454
- Same as above:
resolvedElement['created-by']should be a stored internal_id string.
const resolvedElement = { ...baseIndicator, 'created-by': authorA };
opencti-platform/opencti-graphql/tests/01-unit/database/middleware-test.js:464
- Same as above:
resolvedElement['created-by']should be a stored internal_id string.
const resolvedElement = { ...baseIndicator, 'created-by': authorA };
opencti-platform/opencti-graphql/tests/01-unit/database/middleware-test.js:474
- Same as above:
resolvedElement['created-by']should be a stored internal_id string.
const resolvedElement = { ...baseIndicator, 'created-by': authorA };
opencti-platform/opencti-graphql/tests/01-unit/database/middleware-test.js:485
- Same as above:
resolvedElement['created-by']should be a stored internal_id string.
const resolvedElement = { ...baseIndicator, 'created-by': authorA };
opencti-platform/opencti-graphql/tests/01-unit/database/middleware-test.js:545
- Same as above: even in this test,
resolvedElement['created-by']should reflect the stored internal_id string.
const resolvedElement = { ...baseIndicator, 'created-by': authorA };
| // To prevent too much flickering on multi sources the created-by will be replaced only for strict upper confidence | ||
| const isProtectedCreatedBy = relDef.databaseName === RELATION_CREATED_BY && !isCurrentEmptyData && !isConfidenceUpper; | ||
| const updatable = ((isInputWithData && isCurrentEmptyData) || isConfidenceMatch) && !isProtectedCreatedBy; | ||
| const updatable = isInputWithData && (isCurrentEmptyData || isConfidenceMatch) && !isProtectedCreatedBy; |
| describe('non multiple ref (createdBy)', () => { | ||
| it('should NOT replace an existing ref value with a null input (regression: no data cleaning)', () => { | ||
| // resolvedElement has an author, the incoming patch explicitly provides createdBy = null | ||
| const resolvedElement = { ...baseIndicator, 'created-by': authorA }; |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #17518 +/- ##
==========================================
- Coverage 34.08% 34.07% -0.02%
==========================================
Files 3376 3376
Lines 137588 137588
Branches 37228 37228
==========================================
- Hits 46891 46877 -14
- Misses 90697 90711 +14
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
c61b936
into
master
Proposed changes
This pull request enhances the upsert utilities and their test coverage, specifically focusing on the handling of reference fields in upsert operations. The main change is the export and thorough testing of the
generateRefsInputsForUpsertfunction, ensuring correct behavior for both single and multiple reference fields under various scenarios, including synchronization mode and confidence-based updates.Upsert Utility Improvements
generateRefsInputsForUpsertfunction fromupsert-utils.jsfor direct use in other modules and tests.Test Coverage Enhancements
generateRefsInputsForUpsertinmiddleware-test.js, covering:createdBy): tests for not overwriting with null/undefined, proper filling, replacement with higher confidence, protection against flickering, and explicit removal in synchronization mode.objectLabel): tests for adding missing values, avoiding unnecessary updates, and full replacement in synchronization mode.generateRefsInputsForUpsert.Related issues
How to test this PR
Checklist
Further comments