Skip to content

Accept camelCase JSON keys from OTBR REST API#228

Merged
agners merged 2 commits into
home-assistant-libs:mainfrom
rvben:fix/camelcase-key-normalization
Mar 5, 2026
Merged

Accept camelCase JSON keys from OTBR REST API#228
agners merged 2 commits into
home-assistant-libs:mainfrom
rvben:fix/camelcase-key-normalization

Conversation

@rvben

@rvben rvben commented Feb 13, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add key normalization in from_json methods to accept both camelCase and PascalCase JSON keys
  • The OTBR REST API OpenAPI spec uses camelCase, but the library expected PascalCase
  • PascalCase keys still work (backward compatible)

Fixes #227

Changes

  • models.py: Add _normalize_keys() helper and call it in each from_json before schema validation
  • test_models.py: Add tests for camelCase deserialization

Test plan

  • All existing tests pass
  • Tested against live openthread/otbr:latest REST API

The OTBR REST API (per the OpenAPI spec in ot-br-posix) returns camelCase
JSON keys (e.g. activeTimestamp, channel, extPanId), but the from_json
methods expected PascalCase (ActiveTimestamp, Channel, ExtPanId).

Add a key normalization step that maps camelCase to PascalCase before
schema validation. PascalCase keys pass through unchanged, so this is
fully backward compatible.

Fixes home-assistant-libs#227
@rvben rvben force-pushed the fix/camelcase-key-normalization branch from 618b3c1 to b943b77 Compare February 13, 2026 14:55
@puddly

puddly commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

@emontnemery Are you fine with this approach?

@puddly

puddly commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

I can confirm this gets the OTBR API integration in Home Assistant working correctly:
image

Personally, I'm fine with this approach. It's the smallest fix. In the future, we can look at inverting the case change to be backwards-compatible with older OTBRs instead of the other way around.

@agners agners left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we can look at inverting the case change to be backwards-compatible with older OTBRs instead of the other way around.

Yeah agreed, we could even make an argument to already do that today.

Comment thread python_otbr_api/models.py

import voluptuous as vol # type: ignore[import]

# The OTBR REST API uses camelCase keys (per the OpenAPI spec in ot-br-posix),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I see this is another breaking change of openthread/ot-br-posix#2514, which introduced sweeping changes to the REST API. It follows the JSON API specification, which recommends camelCasing.

Comment thread python_otbr_api/models.py
Comment thread tests/test_models.py Outdated
Co-authored-by: Stefan Agner <stefan@agner.ch>
@agners agners merged commit 5e5197a into home-assistant-libs:main Mar 5, 2026
1 of 2 checks passed
@rvben

rvben commented Mar 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for merging the PR! 🚀

@rvben rvben deleted the fix/camelcase-key-normalization branch March 5, 2026 21:08
@olivluca

olivluca commented Apr 3, 2026

Copy link
Copy Markdown

shoulnd't it also emit camelCase as well as accepting it in replies?
I'm using ot-br-posix on a lidl gateway and ha complains "Failed to call OTBR API" if I try to change the channel.
I see that it's sending the keys in PascalCase and ot-br-posix replies HTTP/1.1 405 Method Not Allowed

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.

ActiveDataSet.from_json fails with current OTBR REST API due to camelCase vs PascalCase key mismatch

4 participants