Skip to content

Commit

Permalink
Fix #8
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 1, 2021
1 parent 8b7a677 commit e0f4206
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
Expand All @@ -18,6 +20,9 @@ public class MoneyDeserializer extends StdDeserializer<Money>
{
private static final long serialVersionUID = 1L;

private final String F_AMOUNT = "amount";
private final String F_CURRENCY = "currency";

public MoneyDeserializer() {
super(Money.class);
}
Expand All @@ -28,6 +33,12 @@ public LogicalType logicalType() {
return LogicalType.POJO;
}

// Needed for proper exception message later on
@Override
public Collection<Object> getKnownPropertyNames() {
return Arrays.<Object>asList(F_AMOUNT, F_CURRENCY);
}

@Override
public Money deserialize(final JsonParser p, final DeserializationContext ctxt)
throws IOException
Expand All @@ -45,18 +56,30 @@ public Money deserialize(final JsonParser p, final DeserializationContext ctxt)
p.nextToken();

switch (field) {
case "amount":
case F_AMOUNT:
amount = ctxt.readValue(p, BigDecimal.class);
break;
case "currency":
case F_CURRENCY:
currencyUnit = ctxt.readValue(p, CurrencyUnit.class);
break;
default:
ctxt.handleUnknownProperty(p, this, handledType(), field);
}
}

return Money.of(currencyUnit, amount);
// 01-Feb-2021, tatu: [datatypes-misc#8] Verify explicitly
String missingName;

if (amount == null) {
missingName = F_AMOUNT;
} else if (currencyUnit == null) {
missingName = F_CURRENCY;
} else {
return Money.of(currencyUnit, amount);
}

return ctxt.reportPropertyInputMismatch(getValueType(ctxt), missingName,
"Property '%s' missing from Object value", missingName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.fasterxml.jackson.datatype.jodamoney;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.TimeZone;
Expand Down Expand Up @@ -81,20 +80,15 @@ protected void verifyException(Throwable e, String... matches)
return;
}
}
fail("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
fail("Expected an exception (type "+e.getClass().getName()+") with one of substrings ("
+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
}

public String quote(String str) {
public String q(String str) {
return '"'+str+'"';
}

protected String aposToQuotes(String json) {
protected String a2q(String json) {
return json.replace("'", "\"");
}

protected <T> T readAndMapFromString(ObjectMapper m, String input, Class<T> cls)
throws IOException
{
return (T) m.readValue("\""+input+"\"", cls);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;

import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
Expand Down Expand Up @@ -55,31 +56,27 @@ public void testShouldDeserializeWhenOrderIsDifferent() throws IOException
/**********************************************************************
*/

public void testShouldFailDeserializationWithoutAmount()
public void testShouldFailDeserializationWithoutAmount() throws Exception
{
final String content = "{\"currency\":\"EUR\"}";

try {
final Money amount = R.readValue(content);
fail("Should not pass but got: "+amount);
} catch (final NullPointerException e) {
verifyException(e, "Amount must not be null");
} catch (final IOException e) {
fail("NullPointerException should have been thrown");
} catch (final MismatchedInputException e) {
verifyException(e, "Property 'amount' missing from Object value");
}
}

public void testShouldFailDeserializationWithoutCurrency()
public void testShouldFailDeserializationWithoutCurrency() throws Exception
{
final String content = "{\"amount\":5000}";

try {
final Money amount = R.readValue(content);
fail("Should not pass but got: "+amount);
} catch (final NullPointerException e) {
verifyException(e, "Currency must not be null");
} catch (final IOException e) {
fail("NullPointerException should have been thrown");
} catch (final MismatchedInputException e) {
verifyException(e, "Property 'currency' missing from Object value");
}
}

Expand All @@ -93,15 +90,16 @@ public void testShouldFailDeserializationWithUnknownProperties()
final Money amount = r.readValue(content);
fail("Should not pass but got: "+amount);
} catch (final IOException e) {
verifyException(e, "test");
verifyException(e, "Unrecognized field \"unknown\"");
verifyException(e, "2 known properties: \"amount\", \"currency\"]");
}
}

public void testShouldPerformDeserializationWithUnknownProperties() throws IOException
{
final ObjectReader r = MAPPER.readerFor(Money.class)
.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
final String content = "{\"amount\":5000,\"currency\":\"EUR\",\"unknown\":\"test\"}";
final String content = a2q("{'amount':5000,'currency':'EUR','unknown':'test'}");
final Money actualAmount = r.readValue(content);
assertEquals(Money.of(CurrencyUnit.EUR, BigDecimal.valueOf(5000)), actualAmount);
}
Expand Down
2 changes: 1 addition & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Modules:

2.13.0 (not yet released)

No changes since 2.12
#8: Improve error handling of "joda-money" `MoneyDeserializer`, `CurrencyUnitDeserializer`

2.12.1 (08-Jan-2021)

Expand Down

0 comments on commit e0f4206

Please sign in to comment.