Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject

import java.time.Instant

@MicronautTest
class JacksonBasicSerdeSpec extends AbstractBasicSerdeSpec {

Expand Down Expand Up @@ -76,4 +78,13 @@
obj == result
json == """{"fooBar":"id2","abcXyz":2,"nested":{"id":2}}"""
}

def "record with multiple constructors"() {
when:
def myRecord = new MyRecord("value", 10, Date.from(Instant.now().minusSeconds(60)))
def str = jsonMapper.writeValueAsString(myRecord)
def result = jsonMapper.readValue(str, MyRecord)
then:
myRecord == result

Check failure on line 88 in serde-jackson/src/test/groovy/io/micronaut/serde/jackson/JacksonBasicSerdeSpec.groovy

View workflow job for this annotation

GitHub Actions / Java CI / Test Report (21)

JacksonBasicSerdeSpec.record with multiple constructors

Condition not satisfied: myRecord == result | | | | | MyRecord[fooBar=value, abcXyz=10, theDate=Mon Dec 01 09:28:22 UTC 2025, otherStr=random] | false MyRecord[fooBar=value, abcXyz=10, theDate=Mon Dec 01 09:27:22 UTC 2025, otherStr=random]
Raw output
Condition not satisfied:

myRecord == result
|        |  |
|        |  MyRecord[fooBar=value, abcXyz=10, theDate=Mon Dec 01 09:28:22 UTC 2025, otherStr=random]
|        false
MyRecord[fooBar=value, abcXyz=10, theDate=Mon Dec 01 09:27:22 UTC 2025, otherStr=random]

	at io.micronaut.serde.jackson.JacksonBasicSerdeSpec.record with multiple constructors(JacksonBasicSerdeSpec.groovy:88)

Check failure on line 88 in serde-jackson/src/test/groovy/io/micronaut/serde/jackson/JacksonBasicSerdeSpec.groovy

View workflow job for this annotation

GitHub Actions / Java CI / Test Report (17)

JacksonBasicSerdeSpec.record with multiple constructors

Condition not satisfied: myRecord == result | | | | | MyRecord[fooBar=value, abcXyz=10, theDate=Mon Dec 01 09:28:34 UTC 2025, otherStr=random] | false MyRecord[fooBar=value, abcXyz=10, theDate=Mon Dec 01 09:27:34 UTC 2025, otherStr=random]
Raw output
Condition not satisfied:

myRecord == result
|        |  |
|        |  MyRecord[fooBar=value, abcXyz=10, theDate=Mon Dec 01 09:28:34 UTC 2025, otherStr=random]
|        false
MyRecord[fooBar=value, abcXyz=10, theDate=Mon Dec 01 09:27:34 UTC 2025, otherStr=random]

	at io.micronaut.serde.jackson.JacksonBasicSerdeSpec.record with multiple constructors(JacksonBasicSerdeSpec.groovy:88)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.micronaut.serde.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.serde.annotation.Serdeable;

import java.time.Instant;
import java.util.Date;

@Serdeable
record MyRecord(String fooBar, int abcXyz, @Nullable Date theDate, @Nullable String otherStr) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding @JsonCreator here resolves the issue btw.

// TODO: Adding this annotation fixes issue
// @JsonCreator
MyRecord {
if (theDate == null) {
theDate = Date.from(Instant.now());
}
}

public MyRecord(String fooBar, int abcXyz, Date theDate) {
this(fooBar, abcXyz, theDate, "random");
}

public MyRecord(String fooBar, int abcXyz) {
this(fooBar, abcXyz, Date.from(Instant.now()), "random");
}
}
Loading