Skip to content

Report key conflicts clearly when Umbraco won't change a key - #1276

Merged
KevinJump merged 2 commits into
v17/mainfrom
feature/key-conflict-error
Nov 21, 2026
Merged

KevinJump merged 2 commits into
v17/mainfrom
feature/key-conflict-error

Conversation

@KevinJump

Copy link
Copy Markdown
Member

Why

From Umbraco 18 the key of a saved entity can't change (umbraco/Umbraco-CMS#21131). When an import matches an existing item by name and its key differs from the file, uSync tries to set the key and the import fails with a bare "Cannot change the Key of an existing Content". This PR doesn't fix the conflict. It replaces that message with one that says what happened and links to docs. It's step 1 of the plan in docs/key-mapping.md.

What changed

  • SyncKeyHelper.SetKey: every key setter in the serializers now goes through this one helper. That covers content/media/members, doctypes, property types, tabs, data types, dictionary items (only with ForceKeySync), relation types, webhooks and containers. When Umbraco refuses the change on a saved item, the helper throws uSyncKeyConflictException. The exception names the item, the site's key and the file's key, and links to uSyncConstants.KeyConflictDocsUrl. The handler's existing error handling shows that message in the dashboard and the logs. Templates are untouched because file-based entities can still change their key.
  • Version detection: Umbraco's CanChangeKey is protected, so SyncKeyHelper.KeysAreImmutable tries a key change on a throwaway EntityBase once and caches the result. The same code is correct on v17, where the set works, and on v18+, where it throws, with no version checks.
  • Report: when the item on the site has a different key from the file and Umbraco won't allow the change, the report puts a key-conflict warning first in the change details. This never triggers on v17.
  • Content key change: OnKeyChange (URL cache cleanup) now runs after the key is set, so it can't clear the old key's URLs when the set fails. The check before it uses HasIdentity, as v18 does, so the forward-port merges cleanly.
  • docs/key-mapping.md: design for the opt-in KeyConflict: Map follow-up. Nothing in this PR depends on it.

Reviewer notes

  • No behaviour change on v17: keys are still set as before.
  • On v18, a container (folder) with a different key now fails with the key-conflict error and stops that item's import. That's already how v18 behaves, just with a clearer message.
  • The docs page at https://docs.jumoo.co.uk/usync/uSync/reference/key-conflicts doesn't exist yet.
  • Tests: SyncKeyHelperTests (6 tests, written to pass on either version). The full suite passes on v17 (317). With the commits applied to v18/main the full suite also passes (391), and a separate check confirmed KeysAreImmutable is true there.

🤖 Generated with Claude Code

KevinJump and others added 2 commits November 21, 2026 10:39
From Umbraco 18 the key of a saved entity can't change, so when the
import matches an item by name and the keys differ it fails with
"Cannot change the Key of an existing Content". All serializer key
setters now go through SyncKeyHelper.SetKey, which turns that into a
uSyncKeyConflictException naming the item, both keys and a docs link.

The report also warns about the conflict ahead of time, using a probe
that detects whether this Umbraco version blocks key changes, so the
same code runs unchanged on v17 (where the set still works).

Adds docs/key-mapping.md, the design for the KeyConflict: Map follow-up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the release/17.6.3 uSync 17.6.3 release label Nov 21, 2026
@KevinJump
KevinJump merged commit 4c344be into v17/main Nov 21, 2026
9 checks passed
@KevinJump
KevinJump deleted the feature/key-conflict-error branch November 21, 2026 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release/17.6.3 uSync 17.6.3 release

Development

Successfully merging this pull request may close these issues.

1 participant