Emit camelCase keys in as_json() to match OTBR REST API spec#239
Emit camelCase keys in as_json() to match OTBR REST API spec#239jnilo1 wants to merge 2 commits into
Conversation
Commit 5e5197a added _normalize_keys() to accept camelCase in GET responses, but as_json() still emitted PascalCase in PUT requests. The upstream ot-br-posix REST API uses cJSON_GetObjectItemCaseSensitive to parse incoming payloads, so PascalCase keys are silently rejected. Add _to_camel_keys() (inverse of _normalize_keys) and apply it in as_json() for Timestamp, SecurityPolicy, ActiveDataSet and PendingDataSet. GET parsing is unchanged (both cases still accepted). Fixes #238
agners
left a comment
There was a problem hiding this comment.
Hm, so this essentially breaks then older OpenThread border routers no?
From looking into the OTBR code, it seems that the (new and old) REST API simply ignores unknown keys. So we can make the library to work for both OTBR versions by simply emitting both capitalization format.
I'd rename _to_camel_keys to something like _to_dual_keys, which then emits both.
Ideally also add unit tests making sure both capitalization formats are emitted.
Small sidenote: routers is actually emitted upper case in OTBR, but it accepts lower case. Since we accept already both, this isn't a problem.
Address review feedback: emit BOTH capitalizations in as_json() so the library works with both older ot-br-posix (PascalCase) and newer versions (camelCase per the OpenAPI spec). Both REST API versions parse payloads case-sensitively and silently ignore unknown keys, so each version reads its preferred capitalization. - Rename _to_camel_keys → _to_dual_keys, emit both variants per known key - Update tests to assert both capitalizations are present at every level - Add roundtrip test covering dual-key input to from_json()
|
Thanks for the thoughtful review! You're absolutely right — emitting only camelCase would break older routers, and the dual-emit approach is a much better fit for a client library that has to talk to both old and new deployments. I've pushed a new commit implementing this:
Tests
All 71 tests pass locally. On
|
Summary
_to_camel_keys()(inverse of_normalize_keys()) and apply it inas_json()forTimestamp,SecurityPolicy,ActiveDataSetandPendingDataSet_normalize_keys()Context
Commit 5e5197a (shipped in v2.9.0) fixed
from_json()to accept camelCase GET responses, butas_json()still emitted PascalCase in PUT requests. Upstreamot-br-posixusescJSON_GetObjectItemCaseSensitiveto parse incoming payloads, so PascalCase keys are silently rejected — causing "Failed to call OTBR API" errors in Home Assistant when changing Thread channel or dataset.Fixes #238
Test plan
as_json()camelCase serializationfrom_json(as_json(x)) == x