Skip to content

Commit 503d499

Browse files
committed
test: add RawFormatModuleTest for configurable default format
fix: simplify AnnotationIntrospector to only override when @IdFormat is present, allowing module-level default format to take effect
1 parent e171c53 commit 503d499

3 files changed

Lines changed: 139 additions & 18 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.devskiller.friendly_id.spring;
2+
3+
import java.util.UUID;
4+
5+
import org.junit.jupiter.api.Test;
6+
import tools.jackson.databind.json.JsonMapper;
7+
8+
import com.devskiller.friendly_id.FriendlyIdFormat;
9+
import com.devskiller.friendly_id.FriendlyIds;
10+
import com.devskiller.friendly_id.jackson.FriendlyIdModule;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
class RawFormatModuleTest {
15+
16+
private final UUID uuid = UUID.fromString("f088ce5b-9279-4cc3-946a-c15ad740dd6d");
17+
18+
private final JsonMapper mapper = JsonMapper.builder()
19+
.addModule(new FriendlyIdModule(FriendlyIdFormat.RAW))
20+
.build();
21+
22+
@Test
23+
void shouldSerializeUuidInStandardFormat() {
24+
String json = mapper.writeValueAsString(uuid);
25+
26+
assertThat(json).isEqualTo("\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\"");
27+
}
28+
29+
@Test
30+
void shouldDeserializeStandardUuid() {
31+
UUID result = mapper.readValue("\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\"", UUID.class);
32+
33+
assertThat(result).isEqualTo(uuid);
34+
}
35+
36+
@Test
37+
void shouldDeserializeFriendlyIdWhenModuleIsRaw() {
38+
String friendlyId = FriendlyIds.toFriendlyId(uuid);
39+
40+
UUID result = mapper.readValue("\"" + friendlyId + "\"", UUID.class);
41+
42+
assertThat(result).isEqualTo(uuid);
43+
}
44+
45+
@Test
46+
void shouldRespectPerFieldOverrideWhenModuleIsRaw() {
47+
var foo = new Foo(uuid, uuid);
48+
49+
String json = mapper.writeValueAsString(foo);
50+
51+
// rawUuid has @IdFormat(RAW) -> standard format
52+
assertThat(json).contains("\"rawUuid\":\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\"");
53+
// friendlyId has no annotation, module default is RAW -> standard format
54+
assertThat(json).contains("\"friendlyId\":\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\"");
55+
}
56+
57+
@Test
58+
void shouldSerializeFriendlyFormatWithUrl62Override() {
59+
var mapper = JsonMapper.builder()
60+
.addModule(new FriendlyIdModule(FriendlyIdFormat.RAW))
61+
.build();
62+
63+
// Foo: rawUuid=@IdFormat(RAW), friendlyId=no annotation (module default RAW)
64+
// Both should be standard UUID when module is RAW
65+
var foo = new Foo(uuid, uuid);
66+
String json = mapper.writeValueAsString(foo);
67+
68+
assertThat(json).contains("\"rawUuid\":\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\"");
69+
assertThat(json).contains("\"friendlyId\":\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\"");
70+
}
71+
}

friendly-id-jackson2-datatype/src/main/java/com/devskiller/friendly_id/jackson2/FriendlyIdAnnotationIntrospector.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,23 @@ public class FriendlyIdAnnotationIntrospector extends JacksonAnnotationIntrospec
1919
@Override
2020
public Object findSerializer(Annotated annotatedMethod) {
2121
IdFormat annotation = _findAnnotation(annotatedMethod, IdFormat.class);
22-
if (annotatedMethod.getRawType() == UUID.class) {
23-
if (annotation != null) {
24-
return switch (annotation.value()) {
25-
case RAW -> UUIDSerializer.class;
26-
case URL62 -> FriendlyIdSerializer.class;
27-
};
28-
}
29-
return FriendlyIdSerializer.class;
30-
} else {
31-
return null;
22+
if (annotatedMethod.getRawType() == UUID.class && annotation != null) {
23+
return switch (annotation.value()) {
24+
case RAW -> UUIDSerializer.class;
25+
case URL62 -> FriendlyIdSerializer.class;
26+
};
3227
}
28+
return null;
3329
}
3430

3531
@Override
3632
public Object findDeserializer(Annotated annotatedMethod) {
3733
var annotation = _findAnnotation(annotatedMethod, IdFormat.class);
38-
if (rawDeserializationType(annotatedMethod) == UUID.class) {
39-
if (annotation != null) {
40-
return switch (annotation.value()) {
41-
case RAW -> UUIDDeserializer.class;
42-
case URL62 -> FriendlyIdDeserializer.class;
43-
};
44-
}
45-
return FriendlyIdDeserializer.class;
34+
if (rawDeserializationType(annotatedMethod) == UUID.class && annotation != null) {
35+
return switch (annotation.value()) {
36+
case RAW -> UUIDDeserializer.class;
37+
case URL62 -> FriendlyIdDeserializer.class;
38+
};
4639
}
4740
return null;
4841
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.devskiller.friendly_id.spring;
2+
3+
import java.util.UUID;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import org.junit.jupiter.api.Test;
7+
8+
import com.devskiller.friendly_id.FriendlyIdFormat;
9+
import com.devskiller.friendly_id.FriendlyIds;
10+
import com.devskiller.friendly_id.jackson2.FriendlyIdJackson2Module;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
class RawFormatModuleTest {
15+
16+
private final UUID uuid = UUID.fromString("f088ce5b-9279-4cc3-946a-c15ad740dd6d");
17+
18+
private final ObjectMapper mapper = new ObjectMapper()
19+
.registerModule(new FriendlyIdJackson2Module(FriendlyIdFormat.RAW));
20+
21+
@Test
22+
void shouldSerializeUuidInStandardFormat() throws Exception {
23+
String json = mapper.writeValueAsString(uuid);
24+
25+
assertThat(json).isEqualTo("\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\"");
26+
}
27+
28+
@Test
29+
void shouldDeserializeStandardUuid() throws Exception {
30+
UUID result = mapper.readValue("\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\"", UUID.class);
31+
32+
assertThat(result).isEqualTo(uuid);
33+
}
34+
35+
@Test
36+
void shouldDeserializeFriendlyIdWhenModuleIsRaw() throws Exception {
37+
String friendlyId = FriendlyIds.toFriendlyId(uuid);
38+
39+
UUID result = mapper.readValue("\"" + friendlyId + "\"", UUID.class);
40+
41+
assertThat(result).isEqualTo(uuid);
42+
}
43+
44+
@Test
45+
void shouldRespectPerFieldAnnotationWhenModuleIsRaw() throws Exception {
46+
Foo foo = new Foo();
47+
foo.setRawUuid(uuid);
48+
foo.setFriendlyId(uuid);
49+
50+
String json = mapper.writeValueAsString(foo);
51+
52+
// rawUuid has @IdFormat(RAW) -> standard format
53+
assertThat(json).contains("\"rawUuid\":\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\"");
54+
// friendlyId has no annotation, module default is RAW -> standard format
55+
assertThat(json).contains("\"friendlyId\":\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\"");
56+
}
57+
}

0 commit comments

Comments
 (0)