Skip to content

Commit

Permalink
Add tests for JsonInclude.Include.NON_NULL
Browse files Browse the repository at this point in the history
See: #2933
  • Loading branch information
sdelamo committed Mar 17, 2020
1 parent 1d72735 commit 1f8bed9
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,65 @@
package io.micronaut.jackson.modules

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonView
import com.fasterxml.jackson.databind.ObjectMapper
import io.micronaut.context.ApplicationContext
import io.micronaut.core.annotation.Introspected
import io.micronaut.http.hateoas.JsonError
import io.micronaut.jackson.JacksonConfiguration
import org.checkerframework.checker.nullness.qual.NonNull
import spock.lang.Issue
import spock.lang.Specification

import javax.annotation.Nullable
import javax.validation.constraints.NotBlank

class BeanIntrospectionModuleSpec extends Specification {

@Issue("https://github.com/micronaut-projects/micronaut-core/issues/2933")
void "Bean introspection works with JsonInclude.Include.NON_NULL"() {
given:
ApplicationContext ctx = ApplicationContext.run(
(JacksonConfiguration.PROPERTY_USE_BEAN_INTROSPECTION):true
)
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)

when:
Guide guide = new Guide()
guide.name = 'Bean Introspection Guide'
String json = objectMapper.writeValueAsString(guide)

then:
noExceptionThrown()
json == '{"name":"Bean Introspection Guide"}'

cleanup:
ctx.close()
}

@Issue("https://github.com/micronaut-projects/micronaut-core/issues/2933")
void "Bean introspection works with JsonInclude.Include.ALWAYS"() {
given:
ApplicationContext ctx = ApplicationContext.run(
(JacksonConfiguration.PROPERTY_USE_BEAN_INTROSPECTION):true
)
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)

when:
GuideWithNull guide = new GuideWithNull()
guide.name = 'Bean Introspection Guide'
String json = objectMapper.writeValueAsString(guide)

then:
noExceptionThrown()
json == '{"name":"Bean Introspection Guide","author":null}'

cleanup:
ctx.close()
}

void "test that introspected serialization works"() {
given:
ApplicationContext ctx = ApplicationContext.run(
Expand Down Expand Up @@ -106,6 +154,20 @@ class BeanIntrospectionModuleSpec extends Specification {
}
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@Introspected
static class Guide {
String name
String author
}

@JsonInclude(JsonInclude.Include.ALWAYS)
@Introspected
static class GuideWithNull {
String name
String author
}

@Introspected
static class Author {
String name
Expand Down

0 comments on commit 1f8bed9

Please sign in to comment.