mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Fix incorrect path in enum adapter error message. (#613)
This commit is contained in:
@@ -291,10 +291,10 @@ final class StandardJsonAdapters {
|
|||||||
if (index != -1) return constants[index];
|
if (index != -1) return constants[index];
|
||||||
|
|
||||||
// We can consume the string safely, we are terminating anyway.
|
// We can consume the string safely, we are terminating anyway.
|
||||||
|
String path = reader.getPath();
|
||||||
String name = reader.nextString();
|
String name = reader.nextString();
|
||||||
throw new JsonDataException("Expected one of "
|
throw new JsonDataException("Expected one of "
|
||||||
+ Arrays.asList(nameStrings) + " but was " + name + " at path "
|
+ Arrays.asList(nameStrings) + " but was " + name + " at path " + path);
|
||||||
+ reader.getPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void toJson(JsonWriter writer, T value) throws IOException {
|
@Override public void toJson(JsonWriter writer, T value) throws IOException {
|
||||||
|
@@ -878,7 +878,7 @@ public final class MoshiTest {
|
|||||||
|
|
||||||
@Test public void invalidEnum() throws Exception {
|
@Test public void invalidEnum() throws Exception {
|
||||||
Moshi moshi = new Moshi.Builder().build();
|
Moshi moshi = new Moshi.Builder().build();
|
||||||
JsonAdapter<Roshambo> adapter = moshi.adapter(Roshambo.class).lenient();
|
JsonAdapter<Roshambo> adapter = moshi.adapter(Roshambo.class);
|
||||||
try {
|
try {
|
||||||
adapter.fromJson("\"SPOCK\"");
|
adapter.fromJson("\"SPOCK\"");
|
||||||
fail();
|
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 {
|
@Test public void nullEnum() throws Exception {
|
||||||
Moshi moshi = new Moshi.Builder().build();
|
Moshi moshi = new Moshi.Builder().build();
|
||||||
JsonAdapter<Roshambo> adapter = moshi.adapter(Roshambo.class).lenient();
|
JsonAdapter<Roshambo> adapter = moshi.adapter(Roshambo.class).lenient();
|
||||||
|
Reference in New Issue
Block a user