Skip to content

Duplicate key error thrown when extending Faker class #1368

@jsolferreira

Description

@jsolferreira

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions