Skip to content

Commit

Permalink
Fix for issue Azure#613
Browse files Browse the repository at this point in the history
Updating FlatteningDeserializer to handle objects that only contain
"@odata.type" field and no additional properties
  • Loading branch information
crachmanin committed May 25, 2019
1 parent 5b613bc commit 1dac37e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public Object deserialize(JsonParser jp, DeserializationContext cxt) throws IOEx
// Json object this method is called to handle.
//
JsonNode currentJsonNode = mapper.readTree(jp);
if (currentJsonNode.isNull()){
currentJsonNode = mapper.getNodeFactory().objectNode();
}
final Class<?> tClass = this.defaultDeserializer.handledType();
for (Class<?> c : TypeToken.of(tClass).getTypes().classes().rawTypes()) {
if (c.isAssignableFrom(Object.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ public void canSerializeMapKeysWithDotAndSlash() throws Exception {
Assert.assertEquals("{\"teacher\":{\"students\":{\"af.B/D\":{},\"af.B/C\":{}}},\"tags\":{\"foo.aa\":\"bar\",\"x.y\":\"zz\"},\"properties\":{\"name\":\"school1\"}}", serialized);
}

/**
* Validates decoding and encoding of a type with type id containing dot and no additional properties
* For decoding and encoding base type will be used.
*
* @throws IOException
*/
@Test
public void canHandleTypeWithTypeIdContainingDotAndNoProperties() throws IOException {
JacksonAdapter adapter = new JacksonAdapter();

String rabbitSerialized = "{\"@odata.type\":\"#Favourite.Pet.RabbitWithTypeIdContainingDot\"}";
String shelterSerialized = "{\"properties\":{\"animalsInfo\":[{\"animal\":{\"@odata.type\":\"#Favourite.Pet.RabbitWithTypeIdContainingDot\"}},{\"animal\":{\"@odata.type\":\"#Favourite.Pet.RabbitWithTypeIdContainingDot\"}}]}}";

AnimalWithTypeIdContainingDot rabbitDeserialized = adapter.deserialize(rabbitSerialized, AnimalWithTypeIdContainingDot.class);
Assert.assertTrue(rabbitDeserialized instanceof RabbitWithTypeIdContainingDot);
Assert.assertNotNull(rabbitDeserialized);

AnimalShelter shelterDeserialized = adapter.deserialize(shelterSerialized, AnimalShelter.class);
Assert.assertTrue(shelterDeserialized instanceof AnimalShelter);
Assert.assertEquals(2, shelterDeserialized.animalsInfo().size());
for (FlattenableAnimalInfo animalInfo: shelterDeserialized.animalsInfo()) {
Assert.assertTrue(animalInfo.animal() instanceof RabbitWithTypeIdContainingDot);
Assert.assertNotNull(animalInfo.animal());
}
}

/**
* Validates that decoding and encoding of a type with type id containing dot and can be done.
* For decoding and encoding base type will be used.
Expand Down

0 comments on commit 1dac37e

Please sign in to comment.