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

Field marked as READ_ONLY in JsonUnwrapped should not be writable #529

Merged
merged 1 commit into from
Jul 24, 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
Expand Up @@ -297,6 +297,65 @@ class Name {
context.close()
}

void "test @JsonUnwrapped with readOnly field"() {
given:
def context = buildContext("""
package unwrapped;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.serde.annotation.Serdeable;

@Serdeable
@Introspected(accessKind = Introspected.AccessKind.FIELD)
class Parent {
public int age;
@JsonUnwrapped
public Name name;
}

@Serdeable
@Introspected(accessKind = Introspected.AccessKind.FIELD)
class Name {
public String first, last;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public String ssn;
}
""")

when:
def name = newInstance(context, 'unwrapped.Name', [first:"Fred", last:"Flinstone", ssn:"abc-123"])
def parent = newInstance(context, 'unwrapped.Parent', [age:10, name:name])

def result = writeJson(jsonMapper, parent)

then:
result == '{"age":10,"first":"Fred","last":"Flinstone"}'

when:
def read = jsonMapper.readValue(result, Argument.of(context.classLoader.loadClass('unwrapped.Parent')))

then:
read.age == 10
read.name.first == 'Fred'
read.name.last == "Flinstone"
read.name.ssn == null

when:
def jsonStr = '{"age":15,"first":"Barney","last":"Rubble","ssn":"def-789"}'
read = jsonMapper.readValue(jsonStr, Argument.of(context.classLoader.loadClass('unwrapped.Parent')))

then:
read.age == 15
read.name.first == 'Barney'
read.name.last == "Rubble"
read.name.ssn == 'def-789'

cleanup:
context.close()
}

@Requires({ jvm.isJava17Compatible() })
void "test @JsonUnwrapped records"() {
given:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ public int getOrder() {
argument.getAnnotationMetadata(),
unwrappedProperty.getAnnotationMetadata()
);
if (!combinedMetadata.booleanValue(SerdeConfig.class, SerdeConfig.IGNORED).orElse(false)) {
if (!combinedMetadata.booleanValue(SerdeConfig.class, SerdeConfig.IGNORED).orElse(false) &&
!combinedMetadata.booleanValue(SerdeConfig.class, SerdeConfig.READ_ONLY).orElse(false)) {
CustomSerProperty<T, Object> prop = new CustomSerProperty<>(SerBean.this, n,
unwrappedPropertyArgument,
combinedMetadata,
Expand Down