Skip to content

Commit

Permalink
Polymorphic information lost on instances with defined polymorphic co…
Browse files Browse the repository at this point in the history
…nfiguration (#546)

Signed-off-by: David Kral <david.k.kral@oracle.com>
  • Loading branch information
Verdent committed Apr 25, 2022
1 parent 37e2400 commit 1fc8c1e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private Class<?> getPolymorphicTypeClass(String alias) {
return entry.getKey();
}
}
throw new JsonbException("Unknown alias \"" + alias + "\" known aliases: "
throw new JsonbException("Unknown alias \"" + alias + "\" of the type " + processedType.getName() + ". Known aliases: "
+ typeInheritanceConfiguration.getAliases().values());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,11 @@ public Constructor<?> getDefaultConstructor() {
}
return defaultConstructor;
}

@Override
public String toString() {
return "ClassModel{"
+ "clazz=" + clazz
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ private void addPolymorphismProperty(TypeInheritanceConfiguration typeInheritanc
Class<?> rawType = classModel.getType();
String alias = typeInheritanceConfiguration.getAliases().get(rawType);
ModelSerializer serializer = createPolymorphismPropertySerializer(typeInheritanceConfiguration, alias);
if ((!typeInheritanceConfiguration.isInherited() || alias != null) && typeInheritanceConfiguration.getParentConfig() != null) {
addParentPolymorphismProperty(typeInheritanceConfiguration.getParentConfig(), propertySerializers, classModel);
}
if (serializer != null) {
if (typeInheritanceConfiguration.getParentConfig() != null) {
addParentPolymorphismProperty(typeInheritanceConfiguration.getParentConfig(), propertySerializers, classModel);
}
propertySerializers.put(typeInheritanceConfiguration.getFieldName(), serializer);
}
for (PropertyModel propertyModel : classModel.getSortedProperties()) {
Expand Down Expand Up @@ -273,10 +273,9 @@ private void addParentPolymorphismProperty(TypeInheritanceConfiguration typeInhe
}
}

private ModelSerializer createPolymorphismPropertySerializer(TypeInheritanceConfiguration typeConfiguration,
String alias) {
private ModelSerializer createPolymorphismPropertySerializer(TypeInheritanceConfiguration configuration, String alias) {
if (alias != null) {
return (value, generator, context) -> generator.write(typeConfiguration.getFieldName(), alias);
return (value, generator, context) -> generator.write(configuration.getFieldName(), alias);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void testUnknownAliasDeserialization() {
JsonbException exception = assertThrows(JsonbException.class,
() -> Jsonbs.defaultJsonb.fromJson("{\"@type\":\"rat\",\"isDog\":false}",
Animal.class));
assertThat(exception.getMessage(), startsWith("Unknown alias \"rat\" known aliases: ["));
assertThat(exception.getMessage(), startsWith("Unknown alias \"rat\" of the type org.eclipse.yasson.customization."
+ "polymorphism.AnnotationPolymorphismTest$Animal. Known aliases: ["));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public void testMultiplePolymorphicInfoPropertyDeserialization() {
assertThat(JSONB.fromJson(json, Labrador.class), instanceOf(Labrador.class));
}

@Test
public void testPolymorphicParentInstanceSerialization() {
String expected = "{\"@type\":\"area\",\"name\":\"North America\",\"population\":600000000}";
Area northAmerica = new Area();
northAmerica.name = "North America";
northAmerica.population = 600000000;
assertThat(JSONB.toJson(northAmerica), is(expected));
}

@Test
public void testPolymorphicParentInstanceDeserialization() {
String json = "{\"@type\":\"area\",\"name\":\"North America\",\"population\":600000000}";
assertThat(JSONB.fromJson(json, Location.class), instanceOf(Area.class));
}

@JsonbTypeInfo(key = "@something", value = {
@JsonbSubtype(alias = "animal", type = Animal.class)
})
Expand All @@ -65,4 +80,28 @@ public static class Labrador implements Dog {
public boolean isLabrador = true;

}

@JsonbTypeInfo({
@JsonbSubtype(alias = "area", type = Area.class)
})
public interface Location {
}

@JsonbTypeInfo(key = "@area", value = {
@JsonbSubtype(alias = "city", type = City.class),
@JsonbSubtype(alias = "state", type = State.class)
})
public static class Area implements Location {
public String name;
public long population;
}

public static class City extends Area {
public String state;
}

public static class State extends Area {
public String capital;
}

}

0 comments on commit 1fc8c1e

Please sign in to comment.