Skip to content

Commit

Permalink
Remove supporting @JsonTypeInfo, @JsonSubTypes (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Apr 11, 2023
1 parent 4abad3d commit 790f9e6
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Map<String, CombinableArbitrary> getCombinableArbitraryByPropertyName() {

public List<Arbitrary<?>> getArbitraries() {
return arbitrariesByChildProperty.values().stream()
.map(CombinableArbitrary::rawValue)
.map(CombinableArbitrary::combined)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;
Expand Down Expand Up @@ -91,10 +90,6 @@ private static final class ArrayBuilder {
private final List<Object> array;
private final Class<?> componentType;
private final int size;
/**
* It is used for specifying {@code JacksonCombinableArbitrary} raw value.
*/
private boolean combined = true;

public ArrayBuilder(Class<?> componentType, int size) {
this.array = new ArrayList<>();
Expand All @@ -108,22 +103,13 @@ ArrayBuilder add(Object value) {
return this;
}

if (value instanceof Map) {
combined = false;
}

array.add(value);
return this;
}

// cast to Object for preventing ClassCastException when primitive type
Object build() {
Object array;
if (combined) {
array = Array.newInstance(componentType, size);
} else {
array = Array.newInstance(Object.class, size);
}
Object array = Array.newInstance(componentType, size);

for (int i = 0; i < this.array.size(); i++) {
Array.set(array, i, this.array.get(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package com.navercorp.fixturemonkey.jackson.introspector;

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

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
Expand All @@ -36,10 +34,6 @@
import net.jqwik.api.Builders.BuilderCombinator;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
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.ObjectMapper;

import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
Expand All @@ -65,10 +59,9 @@ public JacksonArbitraryIntrospector(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

@SuppressWarnings("unchecked")
@Override
public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context) {
Property property = context.getArbitraryProperty().getObjectProperty().getProperty();
Property property = context.getResolvedProperty();
Class<?> type = Types.getActualType(property.getType());

List<ArbitraryProperty> childrenProperties = context.getChildren();
Expand Down Expand Up @@ -100,22 +93,7 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
.map(it -> format(value, it))
.orElse(value);

JsonTypeInfo jsonTypeInfo = getJacksonAnnotation(property, JsonTypeInfo.class);
if (jsonTypeInfo == null) {
map.put(resolvePropertyName, jsonFormatted);
} else {
if (jsonTypeInfo.include() == As.WRAPPER_OBJECT) {
String typeIdentifier = getJsonTypeInfoIdentifier(
jsonTypeInfo,
property
);

Map<String, Object> typeJson = (Map<String, Object>)
map.getOrDefault(typeIdentifier, new HashMap<>());
typeJson.put(resolvePropertyName, jsonFormatted);
map.put(typeIdentifier, typeJson);
}
}
map.put(resolvePropertyName, jsonFormatted);
}
return map;
});
Expand All @@ -129,50 +107,7 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
}

private Map<String, Object> initializeMap(Property property) {
Map<String, Object> defaultMap = new HashMap<>();

JsonTypeInfo jsonTypeInfo = getJacksonAnnotation(property, JsonTypeInfo.class);
if (jsonTypeInfo == null || jsonTypeInfo.include() == As.WRAPPER_OBJECT) {
return defaultMap;
}

String jsonTypeInfoValue = getJsonTypeInfoIdentifier(jsonTypeInfo, property);
String jsonTypeInfoPropertyName = getJsonTypeInfoPropertyName(jsonTypeInfo);

defaultMap.put(jsonTypeInfoPropertyName, jsonTypeInfoValue);
return defaultMap;
}

private String getJsonTypeInfoPropertyName(JsonTypeInfo jsonTypeInfo) {
return "".equals(jsonTypeInfo.property())
? jsonTypeInfo.use().getDefaultPropertyName()
: jsonTypeInfo.property();
}

private String getJsonTypeInfoIdentifier(
JsonTypeInfo jsonTypeInfo,
Property property
) {
JsonTypeName jsonTypeName = getJacksonAnnotation(property, JsonTypeName.class);
Class<?> type = Types.getActualType(property.getType());

Id id = jsonTypeInfo.use();
String jsonTypeInfoValue;
switch (id) {
case NAME:
if (jsonTypeName != null) {
jsonTypeInfoValue = jsonTypeName.value();
} else {
jsonTypeInfoValue = type.getSimpleName();
}
break;
case CLASS:
jsonTypeInfoValue = type.getName();
break;
default:
throw new IllegalArgumentException("Unsupported JsonTypeInfo Id : " + id.name());
}
return jsonTypeInfoValue;
return new HashMap<>();
}

private Object format(Object object, JsonFormat jsonFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,21 @@

package com.navercorp.fixturemonkey.jackson.plugin;

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

import java.util.ArrayList;
import java.util.List;

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

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.navercorp.fixturemonkey.api.matcher.AssignableTypeMatcher;
import com.navercorp.fixturemonkey.api.matcher.Matcher;
import com.navercorp.fixturemonkey.api.option.GenerateOptionsBuilder;
import com.navercorp.fixturemonkey.api.plugin.Plugin;
import com.navercorp.fixturemonkey.api.property.ElementProperty;
import com.navercorp.fixturemonkey.jackson.FixtureMonkeyJackson;
import com.navercorp.fixturemonkey.jackson.generator.ElementJsonSubTypesObjectPropertyGenerator;
import com.navercorp.fixturemonkey.jackson.generator.JsonNodeContainerPropertyGenerator;
import com.navercorp.fixturemonkey.jackson.generator.PropertyJsonSubTypesObjectPropertyGenerator;
import com.navercorp.fixturemonkey.jackson.introspector.JacksonArbitraryIntrospector;
import com.navercorp.fixturemonkey.jackson.introspector.JsonNodeIntrospector;
import com.navercorp.fixturemonkey.jackson.property.JacksonPropertyNameResolver;
Expand Down Expand Up @@ -85,19 +79,7 @@ public void accept(GenerateOptionsBuilder optionsBuilder) {
if (this.defaultOptions) {
optionsBuilder
.objectIntrospector(it -> new JacksonArbitraryIntrospector(objectMapper))
.defaultPropertyNameResolver(new JacksonPropertyNameResolver())
.insertFirstArbitraryObjectPropertyGenerator(
property -> getJacksonAnnotation(property, JsonSubTypes.class) != null,
PropertyJsonSubTypesObjectPropertyGenerator.INSTANCE
)
.insertFirstArbitraryObjectPropertyGenerator(
property -> property instanceof ElementProperty
&& getJacksonAnnotation(
((ElementProperty)property).getContainerProperty(),
JsonSubTypes.class
) != null,
ElementJsonSubTypesObjectPropertyGenerator.INSTANCE
);
.defaultPropertyNameResolver(new JacksonPropertyNameResolver());
}

optionsBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.navercorp.fixturemonkey.tests.TestEnvironment.TEST_COUNT;
import static org.assertj.core.api.BDDAssertions.thenNoException;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.RepeatedTest;

import com.navercorp.fixturemonkey.FixtureMonkey;
Expand All @@ -17,6 +18,7 @@
import com.navercorp.fixturemonkey.tests.java.JacksonSpecs.TypeWithAnnotationsList;
import com.navercorp.fixturemonkey.tests.java.JacksonSpecs.TypeWithAnnotationsValue;

@Disabled
class JacksonTest {
private static final FixtureMonkey SUT = FixtureMonkey.builder()
.plugin(new JacksonPlugin())
Expand Down

0 comments on commit 790f9e6

Please sign in to comment.