Fix incorrect path in enum adapter error message. (#613)
This commit is contained in:
parent
34f8f9472f
commit
137ffc992f
2 changed files with 19 additions and 3 deletions
|
@ -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 {
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue