Skip to content

Report bad CurrencyUnit input as databind exception rather than UnknownCurrencyException/NPE - #95

Merged
cowtowncoder merged 6 commits into
FasterXML:3.xfrom
pjfanning:fix-javax-money-currency-token-handling
Sep 15, 2026
Merged

cowtowncoder merged 6 commits into
FasterXML:3.xfrom
pjfanning:fix-javax-money-currency-token-handling

Conversation

@pjfanning

Copy link
Copy Markdown
Member

Problem

CurrencyUnitDeserializer does no token check and no error handling:

final String currencyCode = parser.getValueAsString();
return Monetary.getCurrency(currencyCode);

so provider exceptions escape to the caller instead of a JacksonException:

input read as CurrencyUnit before
"XYZQ" javax.money.UnknownCurrencyException: Unknown currency code: XYZQ
{"a":1} java.lang.NullPointerException: Currency Code may not be null
12 UnknownCurrencyException: Unknown currency code: 12 (silently coerced)

getValueAsString() returns null for structured values, which Monetary then rejects with an NPE — so a caller catching JacksonException sees neither.

The sibling joda-money module's CurrencyUnitDeserializer already does this correctly, so the two modules in this repo disagree on error handling for the same conceptual type.

Fix

Mirror joda-money: handleWeirdStringValue() for an unusable currency code, handleUnexpectedToken() for a non-String token. As a bonus this routes through DeserializationProblemHandler, so applications can substitute a value.

⚠️ Behaviour change

UnknownCurrencyException is now reported as InvalidFormatException (a MismatchedInputException, so JacksonException). The existing shouldNotDeserializeInvalidCurrency test asserted the old type, in both javax-money and moneta; both are updated here.

Flagging explicitly since it is user-visible — happy to drop the handleWeirdStringValue half and keep only the non-String-token fix if you would rather preserve UnknownCurrencyException for 3.x.

Tests

Updated shouldNotDeserializeInvalidCurrency (×2 modules) plus new cases for number/object/array input.

javax-money 138 tests and moneta 140 tests all green.

pjfanning and others added 2 commits September 9, 2026 10:24
…not `UnknownCurrencyException`/NPE

`CurrencyUnitDeserializer` did no token check and no error handling:

    final String currencyCode = parser.getValueAsString();
    return Monetary.getCurrency(currencyCode);

so provider exceptions escaped to the caller instead of a `JacksonException`:

    "XYZQ"    -> javax.money.UnknownCurrencyException
    {"a":1}   -> java.lang.NullPointerException: Currency Code may not be null

(`getValueAsString()` returns `null` for structured values, which `Monetary`
then rejects with an NPE.)

Handle both cases the way joda-money's `CurrencyUnitDeserializer` already does:
`handleWeirdStringValue()` for an unusable code and `handleUnexpectedToken()`
for a non-String token. This also lets a `DeserializationProblemHandler`
substitute a value.

NOTE: behaviour change. `UnknownCurrencyException` is now reported as
`InvalidFormatException`; the two existing tests asserting the old type are
updated, in both `javax-money` and `moneta`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@cowtowncoder cowtowncoder added the javax-money Issue related javax-money datatype module label Sep 15, 2026
@cowtowncoder cowtowncoder changed the title (javax-money) Report bad CurrencyUnit input as databind exception rather than UnknownCurrencyException/NPE Report bad CurrencyUnit input as databind exception rather than UnknownCurrencyException/NPE Sep 15, 2026
@cowtowncoder
cowtowncoder merged commit c5015cb into FasterXML:3.x Sep 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

javax-money Issue related javax-money datatype module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants