diff --git a/src/main/java/net/datafaker/transformations/JavaObjectTransformer.java b/src/main/java/net/datafaker/transformations/JavaObjectTransformer.java index 6ab1cfcda..ae9f86613 100644 --- a/src/main/java/net/datafaker/transformations/JavaObjectTransformer.java +++ b/src/main/java/net/datafaker/transformations/JavaObjectTransformer.java @@ -16,7 +16,6 @@ import java.util.stream.Stream; public class JavaObjectTransformer implements Transformer { - private static final Map, Object> CLASS2RESULT = new IdentityHashMap<>(); private static final Map, Consumer> SCHEMA2CONSUMER = new IdentityHashMap<>(); private static final Map, Constructor> CLASS2CONSTRUCTOR = new IdentityHashMap<>(); @@ -56,12 +55,15 @@ public Object apply(Object input, Schema schema) { result = getObject(schema, result, primaryConstructor); } else { - result = CLASS2RESULT.get(clazz); if (result == null) { try { - result = clazz.getDeclaredConstructor().newInstance(); - CLASS2RESULT.put(clazz, result); - } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | + Constructor primaryConstructor = CLASS2CONSTRUCTOR.get(clazz); + if (primaryConstructor == null) { + primaryConstructor = clazz.getDeclaredConstructors()[0]; + CLASS2CONSTRUCTOR.put(clazz, primaryConstructor); + } + result = primaryConstructor.newInstance(); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } diff --git a/src/test/java/net/datafaker/annotations/FakeAnnotationTest.java b/src/test/java/net/datafaker/annotations/FakeAnnotationTest.java index 4b9f4e809..eda715568 100644 --- a/src/test/java/net/datafaker/annotations/FakeAnnotationTest.java +++ b/src/test/java/net/datafaker/annotations/FakeAnnotationTest.java @@ -1,12 +1,16 @@ package net.datafaker.annotations; +import java.util.HashSet; +import java.util.List; import java.util.Locale; +import java.util.Objects; import java.util.Random; import net.datafaker.Faker; import net.datafaker.annotations.dto.ComplexPersonJavaRecord; import net.datafaker.annotations.dto.Person; import net.datafaker.annotations.dto.PersonJavaRecord; +import net.datafaker.sequence.FakeCollection; import net.datafaker.service.RandomService; import net.datafaker.transformations.Schema; import org.junit.jupiter.api.Test; @@ -82,6 +86,18 @@ void shouldGenerateEntityWithDefaultSchemaAndInDefaultSchemaInCurrentClass() { assertThat(person.name).isNotNull(); } + @Test + void shouldGenerateEntityWithDefaultSchemaAndInDefaultSchemaInCurrentClass2() { + final int expectedSize = 5; + final Schema schema = customSchema(); + List list = new FakeCollection.Builder() + .suppliers(() -> Faker.populate(DefaultPerson.class, schema)) + .len(expectedSize) + .generate(); + + assertThat(new HashSet<>(list)).hasSize(expectedSize); + } + @Test void shouldGenerateEntityFromJavaRecordWithDefaultSchemaAndInDefaultSchemaInCurrentClass() { var person = Faker.populate(DefaultPersonJavaRecord.class); @@ -113,6 +129,19 @@ void shouldGenerateEntityFromJavaRecordWithDefaultSchemaAndInDefaultSchemaInCurr public static class DefaultPerson { private String name; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof DefaultPerson that)) return false; + + return Objects.equals(name, that.name); + } + + @Override + public int hashCode() { + return name != null ? name.hashCode() : 0; + } } public static class SimplePerson {