Skip to content

Commit

Permalink
Merge pull request lightbend#437 from iantabolt/master
Browse files Browse the repository at this point in the history
Make ValidationProblem implement Serializable
  • Loading branch information
havocp authored Mar 29, 2018
2 parents 4c20fb0 + 032dc70 commit aea2ac4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
41 changes: 31 additions & 10 deletions config/src/main/java/com/typesafe/config/ConfigException.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,36 @@ private void writeObject(java.io.ObjectOutputStream out) throws IOException {
ConfigImplUtil.writeOrigin(out, origin);
}

private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();
ConfigOrigin origin = ConfigImplUtil.readOrigin(in);
// For deserialization - uses reflection to set the final origin field on the object
private static <T> void setOriginField(T hasOriginField, Class<T> clazz,
ConfigOrigin origin) throws IOException {
// circumvent "final"
Field f;
try {
f = ConfigException.class.getDeclaredField("origin");
f = clazz.getDeclaredField("origin");
} catch (NoSuchFieldException e) {
throw new IOException("ConfigException has no origin field?", e);
throw new IOException(clazz.getSimpleName() + " has no origin field?", e);
} catch (SecurityException e) {
throw new IOException("unable to fill out origin field in ConfigException", e);
throw new IOException("unable to fill out origin field in " +
clazz.getSimpleName(), e);
}
f.setAccessible(true);
try {
f.set(this, origin);
f.set(hasOriginField, origin);
} catch (IllegalArgumentException e) {
throw new IOException("unable to set origin field", e);
} catch (IllegalAccessException e) {
throw new IOException("unable to set origin field", e);
}
}

private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();
ConfigOrigin origin = ConfigImplUtil.readOrigin(in);
setOriginField(this, ConfigException.class, origin);
}

/**
* Exception indicating that the type of a value does not match the type you
* requested.
Expand Down Expand Up @@ -310,10 +317,10 @@ public NotResolved(String message) {
* {@link ConfigException.ValidationFailed} exception thrown from
* <code>checkValid()</code> includes a list of problems encountered.
*/
public static class ValidationProblem {
public static class ValidationProblem implements Serializable {

final private String path;
final private ConfigOrigin origin;
final private transient ConfigOrigin origin;
final private String problem;

public ValidationProblem(String path, ConfigOrigin origin, String problem) {
Expand Down Expand Up @@ -347,6 +354,20 @@ public String problem() {
return problem;
}

// We customize serialization because ConfigOrigin isn't
// serializable and we don't want it to be
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
ConfigImplUtil.writeOrigin(out, origin);
}

private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();
ConfigOrigin origin = ConfigImplUtil.readOrigin(in);
setOriginField(this, ValidationProblem.class, origin);
}

@Override
public String toString() {
return "ValidationProblem(" + path + "," + origin + "," + problem + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ class ValidationTest extends TestUtils {
checkValidationException(e, expecteds)
}

@Test
def validationFailedSerializable(): Unit = {
// Reusing a previous test case to generate an error
val reference = parseConfig("""{ a : [{},{},{}] }""")
val conf = parseConfig("""{ a : 42 }""")
val e = intercept[ConfigException.ValidationFailed] {
conf.checkValid(reference)
}

val expecteds = Seq(WrongType("a", 1, "list", "number"))

val actual = checkSerializableNoMeaningfulEquals(e)
checkValidationException(actual, expecteds)
}

@Test
def validationAllowsListOverriddenWithSameTypeList() {
val reference = parseConfig("""{ a : [1,2,3] }""")
Expand Down

0 comments on commit aea2ac4

Please sign in to comment.