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

Correct JsonIgnoreProperties #627

Merged
merged 1 commit into from
Oct 30, 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 @@ -197,6 +197,12 @@
*/
@Internal
@interface SerIgnored {

/**
* Is it unknown ignored.
*/
String IGNORE_UNKNOWN = "ignoreUnknown";

/**
* Ignore handling meta annotation on type.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,4 +482,219 @@ interface MyInterface {

}

void "test ignoreUnknown=false on a record throws and exception"() {
given:
def context = buildContext('test.DeserializableRecord', """
package test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.micronaut.serde.annotation.Serdeable.Deserializable;

@Deserializable
@JsonIgnoreProperties
record DeserializableRecord(String value) {
}


""")
when:
Object deserialized = jsonMapper.readValue("""
{
"unknown": "abc",
"value": "xyz"
}""", typeUnderTest)

then:
def e = thrown(SerdeException)
e.message == 'Unknown property [unknown] encountered during deserialization of type: DeserializableRecord'

cleanup:
context.close()

}

void "test ignoreUnknown=false on a record throws and exception 2"() {
given:
def context = buildContext('test.DeserializableRecord', """
package test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.micronaut.serde.annotation.Serdeable.Deserializable;

@Deserializable
@JsonIgnoreProperties(ignoreUnknown = false)
record DeserializableRecord(String value) {
}


""")
when:
Object deserialized = jsonMapper.readValue("""
{
"unknown": "abc",
"value": "xyz"
}""", typeUnderTest)

then:
def e = thrown(SerdeException)
e.message == 'Unknown property [unknown] encountered during deserialization of type: DeserializableRecord'

cleanup:
context.close()

}

void "test ignoreUnknown=true on a record doesn't throws and exception"() {
given:
def context = buildContext('test.DeserializableRecord', """
package test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.micronaut.serde.annotation.Serdeable.Deserializable;

@Deserializable
@JsonIgnoreProperties(ignoreUnknown = true)
record DeserializableRecord(String value) {
}


""")
when:
Object deserialized = jsonMapper.readValue("""
{
"unknown": "abc",
"value": "xyz"
}""", typeUnderTest)

then:
deserialized.value == "xyz"

cleanup:
context.close()

}

void "test ignoreUnknown=false on a class throws and exception"() {
given:
def context = buildContext('test.DeserializableRecord', """
package test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.micronaut.serde.annotation.Serdeable.Deserializable;

@Deserializable
@JsonIgnoreProperties
class DeserializableRecord {

private String value;

public void setValue(String value) {
this.value = value;
}

public String getValue() {
return value;
}

}


""")
when:
Object deserialized = jsonMapper.readValue("""
{
"unknown": "abc",
"value": "xyz"
}""", typeUnderTest)

then:
def e = thrown(SerdeException)
e.message == 'Unknown property [unknown] encountered during deserialization of type: DeserializableRecord'

cleanup:
context.close()

}

void "test ignoreUnknown=false on a class throws and exception 2"() {
given:
def context = buildContext('test.DeserializableRecord', """
package test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.micronaut.serde.annotation.Serdeable.Deserializable;

@Deserializable
@JsonIgnoreProperties
class DeserializableRecord {

private String value;

public void setValue(String value) {
this.value = value;
}

public String getValue() {
return value;
}

}

""")
when:
Object deserialized = jsonMapper.readValue("""
{
"unknown": "abc",
"value": "xyz"
}""", typeUnderTest)

then:
def e = thrown(SerdeException)
e.message == 'Unknown property [unknown] encountered during deserialization of type: DeserializableRecord'

cleanup:
context.close()

}

void "test ignoreUnknown=true on a class doesn't throws and exception"() {
given:
def context = buildContext('test.DeserializableRecord', """
package test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.micronaut.serde.annotation.Serdeable.Deserializable;

@Deserializable
@JsonIgnoreProperties(ignoreUnknown = true)
class DeserializableRecord {

private String value;

public void setValue(String value) {
this.value = value;
}

public String getValue() {
return value;
}

}

""")
when:
Object deserialized = jsonMapper.readValue("""
{
"unknown": "abc",
"value": "xyz"
}""", typeUnderTest)

then:
deserialized.value == "xyz"

cleanup:
context.close()

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
package io.micronaut.serde.processor.jackson;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.AnnotationValueBuilder;
import io.micronaut.inject.annotation.NamedAnnotationMapper;
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.serde.config.annotation.SerdeConfig;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -31,11 +29,11 @@
public class JacksonIgnorePropertiesMapper implements NamedAnnotationMapper {
@Override
public List<AnnotationValue<?>> map(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
final AnnotationValueBuilder<?> builder = AnnotationValue.builder(SerdeConfig.SerIgnored.class);

return Collections.singletonList(
builder.members(annotation.getValues())
.build()
return List.of(
AnnotationValue.builder(SerdeConfig.SerIgnored.class)
.member(SerdeConfig.SerIgnored.IGNORE_UNKNOWN, annotation.booleanValue(SerdeConfig.SerIgnored.IGNORE_UNKNOWN).orElse(false))
.members(annotation.getValues())
.build()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.function.BiConsumer;

import static io.micronaut.serde.config.annotation.SerdeConfig.SerSubtyped.DiscriminatorValueKind.CLASS_NAME;
Expand Down Expand Up @@ -85,6 +88,8 @@ final class DeserBean<T> {
public final String wrapperProperty;
@Nullable
public final SubtypeInfo<T> subtypeInfo;
@Nullable
public final Set<String> ignoredProperties;

public final int creatorSize;
public final int injectPropertiesSize;
Expand Down Expand Up @@ -130,8 +135,16 @@ public DeserBean(DeserializationConfiguration deserializationConfiguration,
creatorSize = constructorArguments.length;
PropertyNamingStrategy entityPropertyNamingStrategy = getPropertyNamingStrategy(introspection, decoderContext, null);

this.ignoreUnknown = introspection.booleanValue(SerdeConfig.SerIgnored.class, "ignoreUnknown").orElse(deserializationConfiguration.isIgnoreUnknown());
String[] ignored = introspection.stringValues(SerdeConfig.SerIgnored.class);
if (ignored.length == 0) {
ignoredProperties = null;
} else {
ignoredProperties = new HashSet<>(Arrays.asList(ignored));
}
this.ignoreUnknown = introspection.booleanValue(SerdeConfig.SerIgnored.class, SerdeConfig.SerIgnored.IGNORE_UNKNOWN)
.orElse(deserializationConfiguration.isIgnoreUnknown());
final PropertiesBag.Builder<T> creatorPropertiesBuilder = new PropertiesBag.Builder<>(introspection, constructorArguments.length);

List<DerProperty<T, ?>> creatorUnwrapped = null;
AnySetter<Object> anySetterValue = null;
List<DerProperty<T, ?>> unwrappedProperties = null;
Expand Down Expand Up @@ -479,7 +492,7 @@ private void initializeInternal(Deserializer.DecoderContext decoderContext) thro
}

private boolean isSimpleBean() {
if (delegating || subtypeInfo != null || creatorParams != null || creatorUnwrapped != null || unwrappedProperties != null || anySetter != null) {
if (ignoredProperties != null || delegating || subtypeInfo != null || creatorParams != null || creatorUnwrapped != null || unwrappedProperties != null || anySetter != null) {
return false;
}
if (injectProperties != null) {
Expand All @@ -494,7 +507,7 @@ private boolean isSimpleBean() {
}

private boolean isRecordLikeBean() {
if (delegating || subtypeInfo != null || injectProperties != null || creatorUnwrapped != null || unwrappedProperties != null || anySetter != null) {
if (ignoredProperties != null || delegating || subtypeInfo != null || injectProperties != null || creatorUnwrapped != null || unwrappedProperties != null || anySetter != null) {
return false;
}
if (creatorParams != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argume
} else if (ignoreUnknown) {
objectDecoder.skipValue();
} else {
return new SerdeException("Unknown property [" + propertyName + "] encountered during deserialization of type: " + beanType);
throw new SerdeException("Unknown property [" + propertyName + "] encountered during deserialization of type: " + beanType);
}
}
if (!allConsumed) {
Expand Down
Loading
Loading