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 @@ -147,7 +147,8 @@ public Map<String, Object> fromCursor(String cursor) {

/**
* Customizes the {@link ObjectMapper} to use default typing that supports
* {@link Date}, {@link Calendar}, {@link UUID} and classes in {@code java.time}.
* {@link Date}, {@link Calendar}, {@link UUID}, {@link Number}
* {@link Enum} and classes in {@code java.time}.
*/
private static final class JacksonObjectMapperCustomizer {

Expand All @@ -159,6 +160,8 @@ static void customize(CodecConfigurer configurer) {
.allowIfSubType(Calendar.class)
.allowIfSubType(Date.class)
.allowIfSubType(UUID.class)
.allowIfSubType(Number.class)
.allowIfSubType(Enum.class)
.build();

JsonMapper mapper = JsonMapper.builder()
Expand All @@ -174,7 +177,8 @@ static void customize(CodecConfigurer configurer) {

/**
* Customizes the {@link ObjectMapper} to use default typing that supports
* {@link Date}, {@link Calendar}, {@link UUID} and classes in {@code java.time}.
* {@link Date}, {@link Calendar}, {@link UUID}, {@link Number},
* {@link Enum} and classes in {@code java.time}.
*/
@SuppressWarnings("removal")
private static final class Jackson2ObjectMapperCustomizer {
Expand All @@ -188,6 +192,8 @@ static void customize(CodecConfigurer configurer) {
.allowIfSubType(Calendar.class)
.allowIfSubType(Date.class)
.allowIfSubType(UUID.class)
.allowIfSubType(Number.class)
.allowIfSubType(Enum.class)
.build();

com.fasterxml.jackson.databind.ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.graphql.data.query;

import java.math.BigDecimal;
import java.math.MathContext;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
Expand Down Expand Up @@ -91,4 +93,58 @@ void toAndFromCursorWithUUID() {
assertThat(this.cursorStrategy.toCursor(keys)).isEqualTo(json);
assertThat(this.cursorStrategy.fromCursor(json)).isEqualTo(keys);
}

@Test
void toAndFromCursorWithNumber() {
Map<String, Object> keys = new LinkedHashMap<>();
keys.put("byteValue", (byte) 1);
keys.put("shortValue", (short) 2);
keys.put("intValue", 3);
keys.put("longValue", (long) 4);
keys.put("floatValue", (float) 5);
keys.put("doubleValue", (double) 6);
keys.put("bigDecimal", new BigDecimal("10000000000000000000.002"));

//language=JSON
String json = """
[
"java.util.LinkedHashMap",
{
"byteValue": ["java.lang.Byte", 1],
"shortValue": ["java.lang.Short", 2],
"intValue": 3,
"longValue": ["java.lang.Long", 4],
"floatValue": ["java.lang.Float", 5.0],
"doubleValue": 6.0,
"bigDecimal": ["java.math.BigDecimal", 10000000000000000000.002]
}
]
""".replaceAll("\\s+", "");

assertThat(this.cursorStrategy.toCursor(keys)).isEqualTo(json);
assertThat(this.cursorStrategy.fromCursor(json)).isEqualTo(keys);
}

@Test
void toAndFromCursorWithEnum() {
Map<String, Object> keys = new LinkedHashMap<>();
keys.put("enumValue", TestEnum.VALUE_1);

//language=JSON
String json = """
[
"java.util.LinkedHashMap",
{
"enumValue": ["org.springframework.graphql.data.query.JsonKeysetCursorStrategyTests$TestEnum", "VALUE_1"]
}
]
""".replaceAll("\\s+", "");

assertThat(this.cursorStrategy.toCursor(keys)).isEqualTo(json);
assertThat(this.cursorStrategy.fromCursor(json)).isEqualTo(keys);
}

enum TestEnum {
VALUE_1
}
}