Skip to content

Commit

Permalink
Do not include property values in error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aguibert committed Aug 11, 2019
1 parent a96a982 commit c815f52
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public enum MessageKeys {
INCOMPATIBLE_FACTORY_CREATOR_RETURN_TYPE("incompatibleFactoryCreatorReturnType"),
MULTIPLE_JSONB_CREATORS("multipleJsonbCreators"),
INTERNAL_ERROR("internalError"),
SERIALIZE_VALUE_ERROR("serializeValueError"),
SERIALIZE_PROPERTY_ERROR("serializePropertyError"),
DESERIALIZE_VALUE_ERROR("deserializeValueError"),
PARSING_NUMBER("parsingNumber"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

import org.eclipse.yasson.internal.Marshaller;
import org.eclipse.yasson.internal.model.customization.Customization;
import org.eclipse.yasson.internal.properties.MessageKeys;
import org.eclipse.yasson.internal.properties.Messages;

import javax.json.bind.JsonbException;
import javax.json.bind.serializer.JsonbSerializer;
import javax.json.bind.serializer.SerializationContext;
import javax.json.stream.JsonGenerator;
Expand Down Expand Up @@ -51,12 +48,7 @@ public AbstractValueTypeSerializer(Customization customization) {
@Override
public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) {
Marshaller marshaller = (Marshaller) ctx;
try {
serialize(obj, generator, marshaller);
} catch (Exception e) {
throw new JsonbException(Messages.getMessage(MessageKeys.SERIALIZE_VALUE_ERROR,
obj, obj.getClass().getCanonicalName(), e.getMessage()));
}
serialize(obj, generator, marshaller);
}

protected abstract void serialize(T obj, JsonGenerator generator, Marshaller marshaller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ public ObjectSerializer(CurrentItem<?> wrapper, Type runtimeType, ClassModel cla

@Override
protected void serializeInternal(T object, JsonGenerator generator, SerializationContext ctx) {
final PropertyModel[] allProperties = ((Marshaller) ctx).getMappingContext().getOrCreateClassModel(object.getClass()).getSortedProperties();
final PropertyModel[] allProperties = ((Marshaller) ctx).getMappingContext()
.getOrCreateClassModel(object.getClass()).getSortedProperties();
for (PropertyModel model : allProperties) {
try {
marshallProperty(object, generator, ctx, model);
} catch (Exception e) {
throw new JsonbException(Messages.getMessage(MessageKeys.SERIALIZE_PROPERTY_ERROR,
model.getWriteName(), object.getClass().getCanonicalName(), model.getValue(object)), e);
}
try {
marshallProperty(object, generator, ctx, model);
} catch (Exception e) {
throw new JsonbException(Messages.getMessage(MessageKeys.SERIALIZE_PROPERTY_ERROR, model.getWriteName(),
object.getClass().getCanonicalName()), e);
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/yasson-messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ errorCallingJsonbCreator=Exception occurred during call to JSONB creator on clas
incompatibleFactoryCreatorReturnType=Return type of creator {0} must be of type {1}.
multipleJsonbCreators=More than one @JsonbCreator declared in class {0}.
internalError=Internal error: {0}
serializeValueError=Unable to serialize value ''{0}'' of type {1} because of: {2}
serializePropertyError=Unable to serialize property ''{0}'' from {1} with value ''{2}''
serializePropertyError=Unable to serialize property ''{0}'' from {1}
deserializeValueError=Error deserialize JSON value into type: {0}.
parsingNumber=Error parsing number {0} with format {1}.
unknownBinaryDataStrategy=Unknown binary data strategy: {0}
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/org/eclipse/yasson/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public static void shouldFail(Supplier<?> operation, Class<? extends Throwable>
operation.get();
fail("The operation should have failed with a " + expectedType.getCanonicalName() + " but it succeeded.");
} catch (Throwable t) {
String fullErrorMessage = t.getMessage();
for (Throwable current = t; current.getCause() != null && current.getCause() != current; current = current.getCause()) {
fullErrorMessage += current.getMessage();
String fullErrorMessage = "";
for (Throwable current = t; current != null && current.getCause() != current; current = current.getCause()) {
fullErrorMessage += current.getClass().getCanonicalName() + ": ";
fullErrorMessage += current.getMessage() + "\n";
}
if (expectedType.isAssignableFrom(t.getClass())) {
if (!checkExceptionMessage.apply(fullErrorMessage)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,30 +225,28 @@ public static class NumberContainer {

@Test
public void testSerializeInvalidDouble() {
shouldFail(() -> jsonb.toJson(Double.POSITIVE_INFINITY),
msg -> msg.contains("Unable to serialize value") && msg.contains("java.lang.Double"));

NumberContainer obj = new NumberContainer();
obj.doubleProp = Double.POSITIVE_INFINITY;
shouldFail(() -> jsonb.toJson(obj),
msg -> msg.contains("doubleProp") && msg.contains("NumberContainer"));
shouldFail(() -> jsonb.toJson(Double.POSITIVE_INFINITY));

NumberContainer obj = new NumberContainer();
obj.doubleProp = Double.POSITIVE_INFINITY;
shouldFail(() -> jsonb.toJson(obj), msg -> msg.contains("doubleProp") && msg.contains("NumberContainer"));
}


@Test
public void testSerializeInvalidDoubleCollection() {
NumberContainer obj = new NumberContainer();
obj.collectionProp = Collections.singleton(Double.POSITIVE_INFINITY);
shouldFail(() -> jsonb.toJson(obj),
msg -> msg.contains("collectionProp") && msg.contains("NumberContainer") && msg.contains("Infinity"));
NumberContainer obj = new NumberContainer();
obj.collectionProp = Collections.singleton(Double.POSITIVE_INFINITY);
shouldFail(() -> jsonb.toJson(obj),
msg -> msg.contains("collectionProp") && msg.contains("NumberContainer"));
}

@Test
public void testSerializeInvalidDoubleMap() {
NumberContainer obj = new NumberContainer();
obj.mapProp = Collections.singletonMap("doubleKey", Double.POSITIVE_INFINITY);
shouldFail(() -> jsonb.toJson(obj),
msg -> msg.contains("mapProp") && msg.contains("NumberContainer") && msg.contains("Infinity"));
NumberContainer obj = new NumberContainer();
obj.mapProp = Collections.singletonMap("doubleKey", Double.POSITIVE_INFINITY);
shouldFail(() -> jsonb.toJson(obj),
msg -> msg.contains("mapProp") && msg.contains("NumberContainer"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbException;

import org.eclipse.yasson.Assertions;
import org.eclipse.yasson.defaultmapping.modifiers.model.ChildOfPackagePrivateParent;
import org.eclipse.yasson.defaultmapping.modifiers.model.FieldModifiersClass;
import org.junit.Before;
Expand Down Expand Up @@ -67,14 +68,9 @@ public void testNestedPackagePrivateParent() {
NestedPackageChild child = new NestedPackageChild();
child.id = 1;
child.name = "SomeName";
try {
jsonb.toJson(child);
fail();
} catch (JsonbException ex) {
if (!(ex.getCause() instanceof IllegalAccessException)) {
fail();
}
}
Assertions.shouldFail(() -> jsonb.toJson(child),
msg -> msg.contains("Unable to serialize property 'id'") &&
msg.contains("java.lang.IllegalAccessException"));
}

private class NestedPrivateParent {
Expand All @@ -90,14 +86,8 @@ public void testNestedPrivateParent() {
NestedPrivateChild child = new NestedPrivateChild();
child.id = 1;
child.name = "SomeName";
try {
jsonb.toJson(child);
fail();
} catch (JsonbException ex) {
if (!(ex.getCause() instanceof IllegalAccessException)) {
fail();
}
}
Assertions.shouldFail(() -> jsonb.toJson(child),
msg -> msg.contains("java.lang.IllegalAccessException"));
}


Expand All @@ -114,14 +104,8 @@ public void testNestedStaticPackagePrivateParent() {
NestedStaticPackageChild child = new NestedStaticPackageChild();
child.id = 1;
child.name = "SomeName";
try {
jsonb.toJson(child);
fail();
} catch (JsonbException ex) {
if (!(ex.getCause() instanceof IllegalAccessException)) {
fail();
}
}
Assertions.shouldFail(() -> jsonb.toJson(child),
msg -> msg.contains("java.lang.IllegalAccessException"));
}

private static class NestedStaticPrivateParent {
Expand All @@ -137,14 +121,8 @@ public void testNestedStaticPrivateParent() {
NestedStaticPrivateChild child = new NestedStaticPrivateChild();
child.id = 1;
child.name = "SomeName";
try {
jsonb.toJson(child);
fail();
} catch (JsonbException ex) {
if (!(ex.getCause() instanceof IllegalAccessException)) {
fail();
}
}
Assertions.shouldFail(() -> jsonb.toJson(child),
msg -> msg.contains("java.lang.IllegalAccessException"));
}

}

0 comments on commit c815f52

Please sign in to comment.