From 78e30475cddce13dd398c13836a6470d1201f998 Mon Sep 17 00:00:00 2001 From: bvalentino1 Date: Tue, 16 Apr 2019 08:39:42 -0400 Subject: [PATCH] Add Resource level metadata during serialization --- .../jasminb/jsonapi/ResourceConverter.java | 24 ++++++++++++++++++- .../jasminb/jsonapi/SerializationTest.java | 21 ++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/jasminb/jsonapi/ResourceConverter.java b/src/main/java/com/github/jasminb/jsonapi/ResourceConverter.java index f4794cb..7e75751 100644 --- a/src/main/java/com/github/jasminb/jsonapi/ResourceConverter.java +++ b/src/main/java/com/github/jasminb/jsonapi/ResourceConverter.java @@ -898,6 +898,12 @@ private ObjectNode getDataNode(Object object, Map includedCo ObjectNode identifierNode = objectMapper.createObjectNode(); identifierNode.put(TYPE, relationshipType); identifierNode.put(ID, idValue); + + ObjectNode resourceMeta = getResourceMeta(element, settings); + if (resourceMeta != null && shouldSerializeMeta(settings)) { + identifierNode.set(META, resourceMeta); + } + dataArrayNode.add(identifierNode); // Handle included data @@ -920,6 +926,11 @@ private ObjectNode getDataNode(Object object, Map includedCo identifierNode.put(TYPE, relationshipType); identifierNode.put(ID, idValue); + ObjectNode resourceMeta = getResourceMeta(relationshipObject, settings); + if (resourceMeta != null && shouldSerializeMeta(settings)) { + identifierNode.set(META, resourceMeta); + } + relationshipDataNode.set(DATA, identifierNode); if (shouldSerializeRelationship(relationshipName, settings) && idValue != null) { @@ -931,7 +942,6 @@ private ObjectNode getDataNode(Object object, Map includedCo } } } - } if (relationshipsNode.size() > 0) { @@ -1142,6 +1152,18 @@ private JsonNode getRelationshipMeta(Object source, String relationshipName, Ser return null; } + private ObjectNode getResourceMeta(Object source, SerializationSettings settings) + throws IllegalAccessException { + if (shouldSerializeMeta(settings)) { + Field relationshipMetaField = configuration.getMetaField(source.getClass()); + + if (relationshipMetaField != null && relationshipMetaField.get(source) != null) { + return objectMapper.valueToTree(relationshipMetaField.get(source)); + } + } + return null; + } + private JsonNode getResourceLinks(Object resource, ObjectNode serializedResource, String resourceId, SerializationSettings settings) throws IllegalAccessException { Type type = configuration.getType(resource.getClass()); diff --git a/src/test/java/com/github/jasminb/jsonapi/SerializationTest.java b/src/test/java/com/github/jasminb/jsonapi/SerializationTest.java index 33d28a4..aa538c3 100644 --- a/src/test/java/com/github/jasminb/jsonapi/SerializationTest.java +++ b/src/test/java/com/github/jasminb/jsonapi/SerializationTest.java @@ -8,6 +8,7 @@ import com.github.jasminb.jsonapi.models.SimpleMeta; import com.github.jasminb.jsonapi.models.Status; import com.github.jasminb.jsonapi.models.User; +import com.github.jasminb.jsonapi.models.User.UserMeta; import com.github.jasminb.jsonapi.models.errors.Error; import org.junit.Assert; import org.junit.Before; @@ -260,6 +261,26 @@ public void testLinkWithMeta() throws DocumentSerializationException { Assert.assertTrue(new String(data).contains("bar")); } + @Test + public void testHasResourceMetaWithoutIncluded() throws DocumentSerializationException { + converter.disableSerializationOption(SerializationFeature.INCLUDE_RELATIONSHIP_ATTRIBUTES); + + Status status = new Status(); + status.setId("statusId"); + + User user = new User(); + user.setId("userId"); + UserMeta userMeta = new UserMeta(); + userMeta.token = "token"; + user.meta = userMeta; + + status.setUser(user); + + byte[] data = converter.writeDocument(new JSONAPIDocument(status)); + + Assert.assertTrue(new String(data).contains("token")); + } + private JSONAPIDocument createDocument(User user) { JSONAPIDocument document = new JSONAPIDocument<>(user);