Skip to content

Commit 9cb898e

Browse files
committed
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 2ba6e76 commit 9cb898e

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

build.sbt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ publishTo := {
2626
}
2727
ThisBuild / publishMavenStyle := true
2828

29-
3029
autoAPIMappings := true
3130

3231
apiMappings ++= {
@@ -62,6 +61,8 @@ scalaMajorVersion := {
6261
}
6362
}
6463

64+
val addJava17Tests: Boolean = compareVersions(System.getProperty("java.version"), "17.0.0") >= 0
65+
6566
scalacOptions ++= {
6667
val additionalSettings =
6768
if (scalaReleaseVersion.value == 2 && scalaMajorVersion.value <= 12) {
@@ -103,6 +104,17 @@ Test / unmanagedSourceDirectories ++= {
103104
}
104105
}
105106

107+
Test / unmanagedSourceDirectories ++= {
108+
if (addJava17Tests && scalaReleaseVersion.value == 2 && scalaMajorVersion.value >= 13) {
109+
Seq(
110+
(LocalRootProject / baseDirectory).value / "src" / "test" / "java-17",
111+
(LocalRootProject / baseDirectory).value / "src" / "test" / "scala-jdk-17",
112+
)
113+
} else {
114+
Seq.empty
115+
}
116+
}
117+
106118
libraryDependencies ++= Seq(
107119
"tools.jackson.core" % "jackson-core" % jacksonVersion changing(),
108120
"tools.jackson.core" % "jackson-databind" % jacksonVersion changing(),
@@ -152,3 +164,23 @@ ThisBuild / githubWorkflowPublish := Seq(
152164
enablePlugins(SiteScaladocPlugin)
153165
//enablePlugins(GhpagesPlugin)
154166
git.remoteRepo := "git@github.com:FasterXML/jackson-module-scala.git"
167+
168+
def compareVersions(version1: String, version2: String): Int = {
169+
var comparisonResult = 0
170+
val version1Splits = version1.split("\\.")
171+
val version2Splits = version2.split("\\.")
172+
val maxLengthOfVersionSplits = Math.max(version1Splits.length, version2Splits.length)
173+
var i = 0
174+
while (comparisonResult == 0 && i < maxLengthOfVersionSplits) {
175+
val v1 = if (i < version1Splits.length) version1Splits(i).toInt
176+
else 0
177+
val v2 = if (i < version2Splits.length) version2Splits(i).toInt
178+
else 0
179+
val compare = v1.compareTo(v2)
180+
if (compare != 0) {
181+
comparisonResult = compare
182+
}
183+
i += 1
184+
}
185+
comparisonResult
186+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package tools.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 tools.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 tools.jackson.module.scala.deser
2+
3+
import tools.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)