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 @@ -3,6 +3,8 @@
import java.util.HashSet;
import java.util.Set;

import com.github.jasminb.jsonapi.exceptions.UnknownRelationshipException;

/**
* Enumeration that defines list of deserialization features that can be set to {@link ResourceConverter}.
*
Expand All @@ -25,7 +27,13 @@ public enum DeserializationFeature {
* This option determines if relationship (collection) can have unknown type.
* Can be use with polymorphic relationship.
*/
ALLOW_UNKNOWN_TYPE_IN_RELATIONSHIP(false);
ALLOW_UNKNOWN_TYPE_IN_RELATIONSHIP(false),

/**
* This option determines whether encountering undeclared relationships throws
* an {@link UnknownRelationshipException} during conversion
*/
ALLOW_UNKNOWN_RELATIONSHIPS(false);

private final boolean enabledByDefault;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.github.jasminb.jsonapi.annotations.Relationship;
import com.github.jasminb.jsonapi.annotations.Type;
import com.github.jasminb.jsonapi.exceptions.DocumentSerializationException;
import com.github.jasminb.jsonapi.exceptions.UnknownRelationshipException;
import com.github.jasminb.jsonapi.exceptions.UnregisteredTypeException;
import com.github.jasminb.jsonapi.models.errors.Error;

Expand Down Expand Up @@ -543,6 +544,9 @@ private void handleRelationships(JsonNode source, Object object)
}
}
}
} else if (!deserializationFeatures.contains(DeserializationFeature.ALLOW_UNKNOWN_RELATIONSHIPS)) {
throw new UnknownRelationshipException(
"Trying to parse unknown relationship '" + field + "' in " + object.getClass());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.jasminb.jsonapi.exceptions;

public class UnknownRelationshipException extends RuntimeException {

public UnknownRelationshipException(String message, Throwable cause) {
super(message, cause);
}

public UnknownRelationshipException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ public void testEnableAllowUnknownInclusions() throws IOException {
@Test
public void testEnableAllowUnknownInclusionsTwo() throws IOException {
converter.enableDeserializationOption(DeserializationFeature.ALLOW_UNKNOWN_INCLUSIONS);
converter.enableDeserializationOption(DeserializationFeature.ALLOW_UNKNOWN_RELATIONSHIPS);

InputStream rawData = IOUtils.getResource("included_fail.json");
converter.readDocument(rawData, City.class).get();
Expand Down