removed limitation where subtypes should be unique (#856)
* removed limitation where subtypes should be unique There can be use cases where different type labels should match with the same subtype * added test for PolymorphicJsonAdapter non unique subtypes
This commit is contained in:
parent
54c44c5570
commit
687acba760
2 changed files with 19 additions and 13 deletions
|
@ -154,8 +154,8 @@ public final class PolymorphicJsonAdapterFactory<T> implements JsonAdapter.Facto
|
|||
public PolymorphicJsonAdapterFactory<T> withSubtype(Class<? extends T> subtype, String label) {
|
||||
if (subtype == null) throw new NullPointerException("subtype == null");
|
||||
if (label == null) throw new NullPointerException("label == null");
|
||||
if (labels.contains(label) || subtypes.contains(subtype)) {
|
||||
throw new IllegalArgumentException("Subtypes and labels must be unique.");
|
||||
if (labels.contains(label)) {
|
||||
throw new IllegalArgumentException("Labels must be unique.");
|
||||
}
|
||||
List<String> newLabels = new ArrayList<>(labels);
|
||||
newLabels.add(label);
|
||||
|
|
|
@ -160,16 +160,22 @@ public final class PolymorphicJsonAdapterFactoryTest {
|
|||
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
|
||||
}
|
||||
|
||||
@Test public void uniqueSubtypes() {
|
||||
PolymorphicJsonAdapterFactory<Message> factory =
|
||||
PolymorphicJsonAdapterFactory.of(Message.class, "type")
|
||||
.withSubtype(Success.class, "success");
|
||||
try {
|
||||
factory.withSubtype(Success.class, "data");
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertThat(expected).hasMessage("Subtypes and labels must be unique.");
|
||||
}
|
||||
@Test public void nonUniqueSubtypes() throws IOException {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(PolymorphicJsonAdapterFactory.of(Message.class, "type")
|
||||
.withSubtype(Success.class, "success")
|
||||
.withSubtype(Success.class, "data")
|
||||
.withSubtype(Error.class, "error"))
|
||||
.build();
|
||||
|
||||
JsonAdapter<Message> adapter = moshi.adapter(Message.class);
|
||||
|
||||
assertThat(adapter.fromJson("{\"type\":\"success\",\"value\":\"Okay!\"}"))
|
||||
.isEqualTo(new Success("Okay!"));
|
||||
assertThat(adapter.fromJson("{\"type\":\"data\",\"value\":\"Data!\"}"))
|
||||
.isEqualTo(new Success("Data!"));
|
||||
assertThat(adapter.fromJson("{\"type\":\"error\",\"error_logs\":{\"order\":66}}"))
|
||||
.isEqualTo(new Error(Collections.<String, Object>singletonMap("order", 66d)));
|
||||
}
|
||||
|
||||
@Test public void uniqueLabels() {
|
||||
|
@ -180,7 +186,7 @@ public final class PolymorphicJsonAdapterFactoryTest {
|
|||
factory.withSubtype(Error.class, "data");
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertThat(expected).hasMessage("Subtypes and labels must be unique.");
|
||||
assertThat(expected).hasMessage("Labels must be unique.");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue