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

Duplicate key error thrown when extending Faker class #1368

Closed
jsolferreira opened this issue Oct 3, 2024 · 4 comments · Fixed by #1369
Closed

Duplicate key error thrown when extending Faker class #1368

jsolferreira opened this issue Oct 3, 2024 · 4 comments · Fixed by #1369
Assignees
Labels
bug Something isn't working

Comments

@jsolferreira
Copy link

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

@bodiam
Copy link
Contributor

bodiam commented Oct 4, 2024

Hi @jsolferreira , I think this may have been introduced as part of #1271.

@asolntsev , is this something you could have a look at?

@asolntsev asolntsev self-assigned this Oct 4, 2024
@asolntsev asolntsev added the bug Something isn't working label Oct 4, 2024
asolntsev added a commit that referenced this issue Oct 4, 2024
... when a custom faker has multiple methods with the same return type.
@asolntsev
Copy link
Collaborator

@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

bodiam pushed a commit that referenced this issue Oct 5, 2024
... when a custom faker has multiple methods with the same return type.
@bodiam
Copy link
Contributor

bodiam commented Oct 5, 2024

@jsolferreira could you please give the snapshot release a try? If it works, I'll publish a new release.

@bodiam bodiam reopened this Oct 5, 2024
@jsolferreira
Copy link
Author

@bodiam @asolntsev Just tested the same code example and it worked with version 2.4.1-SNAPSHOT

Thanks for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants