-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/Github56.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,77 @@ | ||
package com.fasterxml.jackson.module.kotlin.test | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude | ||
import com.fasterxml.jackson.annotation.JsonUnwrapped | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException | ||
import com.fasterxml.jackson.module.kotlin.* | ||
import org.junit.Before | ||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
private data class TestGalleryWidget_BAD( | ||
val widgetReferenceId: String, | ||
@JsonUnwrapped var gallery: TestGallery | ||
) | ||
|
||
private data class TestGalleryWidget_GOOD(val widgetReferenceId: String) { | ||
@JsonUnwrapped lateinit var gallery: TestGallery | ||
} | ||
|
||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
private data class TestGallery( | ||
val id: String? = null, | ||
val headline: String? = null, | ||
val intro: String? = null, | ||
val role: String? = null, | ||
val images: List<TestImage>? = null | ||
) | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
private data class TestImage( | ||
val id: String? = null, | ||
val escenicId: String? = null, | ||
val caption: String? = null, | ||
val copyright: String? = null, | ||
val crops: Map<String, String>? = null | ||
) | ||
|
||
class Github56 { | ||
lateinit var mapper: ObjectMapper | ||
|
||
private val gallery = TestGallery( | ||
id = "id", | ||
headline = "headline", | ||
intro = "intro", | ||
role = "role", | ||
images = listOf( | ||
TestImage(id = "testImage1"), | ||
TestImage(id = "testImage2") | ||
) | ||
) | ||
val validJson = """ | ||
{"widgetReferenceId":"widgetReferenceId","id":"id","headline":"headline","intro":"intro","role":"role","images":[{"id":"testImage1"},{"id":"testImage2"}]} | ||
""".trim() | ||
|
||
@Before | ||
fun setUp() { | ||
mapper = jacksonObjectMapper() | ||
} | ||
|
||
@Test | ||
fun serializes() { | ||
val result = mapper.writeValueAsString(TestGalleryWidget_BAD("widgetReferenceId", gallery)) | ||
assertEquals(validJson, result) | ||
} | ||
|
||
@Test(expected = InvalidDefinitionException::class) | ||
fun deserializesWithError() { | ||
mapper.readValue<TestGalleryWidget_BAD>(validJson) | ||
} | ||
|
||
@Test | ||
fun deserializesCorrectly() { | ||
mapper.readValue<TestGalleryWidget_GOOD>(validJson) | ||
} | ||
} |