generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #895 from micronaut-projects/2.10.x
Merge 2.10.x
- Loading branch information
Showing
20 changed files
with
537 additions
and
77 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
doc-examples/example-kotlin-ksp/src/main/kotlin/example/NonNullDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package example | ||
|
||
import io.micronaut.serde.annotation.Serdeable | ||
|
||
@Serdeable | ||
data class NonNullDto( | ||
val longField: Long, | ||
) |
9 changes: 9 additions & 0 deletions
9
doc-examples/example-kotlin-ksp/src/main/kotlin/example/NullDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
package example | ||
|
||
import io.micronaut.serde.annotation.Serdeable | ||
|
||
@Serdeable | ||
data class NullDto( | ||
val longField: Long? = null | ||
) |
9 changes: 9 additions & 0 deletions
9
doc-examples/example-kotlin-ksp/src/main/kotlin/example/NullPropertyDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
package example | ||
|
||
import io.micronaut.serde.annotation.Serdeable | ||
|
||
@Serdeable | ||
class NullPropertyDto { | ||
var longField: Long? = null | ||
} |
50 changes: 50 additions & 0 deletions
50
doc-examples/example-kotlin-ksp/src/test/kotlin/example/SerdeNullableFailOnMissingTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package example | ||
|
||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.json.JsonMapper | ||
import io.micronaut.serde.exceptions.SerdeException | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
|
||
@Property(name = "micronaut.serde.deserialization.failOnNullForPrimitives", value = "true") | ||
@MicronautTest | ||
class SerdeNullableFailOnMissingTest { | ||
|
||
@Test | ||
fun testDefaultValue(objectMapper: JsonMapper) { | ||
val result = objectMapper.writeValueAsString(NullDto()) | ||
val bean = objectMapper.readValue(result, NullDto::class.java) | ||
Assertions.assertEquals(null, bean.longField) | ||
} | ||
|
||
@Test | ||
fun testNonNullValue(objectMapper: JsonMapper) { | ||
val e = Assertions.assertThrows(SerdeException::class.java) { | ||
objectMapper.readValue("{}", NonNullDto::class.java) | ||
} | ||
Assertions.assertEquals( | ||
"Unable to deserialize type [example.NonNullDto]. Required constructor parameter [long longField] at index [0] is not present or is null in the supplied data", | ||
e.message | ||
) | ||
} | ||
|
||
@Test | ||
fun testNonNullValue2(objectMapper: JsonMapper) { | ||
val e = Assertions.assertThrows(SerdeException::class.java) { | ||
objectMapper.readValue("{\"longField\": null}", NonNullDto::class.java) | ||
} | ||
e.printStackTrace(); | ||
Assertions.assertEquals( | ||
"Unable to deserialize type [example.NonNullDto]. Required constructor parameter [long longField] at index [0] is not present or is null in the supplied data", | ||
e.message | ||
) | ||
} | ||
|
||
@Test | ||
fun testNullPropertyValue(objectMapper: JsonMapper) { | ||
val bean = objectMapper.readValue("{}", NullPropertyDto::class.java) | ||
Assertions.assertEquals(null, bean.longField) | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
doc-examples/example-kotlin-ksp/src/test/kotlin/example/SerdeNullableTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package example | ||
|
||
import io.micronaut.json.JsonMapper | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
|
||
@MicronautTest | ||
class SerdeNullableTest { | ||
|
||
@Test | ||
fun testDefaultValue(objectMapper: JsonMapper) { | ||
val result = objectMapper.writeValueAsString(NullDto()) | ||
val bean = objectMapper.readValue(result, NullDto::class.java) | ||
Assertions.assertEquals(null, bean.longField) | ||
} | ||
|
||
@Test | ||
fun testNonNullValue(objectMapper: JsonMapper) { | ||
val bean = objectMapper.readValue("{}", NonNullDto::class.java) | ||
Assertions.assertEquals(0, bean.longField) | ||
} | ||
|
||
@Test | ||
fun testNonNullValue2(objectMapper: JsonMapper) { | ||
val bean = objectMapper.readValue("{\"longField\":null}", NonNullDto::class.java) | ||
Assertions.assertEquals(0, bean.longField) | ||
} | ||
|
||
@Test | ||
fun testNullPropertyValue(objectMapper: JsonMapper) { | ||
val bean = objectMapper.readValue("{}", NullPropertyDto::class.java) | ||
Assertions.assertEquals(null, bean.longField) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
serde-jackson/src/test/groovy/io/micronaut/serde/jackson/errors/WithNpe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.micronaut.serde.jackson.errors; | ||
|
||
import io.micronaut.serde.annotation.Serdeable; | ||
|
||
@Serdeable | ||
public class WithNpe { | ||
|
||
private final String someString; | ||
|
||
public WithNpe(String someString) { | ||
this.someString = someString; | ||
if (!"noNPE".equals(someString)) { | ||
throw new NullPointerException("Simulating NPE in constructor"); | ||
} | ||
} | ||
|
||
public String getSomeString() { | ||
if (true) { | ||
throw new NullPointerException("Simulating NPE in getter"); | ||
} | ||
return someString; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "WithNpeToString"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.