Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix @JsonProperty READ_ONLY/WRITE_ONLY, integrate @JsonWrapper TCK #669

Merged
merged 2 commits into from
Nov 14, 2023
Merged
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
@@ -1,10 +1,9 @@
package io.micronaut.serde.jackson

import io.micronaut.core.beans.exceptions.IntrospectionException
import spock.lang.PendingFeature

import spock.lang.Unroll

abstract class JsonPropertySpec extends JsonCompileSpec {
class JsonPropertySpec extends JsonCompileSpec {

void "test JsonProperty on private methods"() {
when:
Expand Down Expand Up @@ -125,42 +124,6 @@ class Test {
URL | 'ws://junk'
}

@PendingFeature
void "test required primitive field"() {
given:
def ctx = buildContext('test.Test', """
package test;

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

@Serdeable
class Test {
@JsonProperty(required = true)
private int value;

@JsonCreator
Test(@JsonProperty("value") int value) {
this.value = value;
}

public int getValue() {
return value;
}
}
""")

when:
def bean = jsonMapper.readValue('{}', argumentOf(ctx, 'test.Test'))
then:
def e = thrown(Exception)
e.message.contains("Unable to deserialize type [test.Test]. Required constructor parameter [int value] at index [0] is not present or is null in the supplied data")

cleanup:
ctx.close()
}

void "test optional by default primitive field"() {

given:
Expand Down Expand Up @@ -218,8 +181,7 @@ class Test {
""")

when:
def bean =
jsonMapper.readValue('{}', argumentOf(ctx, 'test.Test'))
def bean = jsonMapper.readValue('{}', argumentOf(ctx, 'test.Test'))
then:
bean.value == value

Expand Down Expand Up @@ -272,135 +234,4 @@ record Test(
URL | 'ws://junk'
}

@PendingFeature
void "test @JsonProperty on field"() {
given:
def context = buildContext('test.Test', """
package test;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.micronaut.serde.annotation.Serdeable;

@Serdeable
class Test {
@JsonProperty(value = "other", defaultValue = "default")
private String value;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private boolean ignored;
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}

public void setIgnored(boolean b) {
this.ignored = b;
}

public boolean isIgnored() {
return ignored;
}
}
""", [value: 'test'])
when:
def result = writeJson(jsonMapper, beanUnderTest)

then:
result == '{"other":"test"}'

when:
def bean = jsonMapper.readValue('{"ignored":true}', argumentOf(context, 'test.Test'))
then:
bean.ignored == true
bean.value == 'default'

cleanup:
context.close()

}

@PendingFeature
void "test @JsonProperty records"() {
given:
def context = buildContext("""
package test;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.micronaut.serde.annotation.Serdeable;

@Serdeable
record Test(
@JsonProperty(value = "other", defaultValue = "default")
String value,
@JsonProperty(access = JsonProperty.Access.READ_ONLY, defaultValue = "false")
boolean ignored
) {}
""")
when:
def bean = newInstance(context, 'test.Test', "test", false)
def result = writeJson(jsonMapper, bean)

then:
result == '{"other":"test"}'

when:
bean = jsonMapper.readValue('{"ignored":true}', argumentOf(context, 'test.Test'))

then:
bean.ignored == true
bean.value == 'default'

when:
bean = jsonMapper.readValue('{}', argumentOf(context, 'test.Test'))

then:
bean.ignored == false
bean.value == 'default'

cleanup:
context.close()

}

@PendingFeature
void "test @JsonProperty records - invalid default value"() {
given:
def context = buildContext("""
package test;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.micronaut.serde.annotation.Serdeable;

@Serdeable(validate = false)
record Test(
@JsonProperty(value = "other", defaultValue = "default")
String value,
@JsonProperty(access = JsonProperty.Access.READ_ONLY, defaultValue = "junk")
int number
) {}
""")
when:
def bean = newInstance(context, 'test.Test', "test", 10)
def result = writeJson(jsonMapper, bean)

then:
result == '{"other":"test"}'

when:
bean = jsonMapper.readValue('{"ignored":true}', argumentOf(context, 'test.Test'))

then:
def e = thrown(IntrospectionException)
e.cause.message.contains("Constructor Argument [int number] of type [test.Test] defines an invalid default value")

cleanup:
context.close()

}


}
Loading
Loading