diff --git a/src/main/java/com/fauna/codec/codecs/PageCodec.java b/src/main/java/com/fauna/codec/codecs/PageCodec.java index 72d72060..9d2c39e9 100644 --- a/src/main/java/com/fauna/codec/codecs/PageCodec.java +++ b/src/main/java/com/fauna/codec/codecs/PageCodec.java @@ -8,6 +8,7 @@ import com.fauna.exception.CodecException; import com.fauna.types.Page; +import java.util.ArrayList; import java.util.List; /** @@ -84,10 +85,19 @@ public Class getCodecClass() { private L decodePage(final UTF8FaunaParser parser, final FaunaTokenType endToken) throws CodecException { + + parser.read(); + if (parser.getCurrentTokenType() == FaunaTokenType.STRING) { + return handleUnmaterialized(parser, endToken); + } else { + return handleMaterialized(parser, endToken); + } + } + + private L handleMaterialized(final UTF8FaunaParser parser, final FaunaTokenType endToken) { List data = null; String after = null; - - while (parser.read() && parser.getCurrentTokenType() != endToken) { + do { String fieldName = parser.getValueAsString(); parser.read(); @@ -101,16 +111,23 @@ private L decodePage(final UTF8FaunaParser parser, final FaunaTokenType endToken default: break; } - } + } while (parser.read() && parser.getCurrentTokenType() != endToken); - if (data == null) { - throw new CodecException( - "No page data found while deserializing into Page<>"); + //noinspection unchecked + return (L) new Page<>(data, after); + } + + private L handleUnmaterialized(final UTF8FaunaParser parser, final FaunaTokenType endToken) { + var after = parser.getValueAsString(); + parser.read(); + + if (parser.getCurrentTokenType() != endToken) { + throw new CodecException(unexpectedTokenExceptionMessage(parser.getCurrentTokenType())); } - @SuppressWarnings("unchecked") - L res = (L) new Page<>(data, after); - return res; + //noinspection unchecked + return (L) new Page<>(new ArrayList<>(), after); + } private L wrapInPage(final UTF8FaunaParser parser) throws CodecException { diff --git a/src/test/java/com/fauna/codec/codecs/PageCodecTest.java b/src/test/java/com/fauna/codec/codecs/PageCodecTest.java index 8851051a..2750ec76 100644 --- a/src/test/java/com/fauna/codec/codecs/PageCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/PageCodecTest.java @@ -4,8 +4,10 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; +import com.fauna.codec.Helpers; import com.fauna.exception.CodecException; import com.fauna.types.Page; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -16,6 +18,8 @@ import java.util.List; import java.util.stream.Stream; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class PageCodecTest extends TestBase { public static final String PAGE_WIRE = @@ -77,4 +81,15 @@ public void page_runUnsupportedTypeTestCases(String wire, FaunaType type) runCase(TestType.Decode, pageCodecOf(Object.class), wire, null, new CodecException(exMsg)); } + + @Test + public void page_decodeUnmaterializedSet() + { + var token = "aftertoken"; + var wire = "{\"@set\":\"" + token + "\"}"; + var codec = pageCodecOf(Object.class); + var decoded = Helpers.decode(codec, wire); + assertEquals(token, decoded.getAfter().get().getToken()); + assertEquals(0, decoded.getData().size()); + } }