Skip to content

Commit

Permalink
Fix generating nested generic type in Jackson (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Aug 1, 2023
1 parent d9a5256 commit b6dd7ec
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

package com.navercorp.fixturemonkey.jackson.introspector;

import java.lang.reflect.Type;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.ArrayType;
import com.fasterxml.jackson.databind.type.TypeFactory;
Expand All @@ -33,6 +36,7 @@
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.type.Types;
import com.navercorp.fixturemonkey.jackson.FixtureMonkeyJackson;
import com.navercorp.fixturemonkey.jackson.type.JacksonTypeReference;

@API(since = "0.5.5", status = Status.MAINTAINED)
public final class JacksonArrayArbitraryIntrospector implements ArbitraryIntrospector, Matcher {
Expand All @@ -52,11 +56,16 @@ public boolean match(Property property) {
return Types.getActualType(property.getType()).isArray();
}

@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context) {
Property property = context.getResolvedProperty();
Class<?> elementType = Types.getArrayComponentType(property.getAnnotatedType());
TypeFactory typeFactory = TypeFactory.defaultInstance();
JavaType elementType = typeFactory.constructType(new JacksonTypeReference<Object>() {
@Override
public Type getType() {
return Types.getArrayComponentAnnotatedType(property.getAnnotatedType()).getType();
}
});

ArrayType arrayType = TypeFactory.defaultInstance()
.constructArrayType(elementType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

package com.navercorp.fixturemonkey.jackson.introspector;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Type;
import java.util.Collection;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
Expand All @@ -35,6 +38,7 @@
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.type.Types;
import com.navercorp.fixturemonkey.jackson.FixtureMonkeyJackson;
import com.navercorp.fixturemonkey.jackson.type.JacksonTypeReference;

@API(since = "0.5.5", status = Status.MAINTAINED)
public final class JacksonCollectionArbitraryIntrospector implements ArbitraryIntrospector, Matcher {
Expand All @@ -54,14 +58,21 @@ public boolean match(Property property) {
return Collection.class.isAssignableFrom(Types.getActualType(property.getType()));
}

@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings("unchecked")
@Override
public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context) {
Property property = context.getResolvedProperty();
Class<?> containerType = Types.getActualType(property.getType());
Class<?> elementType = Types.getActualType(Types.getGenericsTypes(property.getAnnotatedType()).get(0));
TypeFactory typeFactory = TypeFactory.defaultInstance();
AnnotatedType elementAnnotatedType = Types.getGenericsTypes(property.getAnnotatedType()).get(0);
JavaType elementType = typeFactory.constructType(new JacksonTypeReference<Object>() {
@Override
public Type getType() {
return elementAnnotatedType.getType();
}
});

CollectionType collectionType = TypeFactory.defaultInstance()
CollectionType collectionType = typeFactory
.constructCollectionType((Class<? extends Collection<?>>)containerType, elementType);

return new ArbitraryIntrospectorResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

package com.navercorp.fixturemonkey.jackson.introspector;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Type;
import java.util.Map;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapLikeType;
import com.fasterxml.jackson.databind.type.TypeFactory;
Expand All @@ -35,6 +38,7 @@
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.type.Types;
import com.navercorp.fixturemonkey.jackson.FixtureMonkeyJackson;
import com.navercorp.fixturemonkey.jackson.type.JacksonTypeReference;

@API(since = "0.5.5", status = Status.MAINTAINED)
public final class JacksonMapArbitraryIntrospector implements ArbitraryIntrospector, Matcher {
Expand All @@ -54,15 +58,31 @@ public boolean match(Property property) {
return Map.class.isAssignableFrom(Types.getActualType(property.getType()));
}

@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings("unchecked")
@Override
public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context) {
TypeFactory typeFactory = TypeFactory.defaultInstance();

Property property = context.getResolvedProperty();
Class<? extends Map<?, ?>> containerType = (Class<? extends Map<?, ?>>)Types.getActualType(property.getType());
Class<?> keyType = Types.getActualType(Types.getGenericsTypes(property.getAnnotatedType()).get(0));
Class<?> valueType = Types.getActualType(Types.getGenericsTypes(property.getAnnotatedType()).get(1));

MapLikeType mapType = TypeFactory.defaultInstance()
AnnotatedType keyAnnotatedType = Types.getGenericsTypes(property.getAnnotatedType()).get(0);
AnnotatedType valueAnnotatedType = Types.getGenericsTypes(property.getAnnotatedType()).get(1);
JavaType keyType = typeFactory.constructType(new JacksonTypeReference<Object>() {
@Override
public Type getType() {
return keyAnnotatedType.getType();
}
});

JavaType valueType = typeFactory.constructType(new JacksonTypeReference<Object>() {
@Override
public Type getType() {
return valueAnnotatedType.getType();
}
});

MapLikeType mapType = typeFactory
.constructMapType(containerType, keyType, valueType);

return new ArbitraryIntrospectorResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static com.navercorp.fixturemonkey.jackson.property.JacksonAnnotations.getJacksonAnnotation;

import java.lang.reflect.Type;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
Expand All @@ -36,7 +37,9 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
Expand All @@ -50,6 +53,7 @@
import com.navercorp.fixturemonkey.api.property.PropertyDescriptorProperty;
import com.navercorp.fixturemonkey.api.type.Types;
import com.navercorp.fixturemonkey.jackson.FixtureMonkeyJackson;
import com.navercorp.fixturemonkey.jackson.type.JacksonTypeReference;

@API(since = "0.5.5", status = Status.MAINTAINED)
public final class JacksonObjectArbitraryIntrospector implements ArbitraryIntrospector {
Expand All @@ -66,7 +70,13 @@ public JacksonObjectArbitraryIntrospector(ObjectMapper objectMapper) {
@Override
public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context) {
Property property = context.getResolvedProperty();
Class<?> type = Types.getActualType(property.getType());
TypeFactory typeFactory = TypeFactory.defaultInstance();
JavaType type = typeFactory.constructType(new JacksonTypeReference<Object>() {
@Override
public Type getType() {
return property.getType();
}
});

return new ArbitraryIntrospectorResult(
new JacksonCombinableArbitrary<>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Fixture Monkey
*
* Copyright (c) 2021-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.fixturemonkey.jackson.type;

import static com.navercorp.fixturemonkey.api.type.Types.generateAnnotatedTypeWithoutAnnotation;

import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Type;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.type.Types;

@SuppressWarnings("unused")
@API(since = "0.6.3", status = Status.MAINTAINED)
public abstract class JacksonTypeReference<T> extends com.fasterxml.jackson.core.type.TypeReference<T> {
private final AnnotatedType annotatedType;

protected JacksonTypeReference() {
AnnotatedType annotatedType = getClass().getAnnotatedSuperclass();
this.annotatedType = ((AnnotatedParameterizedType)annotatedType).getAnnotatedActualTypeArguments()[0];
}

protected JacksonTypeReference(Class<T> type) {
this.annotatedType = generateAnnotatedTypeWithoutAnnotation(type);
}

public Type getType() {
return this.annotatedType.getType();
}

public AnnotatedType getAnnotatedType() {
return this.annotatedType;
}

public boolean isGenericType() {
return !Types.getGenericsTypes(annotatedType).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import net.jqwik.api.Property;

Expand All @@ -38,11 +42,14 @@
import lombok.Value;

import com.navercorp.fixturemonkey.FixtureMonkey;
import com.navercorp.fixturemonkey.api.type.TypeReference;
import com.navercorp.fixturemonkey.customizer.InnerSpec;
import com.navercorp.fixturemonkey.jackson.plugin.JacksonPlugin;

class FixtureMonkeyJacksonTest {
private static final FixtureMonkey SUT = FixtureMonkey.builder()
.plugin(new JacksonPlugin())
.defaultNotNull(true)
.build();

@Property
Expand All @@ -64,6 +71,61 @@ void sampleNested() {
.isThrownBy(() -> SUT.giveMeOne(NestedStringValue.class));
}

@Property
void sampleGenericObject() {
StringValue actual = SUT.giveMeOne(new TypeReference<GenericObject<StringValue>>() {
})
.getValue();

then(actual).isInstanceOf(StringValue.class);
then(actual).isNotNull();
}

@Property
void sampleListNestedElement() {
StringValue actual = SUT.giveMeBuilder(new TypeReference<List<List<StringValue>>>() {
})
.size("$", 1)
.size("$[0]", 1)
.sample()
.get(0)
.get(0);

then(actual).isInstanceOf(StringValue.class);
then(actual).isNotNull();
}

@Property
void sampleMapNestedListValue() {
StringValue actual = SUT.giveMeBuilder(new TypeReference<Map<String, List<StringValue>>>() {
})
.setInner(
new InnerSpec()
.size(1)
.value(v -> v.size(1))
)
.sample()
.values().stream()
.flatMap(Collection::stream)
.collect(Collectors.toList())
.get(0);

then(actual).isInstanceOf(StringValue.class);
then(actual).isNotNull();
}

@Property
void sampleGenericArray() {
StringValue actual = SUT.giveMeBuilder(new TypeReference<StringValue[][]>() {
})
.size("$", 1)
.size("$[0]", 1)
.sample()[0][0];

then(actual).isInstanceOf(StringValue.class);
then(actual).isNotNull();
}

@Value
public static class JsonFormatSpec {
@JsonFormat(shape = Shape.NUMBER)
Expand Down Expand Up @@ -109,4 +171,9 @@ public static class StringValue {
public static class NestedStringValue {
StringValue value;
}

@Value
public static class GenericObject<T> {
T value;
}
}

0 comments on commit b6dd7ec

Please sign in to comment.