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(model): Use SettableBeanProperty.Delegating for jackson delegation #6343

Merged
merged 4 commits into from
Sep 23, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#### Bugs
* Fix #6038: Support for Gradle configuration cache
* Fix #6214: Java generator does not recognize fields in CRDs other than metadata, spec, and status
* Fix #6342: UnmatchedFieldTypeModule prevents certain jackson features from working

#### Improvements
* Fix #5264: Remove deprecated `Config.errorMessages` field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@
package io.fabric8.kubernetes.model.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.NullValueProvider;
import com.fasterxml.jackson.databind.deser.SettableAnyProperty;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.ObjectIdInfo;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.function.BooleanSupplier;

/**
* This concrete sub-class encapsulates a {@link SettableBeanProperty} delegate that is always tried first.
* This concrete subclass encapsulates a {@link SettableBeanProperty} delegate that is always tried first.
*
* <p>
* A fall-back mechanism is implemented in the deserializeAndSet methods to allow field values that don't match the
* target type to be preserved in the anySetter method if exists.
*/
public class SettableBeanPropertyDelegate extends SettableBeanProperty {
public class SettableBeanPropertyDelegate extends SettableBeanProperty.Delegating {

private final SettableBeanProperty delegate;
private final SettableAnyProperty anySetter;
private final transient BooleanSupplier useAnySetter;

SettableBeanPropertyDelegate(SettableBeanProperty delegate, SettableAnyProperty anySetter, BooleanSupplier useAnySetter) {
super(delegate);
this.delegate = delegate;
this.anySetter = anySetter;
this.useAnySetter = useAnySetter;
}
Expand All @@ -54,64 +54,113 @@ public class SettableBeanPropertyDelegate extends SettableBeanProperty {
* {@inheritDoc}
*/
@Override
public SettableBeanProperty withValueDeserializer(JsonDeserializer<?> deser) {
return new SettableBeanPropertyDelegate(delegate.withValueDeserializer(deser), anySetter, useAnySetter);
protected SettableBeanProperty withDelegate(SettableBeanProperty d) {
return new SettableBeanPropertyDelegate(d, anySetter, useAnySetter);
}

/**
* {@inheritDoc}
*/
@Override
public SettableBeanProperty withName(PropertyName newName) {
return new SettableBeanPropertyDelegate(delegate.withName(newName), anySetter, useAnySetter);
public void markAsIgnorable() {
delegate.markAsIgnorable();
}

/**
* {@inheritDoc}
*/
@Override
public SettableBeanProperty withNullProvider(NullValueProvider nva) {
return new SettableBeanPropertyDelegate(delegate.withNullProvider(nva), anySetter, useAnySetter);
public boolean isIgnorable() {
return delegate.isIgnorable();
}

/**
* {@inheritDoc}
*/
@Override
public AnnotatedMember getMember() {
return delegate.getMember();
public void setViews(Class<?>[] views) {
delegate.setViews(views);
}

/**
* {@inheritDoc}
*/
@Override
public <A extends Annotation> A getAnnotation(Class<A> acls) {
return delegate.getAnnotation(acls);
public <A extends Annotation> A getContextAnnotation(Class<A> acls) {
return delegate.getContextAnnotation(acls);
}

/**
* {@inheritDoc}
*/
@Override
public void fixAccess(DeserializationConfig config) {
delegate.fixAccess(config);
public PropertyName getWrapperName() {
return delegate.getWrapperName();
}

/**
* {@inheritDoc}
*/
@Override
public void markAsIgnorable() {
delegate.markAsIgnorable();
public NullValueProvider getNullValueProvider() {
return delegate.getNullValueProvider();
}

/**
* {@inheritDoc}
*/
@Override
public boolean isIgnorable() {
return delegate.isIgnorable();
public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor, SerializerProvider provider)
throws JsonMappingException {
delegate.depositSchemaProperty(objectVisitor, provider);
}

/**
* {@inheritDoc}
*/
@Override
public JavaType getType() {
return delegate.getType();
}

/**
* {@inheritDoc}
*/
@Override
public PropertyName getFullName() {
return delegate.getFullName();
}

/**
* {@inheritDoc}
*/
@Override
public void setManagedReferenceName(String n) {
delegate.setManagedReferenceName(n);
}

/**
* {@inheritDoc}
*/
@Override
public SettableBeanProperty withSimpleName(String simpleName) {
return _with(delegate.withSimpleName(simpleName));
}

/**
* {@inheritDoc}
*/
@Override
public void setObjectIdInfo(ObjectIdInfo objectIdInfo) {
delegate.setObjectIdInfo(objectIdInfo);
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return delegate.toString();
}

/**
Expand Down Expand Up @@ -151,23 +200,7 @@ public Object deserializeSetAndReturn(JsonParser p, DeserializationContext ctxt,
} catch (MismatchedInputException ex) {
deserializeAndSet(p, ctxt, instance);
}
return null;
}

/**
* {@inheritDoc}
*/
@Override
public void set(Object instance, Object value) throws IOException {
delegate.set(instance, value);
}

/**
* {@inheritDoc}
*/
@Override
public Object setAndReturn(Object instance, Object value) throws IOException {
return delegate.setAndReturn(instance, value);
return instance;
}

private boolean shouldUseAnySetter() {
Expand Down
Loading
Loading