Skip to content

Tag import: parent_id isn't re-validated when it matches the pre-import parent #858

Closed

Description

@ufedaseyeuconsultant

Summary

A tag import (CSV or JSON, with the optional previous_id column from #673) must reject a row whose parent_id is its current parent's pre-import id when the same file renames that parent. It must keep accepting a child that names the parent's id after the import (the end-state id), also when two parents swap their ids.

Fixing this showed that several neighboring cases went wrong in the same code: parent references depended on the order of the rows, kept children could be lost to the delete cascade, parent cycles and imports deeper than the depth limit were not rejected at the Plan step, and some valid imports failed partway through. Each of those is described below with its own acceptance criteria, because the fix for this issue delivers them too.

In the error messages below, "Row N" is the N-th data row of the file, counting from 1. In a CSV file that is spreadsheet line N + 1, because the header is line 1. In a JSON file it is the N-th entry of tags. In the import log, an error raised by a row's action starts with that action, for example Action error in 'update_parent' (#6): Row 2: .... The #6 is the action's number in the plan, which can differ from the row number because the plan adds its own steps and builds a parent's row before its children's rows. The row is the Row N part.

Problem A: a stale parent_id of an existing child is accepted

UpdateParentTag.applies_for decides whether a row changes its tag's parent by comparing the row's parent_id with the external_id of the tag's parent before the import. When the two match, the row is "no change", so _validate_parent never runs for it.

Take an existing child tag_2 under tag_1. The file renames tag_1 to tag_50 and keeps the tag_2 row with parent_id: tag_1. The tag_2 row matches the pre-import parent, so it is "no change", and the import accepts it silently. After the import no tag has the id tag_1, so the row's parent_id names nothing. #673's scenario "a child row referencing its parent's pre-import external_id is rejected" requires rejecting exactly this file.

The existing regression tests only used a brand-new child. A new child is created through CreateTag, which runs _validate_parent, so those tests passed while the existing-child case was broken.

Problem B: the first fix rejected valid swaps

A first attempt at fixing Problem A (reverted) rejected every parent_id that named an id which one rename row gives up and another rename row takes, as happens in a swap. That also rejected a child that names the correct end-state id. For example, when tag_1 and tag_3 swap ids, tag_4 (a child of the old tag_3) correctly says parent_id: tag_1, and the first fix rejected it. As a result, two swapped parents with children could not be imported at all.

Root cause and proposed fix

Both problems come from keying the validation on the wrong condition. A row that names its parent's pre-import id is only a problem when the same file gives that id away: the file renames the parent to another id, or, in a full replace import, the file deletes the parent and renames another tag onto its id.

The fix treats such a row as a parent change, so the per-row parent validation runs for it even though its parent_id matches the parent's pre-import id. When the file renames the parent away, the validation rejects the row. The only ambiguous reading is a row whose parent_id equals the pre-import id of its own current parent while another row of the same file takes that id, as in a swap. That row is rejected too. The error names the row and the parent's new id, for example: Row 2: Parent tag tag_1 is renamed to tag_50 in this file; use tag_50 as the parent_id. When another row takes the id, the error adds a second sentence for the other possible meaning: To move under the tag that takes the id tag_1 in this file instead, import the id changes and the move as two separate files. A child that names the end-state id of its parent is accepted. When the file deletes the parent and renames another tag onto its id, a child that names that id moves under the renamed tag.

A comment in the import plan says that _validate_parent reports a reference to an id the import renames away. Before this fix the comment was wrong for a "no change" row, which never called _validate_parent. With the fix the comment holds, because such a row is now a parent change and reaches _validate_parent.

Acceptance criteria

Every criterion in this issue must hold whatever the order of the rows in the file.

  1. [BE] Support editing an existing tag's external_id (Competency ID) on taxonomy re-import #673's scenario "a child row referencing its parent's pre-import external_id is rejected" is rejected:
    • Given a taxonomy where tag_2 is an existing child of tag_1
    • When the admin imports a file that renames tag_1 to tag_50 and keeps the tag_2 row with parent_id: tag_1
    • Then the Plan step rejects the import with an error that names the tag_2 row and says to use tag_50 as the parent_id. With the tag_2 row as the second row, the error is Row 2: Parent tag tag_1 is renamed to tag_50 in this file; use tag_50 as the parent_id.
    • And nothing in the taxonomy changes.
  2. A swap of two parents with children succeeds:
    • Given tag_2 under tag_1 and tag_4 under tag_3
    • When the admin imports a full replace file (replace=True) that renames tag_3 to tag_1 and tag_1 to tag_3, with tag_2 saying parent_id: tag_3 and tag_4 saying parent_id: tag_1
    • Then the import succeeds
    • And each child stays on the same physical parent: tag_2 is under the tag now called tag_3, and tag_4 is under the tag now called tag_1.
  3. An ambiguous swap reference is still rejected:
    • Given tag_2 under tag_1
    • When the file swaps the ids of tag_1 and tag_3, and the tag_2 row still says parent_id: tag_1
    • Then the Plan step rejects the import, because after the import tag_1 names the old tag_3, so the row could mean "stay with my parent" or "move under the tag now called tag_1"
    • And the error names the tag_2 row, says to use tag_3 as the parent_id, and says how to make the other move. With the tag_2 row as the third row, the error is Row 3: Parent tag tag_1 is renamed to tag_3 in this file; use tag_3 as the parent_id. To move under the tag that takes the id tag_1 in this file instead, import the id changes and the move as two separate files.
  4. test_generate_actions_parent_id_new_id_after_earlier_rename_accepted and the test_generate_actions_rename_external_id_replace_skips_delete* tests continue to pass unchanged.

References

Activity

  1. moved this from Needs additional details to Todo in Competency Criteria and Student Progresson Oct 9, 2026
  2. moved this from In Progress to Unicon Code Review in Competency Criteria and Student Progresson Oct 13, 2026
  3. moved this from In Progress to Unicon Code Review in Competency Criteria and Student Progresson Oct 17, 2026
  4. added a commit that references this issue on Oct 19, 2026
  5. moved this from Unicon Code Review to Community Code Review in Competency Criteria and Student Progresson Oct 20, 2026
  6. added a commit that references this issue on Oct 27, 2026
    6dd06c7
  7. dvarenikqaconsultant commented on Oct 29, 2026

    @dvarenikqaconsultant

    Test report: Tag import parent_id re-validation (#858)

    Environment: Master Sandbox, https://studio.master.openedx.io (openedx-core 1.17.0, which includes the fix from #877)
    Test user: dmitryvarenikqa (staff)
    Test type: API (Postman, Studio REST API: PUT .../tags/import/plan/ and PUT .../tags/import/) and UI (Studio taxonomy import wizard)
    Result: All test cases passed. No defects found.

    Stale parent_id after a parent rename (AC 1)

    Case Scenario Expected Actual Result
    TC-1 tag_1 is renamed to tag_50, and child tag_2 still has parent_id=tag_1 The plan is rejected with "Row 2: Parent tag tag_1 is renamed to tag_50 in this file; use tag_50 as the parent_id." Same error, 400 Pass
    TC-2 Same file, but the child row comes before the rename row The same rejection, reported for Row 1 Same error for Row 1, 400 Pass
    TC-3 The TC-1 file is sent straight to import, skipping the plan The import is rejected and the taxonomy does not change 400, and the export is identical to the original taxonomy Pass
    TC-11 Stale parent_id written in a different letter case (TAG_1) The rejection still happens "Row 2: Parent tag TAG_1 is renamed to tag_50 ...", 400 Pass
    TC-13 Two children both point to the stale parent id Both rows are reported in one error block One block with errors for Row 2 and Row 3; the import is rejected and the taxonomy does not change Pass
    TC-20 The CSV variant of the TC-1 file, with a header row The error counts data rows, so it names Row 2 "Row 2: Parent tag tag_1 is renamed to tag_50 ...", 400 Pass

    Child references the parent's new id (AC 4)

    Case Scenario Expected Actual Result
    TC-9 tag_1 is renamed to tag_50, and the child uses parent_id=tag_50 The import succeeds 200; the export shows tag_50 "Tag 1" with child tag_2 Pass
    TC-12 Same as TC-9, with parent_id=TAG_50 in a different letter case The import succeeds 200; tag_2 is under tag_50 Pass
    TC-10 tag_1 takes the id of an existing root tag_3 that is not in the file The old tag_3 is deleted, and tag_1 is renamed in place The plan deletes tag_3 and renames tag_1 to tag_3; the export shows one tag, tag_3 "Tag 1"; the numeric tag id stays the same (13719) Pass

    Swapping the ids of two parents (AC 2 and AC 3)

    Case Scenario Expected Actual Result
    TC-4 tag_1 and tag_3 swap ids, parent rows first, and each child points to its parent's new id The plan has no errors, and the import succeeds 200; export: tag_3 "Tag 1" > tag_2, tag_1 "Tag 3" > tag_4. No "Move ... to the root first" step, because the children keep their physical parents Pass
    TC-5 The same swap, with the child rows first Same result as TC-4 200, same export as TC-4 Pass
    TC-6 The swap, but tag_2 still points to the old id tag_1 Rejected, with a hint to import the id changes and the move as two files "Row 3: Parent tag tag_1 is renamed to tag_3 in this file; use tag_3 as the parent_id. To move under the tag that takes the id tag_1 in this file instead, import the id changes and the move as two separate files.", 400 Pass
    TC-7 Mirror of TC-6: only tag_4 points to the old id Only Row 4 is rejected Error for Row 4 only; the tag_2 row has no error Pass
    TC-8 The two-file approach from the TC-6 hint: after the swap, move both children under tag_1 The plan moves tag_2 to the root first, then to the new parent, which is named explicitly 200; the plan contains "Move tag (tag_2 / Tag 2) to the root first", then "Update the parent ... to tag_1"; both children end up under tag_1 Pass
    TC-21 The swap from TC-4 on a taxonomy whose tags are applied to two units of course course-v1:QA858+TAGS+2026 Content tags stay on the same units, and the tag objects keep their ids Both units show the same tags before and after the swap ("Tag 1 > Tag 2" and "Tag 3 > Tag 4"); numeric tag ids 13724 to 13727 do not change Pass

    Regression checks: row order, moves, cycles, depth, and messages

    Case Scenario Expected Actual Result
    TC-14 Re-import of an unchanged taxonomy "No changes needed" for every row, with no move-to-root steps Plan 200, every row says "No changes needed" Pass
    TC-15 Move tag_2 under tag_3 without any renames The plan moves tag_2 to the root first and names the previous parent explicitly "Move tag (tag_2 / Tag 2) to the root first, so that the parent changes in this file can run in any order.", then "Update the parent ... from parent (tag_1 / Tag 1) to tag_3"; import 200 Pass
    TC-16 A new child row comes before its new parent row The import succeeds 200; tag_6 is under tag_5 Pass
    TC-17 Two tags are each other's parent Rejected as a cycle "Row 1's parent_id (tag_2) creates a parent cycle.", 400 Pass
    TC-18 A chain of 7 levels, and a chain of 6 levels 7 levels are rejected, and 6 levels are accepted 7 levels: "This import would leave tag d7 more than 6 levels deep.", 400. 6 levels: 200, and the chain d1 to d6 is created Pass
    TC-19 A parent id that does not exist The new error text is shown "Unknown parent tag (tag_100). Add a row for the parent to your file.", 400 Pass

    UI checks in the Studio import wizard

    • TC-1: Re-importing the stale-parent JSON file shows the plan log with the error "Row 2: Parent tag tag_1 is renamed to tag_50 in this file; use tag_50 as the parent_id." in a red block on the Upload file step. The Import button stays disabled, so the wizard cannot go further.
    • TC-4: Re-importing the swap CSV file shows "Differences between files" with 6 changes, matching the API plan. After confirmation, Studio shows that the taxonomy was updated. The tree shows Tag 1 > Tag 2 and Tag 3 > Tag 4, and the export matches the API result.

    Existing automated tests (AC 4)

    TC-22 is covered by CI on #877. All checks pass: tests (django52), quality, docs, and package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions