Skip to content

fix: PATCH /v1/admin/api-keys/{key_id} returns 500 — cjson roundtrip bug#70

Merged
amavashev merged 1 commit into
mainfrom
fix/api-key-update-500
Apr 9, 2026
Merged

fix: PATCH /v1/admin/api-keys/{key_id} returns 500 — cjson roundtrip bug#70
amavashev merged 1 commit into
mainfrom
fix/api-key-update-500

Conversation

@amavashev
Copy link
Copy Markdown
Collaborator

Summary

PATCH to any API key returns 500 Internal Server Error.

Root cause

The Lua UPDATE_KEY_LUA script uses cjson.decode → modify → cjson.encode roundtrip. Redis cjson converts empty JSON arrays [] to objects {}. When scope_filter or permissions is [], the re-encoded JSON has {} which Jackson cannot deserialize into List<String>, causing an unhandled exception → 500.

Fix

Replace the Lua cjson roundtrip with Java-side read-modify-write:

  1. jedis.get() — read current key JSON
  2. objectMapper.readValue() — deserialize with Jackson
  3. Apply partial updates (only non-null request fields)
  4. objectMapper.writeValueAsString() — serialize with Jackson (clean, no cjson issues)
  5. jedis.set() — write back

Trade-off: not atomic (was Lua atomic), but admin key updates are low-frequency operations.

Test plan

  • mvn verify — 419 tests, 0 failures, 95%+ coverage
  • Repository tests updated to mock get/set instead of eval
  • Tests cover: success, not found, revoked, expired, partial update

Root cause: Redis cjson.decode/encode roundtrip converts empty JSON
arrays [] to objects {}, corrupting scope_filter and permissions fields.
Jackson then fails to deserialize {} into List<String>.

Fix: Replace Lua cjson roundtrip with Java-side read-modify-write
using Jackson serialization. Reads key, validates status, applies
partial updates, writes back with clean Jackson JSON.

Trade-off: not atomic (was Lua atomic), but admin key updates are
low-frequency operations with minimal race risk.
@amavashev amavashev merged commit 438fe01 into main Apr 9, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant