Skip to content

Commit 2f7c56d

Browse files
authored
test deserialization with Java Record (#645)
* add check for java records * Update build.sbt * add test * Update build.sbt * Update MRecord.java * Update build.sbt
1 parent 4fc0bd5 commit 2f7c56d

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

build.sbt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ scalaMajorVersion := {
5151
}
5252
}
5353

54+
val addJava17Tests: Boolean = compareVersions(System.getProperty("java.version"), "17.0.0") >= 0
55+
5456
mimaPreviousArtifacts := {
5557
if (scalaReleaseVersion.value > 2)
5658
Set.empty
@@ -99,6 +101,17 @@ Test / unmanagedSourceDirectories ++= {
99101
}
100102
}
101103

104+
Test / unmanagedSourceDirectories ++= {
105+
if (addJava17Tests && scalaReleaseVersion.value == 2 && scalaMajorVersion.value >= 13) {
106+
Seq(
107+
(LocalRootProject / baseDirectory).value / "src" / "test" / "java-17",
108+
(LocalRootProject / baseDirectory).value / "src" / "test" / "scala-jdk-17",
109+
)
110+
} else {
111+
Seq.empty
112+
}
113+
}
114+
102115
libraryDependencies ++= Seq(
103116
"com.fasterxml.jackson.core" % "jackson-core" % jacksonVersion,
104117
"com.fasterxml.jackson.core" % "jackson-annotations" % jacksonVersion,
@@ -227,3 +240,23 @@ mimaBinaryIssueFilters ++= Seq(
227240
ProblemFilters.exclude[DirectMissingMethodProblem]("com.fasterxml.jackson.module.scala.introspect.ScalaAnnotationIntrospector.findSerializationInclusionForContent"),
228241
ProblemFilters.exclude[DirectMissingMethodProblem]("com.fasterxml.jackson.module.scala.introspect.ScalaAnnotationIntrospector.findSerializationInclusion")
229242
)
243+
244+
def compareVersions(version1: String, version2: String): Int = {
245+
var comparisonResult = 0
246+
val version1Splits = version1.split("\\.")
247+
val version2Splits = version2.split("\\.")
248+
val maxLengthOfVersionSplits = Math.max(version1Splits.length, version2Splits.length)
249+
var i = 0
250+
while (comparisonResult == 0 && i < maxLengthOfVersionSplits) {
251+
val v1 = if (i < version1Splits.length) version1Splits(i).toInt
252+
else 0
253+
val v2 = if (i < version2Splits.length) version2Splits(i).toInt
254+
else 0
255+
val compare = v1.compareTo(v2)
256+
if (compare != 0) {
257+
comparisonResult = compare
258+
}
259+
i += 1
260+
}
261+
comparisonResult
262+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fasterxml.jackson.module.scala.deser;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
6+
public record MRecord(String name, String domain) {
7+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
8+
public static MRecord fromString(String uri) {
9+
String[] parts = uri.split("@");
10+
if (parts.length > 2 || parts.length == 0) {
11+
throw new RuntimeException(String.format("Invalid SIP URI: %s", uri));
12+
}
13+
14+
String[] usernameParts = parts[0].split("\\.");
15+
return new MRecord(usernameParts[usernameParts.length - 1], parts.length == 2 ? parts[1] : null);
16+
}
17+
18+
@Override
19+
@JsonValue
20+
public String toString() {
21+
return String.format("%s@%s", name, domain);
22+
}
23+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.fasterxml.jackson.module.scala.deser;
2+
3+
public record MRecordWrapper(MRecord value) {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.fasterxml.jackson.module.scala.deser
2+
3+
import com.fasterxml.jackson.module.scala.DefaultScalaModule
4+
5+
class RecordTest extends DeserializerTest {
6+
lazy val module: DefaultScalaModule.type = DefaultScalaModule
7+
8+
"An ObjectMapper with DefaultScalaModule" should "not affect Java Record deserialization" in {
9+
val json = "{\"value\": \"a@b\"}"
10+
val mapper = newMapper
11+
val testVal = mapper.readValue(json, classOf[MRecordWrapper])
12+
testVal.value shouldEqual new MRecord("a", "b")
13+
}
14+
}

0 commit comments

Comments
 (0)