Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/main/java/com/github/jasminb/jsonapi/ResourceConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,12 @@ private ObjectNode getDataNode(Object object, Map<String, ObjectNode> 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
Expand All @@ -920,6 +926,11 @@ private ObjectNode getDataNode(Object object, Map<String, ObjectNode> 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) {
Expand All @@ -931,7 +942,6 @@ private ObjectNode getDataNode(Object object, Map<String, ObjectNode> includedCo
}
}
}

}

if (relationshipsNode.size() > 0) {
Expand Down Expand Up @@ -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());
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/github/jasminb/jsonapi/SerializationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>(status));

Assert.assertTrue(new String(data).contains("token"));
}

private JSONAPIDocument<User> createDocument(User user) {
JSONAPIDocument<User> document = new JSONAPIDocument<>(user);

Expand Down