fix: return 409 Conflict instead of 500 when creating a duplicate admin or realm - #80
Merged
Merged
Conversation
pointche-ens
requested review from
Manuthor and
charming-wicket-5502
and
a lite review from Copilot
September 4, 2026 12:34
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The core behavior change is correctly implemented across all backends and covered by targeted tests, with only a minor test naming/doc wording nit noted.
Pull request overview
This PR updates the database insert paths for realm and admin creation so that attempting to create an already-existing realm/admin returns a clean 409 Conflict (instead of bubbling up a 500 with database error internals), aligning behavior across SQLite/PostgreSQL/MySQL and strengthening the API’s error contract.
Changes:
- Map unique-constraint insert failures to
AuthDbError::Conflictforcreate_realmandcreate_adminacross SQLite/PostgreSQL/MySQL. - Add/adjust integration tests to assert duplicate realm/admin creation yields
409. - Add a branch changelog entry documenting the behavior change.
File summaries
| File | Description |
|---|---|
| server/src/tests/super_admin_api.rs | Adds a test ensuring duplicate realm creation returns 409 Conflict. |
| server/src/tests/admin_api.rs | Tightens the duplicate admin creation test to explicitly assert 409 (and updates imports). |
| server/src/database/impls/sqlite.rs | Maps duplicate realm/admin insert errors to Conflict via AuthDbError::from_insert_error. |
| server/src/database/impls/postgres.rs | Same as SQLite for PostgreSQL backend. |
| server/src/database/impls/mysql.rs | Same as SQLite for MySQL backend. |
| CHANGELOG/fix_admin-realm-create-conflict.md | Documents the 500→409 behavior change for duplicate admin/realm creation. |
Review details
- Files reviewed: 6/6 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Manuthor
approved these changes
Sep 4, 2026
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.
Summary
POST /adminsandPOST /admins/realmsused to leak a raw500with database internals when the target ID already existed (e.g. re-creating the seededadminuser, or an existing realm), instead of a clean409 Conflict.POST /realms/{realm_id}/userpass(AuthDbError::from_insert_error), which hadn't been extended to the admin/realm creation paths.