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

added SingleArgumentPropertiesCreatorModeAnnotationIntrospector #2

Merged
Next Next commit
added SingleArgumentPropertiesCreatorModeAnnotationIntrospector
  • Loading branch information
lpandzic committed Apr 15, 2021
commit dbd76b7e79431c4f3bbc2bd56c5be4cf54c6da9d
Original file line number Diff line number Diff line change
@@ -8,5 +8,6 @@ public class InfobipJacksonModule extends SimpleModule {
public void setupModule(SetupContext context) {
super.setupModule(context);
context.insertAnnotationIntrospector(new InfobipJacksonAnnotationIntrospector());
context.insertAnnotationIntrospector(new SingleArgumentPropertiesCreatorModeAnnotationIntrospector());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.infobip.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.*;

import java.lang.reflect.Field;
import java.util.Objects;
import java.util.stream.Stream;

public class SingleArgumentPropertiesCreatorModeAnnotationIntrospector extends NopAnnotationIntrospector {

@Override
public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated a) {
JsonCreator ann = _findAnnotation(a, JsonCreator.class);

if (Objects.nonNull(ann)) {
return ann.mode();
}

if (!(a instanceof AnnotatedConstructor)) {
return null;
}

AnnotatedConstructor annotatedConstructor = (AnnotatedConstructor) a;
Class<?> declaringClass = annotatedConstructor.getDeclaringClass();
if (Throwable.class.isAssignableFrom(declaringClass)) {
return null;
}

if (annotatedConstructor.getParameterCount() != 1) {
return null;
}

if (declaringClass.getDeclaredConstructors().length > 1) {
return null;
}

String parameterName = annotatedConstructor.getAnnotated().getParameters()[0].getName();
boolean hasAMatchingField = Stream.of(declaringClass.getDeclaredFields())
.map(Field::getName)
.anyMatch(fieldName -> fieldName.equals(parameterName));
if(!hasAMatchingField) {
return null;
}

return JsonCreator.Mode.PROPERTIES;
}
}
Original file line number Diff line number Diff line change
@@ -110,14 +110,12 @@ interface FooBar extends SimpleJsonHierarchy<FooBarType> {
FooBarType getType();
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Foo implements FooBar {
private final String foo;
private final FooBarType type = FooBarType.FOO;
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Bar implements FooBar {
private final String bar;
@@ -132,4 +130,4 @@ enum FooBarType implements TypeProvider {

private final Class<? extends FooBar> type;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.infobip.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.*;
import org.junit.jupiter.api.Test;
@@ -48,14 +47,12 @@ public Class<?> resolve(Map<String, Object> json) {
}
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Foo extends FooBar {
private final String foo;
private final FooBarType type = FooBarType.FOO;
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Bar extends FooBar {
private final String bar;
@@ -70,4 +67,4 @@ enum FooBarType {

private final Class<? extends FooBar> type;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.infobip.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.*;
@@ -113,14 +112,12 @@ public Class<?> resolve(Map<String, Object> json) {
}
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Foo implements FooBar {
private final String foo;
private final FooBarType type = FooBarType.FOO;
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Bar implements FooBar {
private final String bar;
@@ -135,4 +132,4 @@ enum FooBarType {

private final Class<? extends FooBar> type;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.infobip.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
@@ -108,14 +107,12 @@ interface FooBar extends SimpleJsonHierarchy<FooBarType> {
FooBarType getType();
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Foo implements FooBar {
private final String foo;
private final FooBarType type = FooBarType.FOO;
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Bar implements FooBar {
private final String bar;
@@ -135,4 +132,4 @@ String getValue() {
return toString().toLowerCase();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.infobip.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.*;
@@ -123,7 +122,6 @@ public MammalJsonTypeResolver() {
}
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Human implements Mammal {
private final String name;
@@ -146,4 +144,4 @@ enum MammalType implements TypeProvider {

private final Class<? extends Mammal> type;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.infobip.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.*;
@@ -106,7 +105,6 @@ interface Animal extends SimpleJsonHierarchy<AnimalType> {
interface Mammal extends Animal {
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Human implements Mammal {
private final String name;
@@ -124,4 +122,4 @@ enum AnimalType implements TypeProvider {

private final Class<? extends Animal> type;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.infobip.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
@@ -117,14 +116,12 @@ public LowerUnderscorePresentPropertyJsonTypeResolver(Class<E> type) {
}
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class RoadBike implements Bike {

private final String roadBike;
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class MountainBike implements Bike {

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.infobip.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.*;
@@ -103,14 +102,12 @@ interface Bike extends PresentPropertyJsonHierarchy<BikeType> {

}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class RoadBike implements Bike {

private final String roadBike;
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Bmx implements Bike {

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.infobip.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.CaseFormat;
import lombok.*;
@@ -43,14 +42,12 @@ public BikeTypeResolver() {
}
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class RoadBike implements Bike {

private final String roadBike;
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class MountainBike implements Bike {

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.infobip.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.*;
@@ -105,14 +104,12 @@ void shouldDeserializeFooAsFooFromJson() throws JsonProcessingException {
interface FooBar extends SimpleJsonHierarchy<FooBarType> {
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Foo implements FooBar {
private final String foo;
private final FooBarType type = FooBarType.FOO;
}

@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Bar implements FooBar {
private final String bar;
@@ -127,4 +124,4 @@ enum FooBarType implements TypeProvider {

private final Class<? extends FooBar> type;
}
}
}