Skip to content

Commit 7462b6a

Browse files
committed
Add enum and number values support to JsonKeysetCursorStrategy
Signed-off-by: Ilya Bakaev <[email protected]>
1 parent fdfabbb commit 7462b6a

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/query/JsonKeysetCursorStrategy.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ public Map<String, Object> fromCursor(String cursor) {
147147

148148
/**
149149
* Customizes the {@link ObjectMapper} to use default typing that supports
150-
* {@link Date}, {@link Calendar}, {@link UUID} and classes in {@code java.time}.
150+
* {@link Date}, {@link Calendar}, {@link UUID}, {@link Number}
151+
* {@link Enum} and classes in {@code java.time}.
151152
*/
152153
private static final class JacksonObjectMapperCustomizer {
153154

@@ -159,6 +160,8 @@ static void customize(CodecConfigurer configurer) {
159160
.allowIfSubType(Calendar.class)
160161
.allowIfSubType(Date.class)
161162
.allowIfSubType(UUID.class)
163+
.allowIfSubType(Number.class)
164+
.allowIfSubType(Enum.class)
162165
.build();
163166

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

175178
/**
176179
* Customizes the {@link ObjectMapper} to use default typing that supports
177-
* {@link Date}, {@link Calendar}, {@link UUID} and classes in {@code java.time}.
180+
* {@link Date}, {@link Calendar}, {@link UUID}, {@link Number},
181+
* {@link Enum} and classes in {@code java.time}.
178182
*/
179183
@SuppressWarnings("removal")
180184
private static final class Jackson2ObjectMapperCustomizer {
@@ -188,6 +192,8 @@ static void customize(CodecConfigurer configurer) {
188192
.allowIfSubType(Calendar.class)
189193
.allowIfSubType(Date.class)
190194
.allowIfSubType(UUID.class)
195+
.allowIfSubType(Number.class)
196+
.allowIfSubType(Enum.class)
191197
.build();
192198

193199
com.fasterxml.jackson.databind.ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().build();

spring-graphql/src/test/java/org/springframework/graphql/data/query/JsonKeysetCursorStrategyTests.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.graphql.data.query;
1818

19+
import java.math.BigDecimal;
20+
import java.math.MathContext;
1921
import java.time.LocalDateTime;
2022
import java.time.Month;
2123
import java.time.ZoneId;
@@ -91,4 +93,58 @@ void toAndFromCursorWithUUID() {
9193
assertThat(this.cursorStrategy.toCursor(keys)).isEqualTo(json);
9294
assertThat(this.cursorStrategy.fromCursor(json)).isEqualTo(keys);
9395
}
96+
97+
@Test
98+
void toAndFromCursorWithNumber() {
99+
Map<String, Object> keys = new LinkedHashMap<>();
100+
keys.put("byteValue", (byte) 1);
101+
keys.put("shortValue", (short) 2);
102+
keys.put("intValue", 3);
103+
keys.put("longValue", (long) 4);
104+
keys.put("floatValue", (float) 5);
105+
keys.put("doubleValue", (double) 6);
106+
keys.put("bigDecimal", new BigDecimal("10000000000000000000.002"));
107+
108+
//language=JSON
109+
String json = """
110+
[
111+
"java.util.LinkedHashMap",
112+
{
113+
"byteValue": ["java.lang.Byte", 1],
114+
"shortValue": ["java.lang.Short", 2],
115+
"intValue": 3,
116+
"longValue": ["java.lang.Long", 4],
117+
"floatValue": ["java.lang.Float", 5.0],
118+
"doubleValue": 6.0,
119+
"bigDecimal": ["java.math.BigDecimal", 10000000000000000000.002]
120+
}
121+
]
122+
""".replaceAll("\\s+", "");
123+
124+
assertThat(this.cursorStrategy.toCursor(keys)).isEqualTo(json);
125+
assertThat(this.cursorStrategy.fromCursor(json)).isEqualTo(keys);
126+
}
127+
128+
@Test
129+
void toAndFromCursorWithEnum() {
130+
Map<String, Object> keys = new LinkedHashMap<>();
131+
keys.put("enumValue", TestEnum.VALUE_1);
132+
133+
//language=JSON
134+
String json = """
135+
[
136+
"java.util.LinkedHashMap",
137+
{
138+
"enumValue": ["org.springframework.graphql.data.query.JsonKeysetCursorStrategyTests$TestEnum", "VALUE_1"]
139+
}
140+
]
141+
""".replaceAll("\\s+", "");
142+
143+
assertThat(this.cursorStrategy.toCursor(keys)).isEqualTo(json);
144+
assertThat(this.cursorStrategy.fromCursor(json)).isEqualTo(keys);
145+
}
146+
147+
enum TestEnum {
148+
VALUE_1
149+
}
94150
}

0 commit comments

Comments
 (0)