Skip to content

Commit 15a7412

Browse files
heruanbclozel
authored andcommitted
Add UUID support to JsonKeysetCursorStrategy
Closes gh-1328 Signed-off-by: Giovanni Lovato <giovanni@lova.to> [brian.clozel@broadcom.com: add documentation] Signed-off-by: Brian Clozel <brian.clozel@broadcom.com>
1 parent 6d05641 commit 15a7412

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

spring-graphql-docs/modules/ROOT/pages/data.adoc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,10 @@ allowed types. For example:
441441
.allowIfSubType(ZonedDateTime.class)
442442
.build();
443443
444-
ObjectMapper mapper = new ObjectMapper();
445-
mapper.activateDefaultTyping(validator, ObjectMapper.DefaultTyping.NON_FINAL);
444+
JsonMapper mapper = JsonMapper.builder()
445+
.activateDefaultTyping(validator, DefaultTyping.NON_FINAL)
446+
.enable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
447+
.build();
446448
----
447449

448450
You can then create `JsonKeysetCursorStrategy`:
@@ -460,7 +462,7 @@ You can then create `JsonKeysetCursorStrategy`:
460462

461463
By default, if `JsonKeysetCursorStrategy` is created without a `CodecConfigurer` and the
462464
Jackson library is on the classpath, customizations like the above are applied for
463-
`Date`, `Calendar`, and any type from `java.time`.
465+
`Date`, `Calendar`, `UUID` and any type from `java.time`.
464466

465467

466468

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collections;
2222
import java.util.Date;
2323
import java.util.Map;
24+
import java.util.UUID;
2425

2526
import tools.jackson.databind.DefaultTyping;
2627
import tools.jackson.databind.ObjectMapper;
@@ -146,7 +147,7 @@ public Map<String, Object> fromCursor(String cursor) {
146147

147148
/**
148149
* Customizes the {@link ObjectMapper} to use default typing that supports
149-
* {@link Date}, {@link Calendar}, and classes in {@code java.time}.
150+
* {@link Date}, {@link Calendar}, {@link UUID} and classes in {@code java.time}.
150151
*/
151152
private static final class JacksonObjectMapperCustomizer {
152153

@@ -157,6 +158,7 @@ static void customize(CodecConfigurer configurer) {
157158
.allowIfSubType("java.time.")
158159
.allowIfSubType(Calendar.class)
159160
.allowIfSubType(Date.class)
161+
.allowIfSubType(UUID.class)
160162
.build();
161163

162164
JsonMapper mapper = JsonMapper.builder()
@@ -172,7 +174,7 @@ static void customize(CodecConfigurer configurer) {
172174

173175
/**
174176
* Customizes the {@link ObjectMapper} to use default typing that supports
175-
* {@link Date}, {@link Calendar}, and classes in {@code java.time}.
177+
* {@link Date}, {@link Calendar}, {@link UUID} and classes in {@code java.time}.
176178
*/
177179
@SuppressWarnings("removal")
178180
private static final class Jackson2ObjectMapperCustomizer {
@@ -185,6 +187,7 @@ static void customize(CodecConfigurer configurer) {
185187
.allowIfSubType("java.time.")
186188
.allowIfSubType(Calendar.class)
187189
.allowIfSubType(Date.class)
190+
.allowIfSubType(UUID.class)
188191
.build();
189192

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

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Date;
2424
import java.util.LinkedHashMap;
2525
import java.util.Map;
26+
import java.util.UUID;
2627

2728
import org.junit.jupiter.api.Test;
2829

@@ -78,4 +79,16 @@ void toAndFromCursorWithZonedDateTime() {
7879
assertThat(this.cursorStrategy.fromCursor(json)).isEqualTo(keys);
7980
}
8081

82+
@Test
83+
void toAndFromCursorWithUUID() {
84+
85+
UUID uuid = UUID.randomUUID();
86+
87+
Map<String, Object> keys = new LinkedHashMap<>();
88+
keys.put("uuid", uuid);
89+
String json = "[\"java.util.LinkedHashMap\",{\"uuid\":[\"java.util.UUID\",\"" + uuid + "\"]}]";
90+
91+
assertThat(this.cursorStrategy.toCursor(keys)).isEqualTo(json);
92+
assertThat(this.cursorStrategy.fromCursor(json)).isEqualTo(keys);
93+
}
8194
}

0 commit comments

Comments
 (0)