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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public ConverterConfiguration(Class<?>... classes) {
private void processClass(Class<?> clazz) {
if (clazz.isAnnotationPresent(Type.class)) {
Type annotation = clazz.getAnnotation(Type.class);
typeToClassMapping.put(annotation.value(), clazz);
for (String typeName : annotation.value()) {
typeToClassMapping.put(typeName, clazz);
}
typeAnnotations.put(clazz, annotation);
relationshipTypeMap.put(clazz, new HashMap<String, Class<?>>());
relationshipFieldMap.put(clazz, new HashMap<String, Field>());
Expand All @@ -70,8 +72,10 @@ private void processClass(Class<?> clazz) {

Relationship relationship = relationshipField.getAnnotation(Relationship.class);
Class<?> targetType = ReflectionUtils.getFieldType(relationshipField);
relationshipTypeMap.get(clazz).put(relationship.value(), targetType);
relationshipFieldMap.get(clazz).put(relationship.value(), relationshipField);
for (String relationshipName : relationship.value()) {
relationshipTypeMap.get(clazz).put(relationshipName, targetType);
relationshipFieldMap.get(clazz).put(relationshipName, relationshipField);
}
fieldRelationshipMap.put(relationshipField, relationship);

if (relationship.resolve() && relationship.relType() == null) {
Expand Down Expand Up @@ -286,7 +290,7 @@ public String getTypeName(Class<?> clazz) {
Type type = typeAnnotations.get(clazz);

if (type != null) {
return type.value();
return type.value()[0];
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static List<Field> getAnnotatedFields(Class<?> clazz, Class<? extends Ann
* @param clazz type class
* @return name of the type or <code>null</code> in case Type annotation is not present
*/
public static String getTypeName(Class<?> clazz) {
public static String[] getTypeNames(Class<?> clazz) {
Type typeAnnotation = clazz.getAnnotation(Type.class);
return typeAnnotation != null ? typeAnnotation.value() : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ public void setGlobalResolver(RelationshipResolver resolver) {
*/
public void setTypeResolver(RelationshipResolver resolver, Class<?> type) {
if (resolver != null) {
String typeName = ReflectionUtils.getTypeName(type);
String[] typeNames = ReflectionUtils.getTypeNames(type);

if (typeName != null) {
if (typeNames != null) {
typedResolvers.put(type, resolver);
}
}
Expand Down Expand Up @@ -851,7 +851,7 @@ private ObjectNode getDataNode(Object object, Map<String, ObjectNode> includedCo
continue;
}

String relationshipName = relationship.value();
String relationshipName = relationship.value()[0];

ObjectNode relationshipDataNode = objectMapper.createObjectNode();
relationshipsNode.set(relationshipName, relationshipDataNode);
Expand Down Expand Up @@ -1183,7 +1183,7 @@ private JsonNode getRelationshipLinks(Object source, Relationship relationship,
Links links = null;

Field relationshipLinksField = configuration
.getRelationshipLinksField(source.getClass(), relationship.value());
.getRelationshipLinksField(source.getClass(), relationship.value()[0]);

if (relationshipLinksField != null) {
links = (Links) relationshipLinksField.get(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Relationship {
String value();
String[] value();
boolean resolve() default false;
boolean serialise() default true;
boolean serialiseData() default true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
/**
* Resource type name.
*/
String value();
String[] value();

/**
* Resource path, used to generate <code>self</code> link.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Collections;
import java.util.List;

@Type("articles")
@Type({"article", "articles"})
@JsonIdentityInfo(generator = ObjectIdGenerators.StringIdGenerator.class, property = "id")
public class Article {
@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Author {
private String lastName;
private String twitter;

@Relationship("articles")
@Relationship({"articles", "article"})
private Collection<Article> articles;

public String getId() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/github/jasminb/jsonapi/models/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import com.github.jasminb.jsonapi.annotations.Id;
import com.github.jasminb.jsonapi.annotations.Type;

@Type("comments")
@Type({"comments", "comment"})
public class Comment {
@Id
private String id;
private String body;

@Relationship("author")
@Relationship({"author", "authors"})
private Author author;

public String getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @author jbegic
*/
@Type("engineer")
@Type({"engineer", "engineers"})
public class Engineer extends Person {

@Relationship("field")
Expand Down
6 changes: 3 additions & 3 deletions src/test/resources/articles-with-link-objects.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"data": [{
"type": "articles",
"type": "article",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
Expand Down Expand Up @@ -48,7 +48,7 @@
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"type": "comment",
"id": "5",
"attributes": {
"body": "First!"
Expand All @@ -62,7 +62,7 @@
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"type": "comment",
"id": "12",
"attributes": {
"body": "I like XML better"
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/articles.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"data": [{
"type": "articles",
"type": "article",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/missing-type-inclusion.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"data": {
"type": "engineer",
"type": "engineers",
"id": "id",
"attributes": {
"firstName": "John",
Expand Down