Duplicate key error thrown when extending Faker class
Closed this issue ยท 4 comments
Describe the bug
If a class that extends the Faker class has two methods returing the same type, calling one method of the Faker class throws the following error:
Exception in thread "main" java.lang.IllegalStateException: Duplicate key String (attempted merging values public java.lang.Long org.example.BaseFaker.anotherMethodReturningString() and public java.lang.Long org.example.BaseFaker.oneMethodReturningString())
at java.base/java.util.stream.Collectors.duplicateKeyException(Collectors.java:135)
at java.base/java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$1(Collectors.java:182)
at java.base/java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:1024)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
at net.datafaker.providers.base.ObjectMethods.scanMethodsByReturnType(ObjectMethods.java:26)
at java.base/java.util.Map.computeIfAbsent(Map.java:1066)
at net.datafaker.providers.base.ObjectMethods.getMethodByReturnType(ObjectMethods.java:44)
at net.datafaker.providers.base.ObjectMethods.executeMethodByReturnType(ObjectMethods.java:50)
at net.datafaker.providers.base.ProviderRegistration.getProvider(ProviderRegistration.java:20)
To Reproduce
BaseFaker class:
import net.datafaker.Faker;
public class BaseFaker extends Faker {
public String oneMethodReturningString() {
return this.name().fullName();
}
public String anotherMethodReturningString() {
return this.name().fullName();
}
}
The error is thrown when calling oneMethodReturningString()
public class Main {
public static void main(String[] args) {
BaseFaker baseFaker = new BaseFaker();
baseFaker.oneMethodReturningString();
}
}
Expected behavior
The error should not be thrown
Versions:
- OS: Windows
- JDK: Java 21
- Faker Version [e.g. 2.4.0]
Additional context
The error starts to happen after version 2.3.0. With version 2.2.2 the code provided works well
Hi @jsolferreira , I think this may have been introduced as part of #1271.
@asolntsev , is this something you could have a look at?
@jsolferreira thank you for the precise description and steps to reproduce. It helps to quickly fix the problem!
Should be fixed by this PR: #1369
@jsolferreira could you please give the snapshot release a try? If it works, I'll publish a new release.
@bodiam @asolntsev Just tested the same code example and it worked with version 2.4.1-SNAPSHOT
Thanks for the quick fix!