Fix incorrect path in enum adapter error message. (#613)

This commit is contained in:
Eric Cochran 2018-08-06 22:46:41 -07:00 committed by GitHub
parent 34f8f9472f
commit 137ffc992f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View file

@ -291,10 +291,10 @@ final class StandardJsonAdapters {
if (index != -1) return constants[index];
// We can consume the string safely, we are terminating anyway.
String path = reader.getPath();
String name = reader.nextString();
throw new JsonDataException("Expected one of "
+ Arrays.asList(nameStrings) + " but was " + name + " at path "
+ reader.getPath());
+ Arrays.asList(nameStrings) + " but was " + name + " at path " + path);
}
@Override public void toJson(JsonWriter writer, T value) throws IOException {

View file

@ -878,7 +878,7 @@ public final class MoshiTest {
@Test public void invalidEnum() throws Exception {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Roshambo> adapter = moshi.adapter(Roshambo.class).lenient();
JsonAdapter<Roshambo> adapter = moshi.adapter(Roshambo.class);
try {
adapter.fromJson("\"SPOCK\"");
fail();
@ -888,6 +888,22 @@ public final class MoshiTest {
}
}
@Test public void invalidEnumHasCorrectPathInExceptionMessage() throws Exception {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Roshambo> adapter = moshi.adapter(Roshambo.class);
JsonReader reader = JsonReader.of(new Buffer().writeUtf8("[\"SPOCK\"]"));
reader.beginArray();
try {
adapter.fromJson(reader);
fail();
} catch (JsonDataException expected) {
assertThat(expected).hasMessage(
"Expected one of [ROCK, PAPER, scr] but was SPOCK at path $[0]");
}
reader.endArray();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
}
@Test public void nullEnum() throws Exception {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Roshambo> adapter = moshi.adapter(Roshambo.class).lenient();