Skip to content

number_between for integers returns doubles from time to time #1463

@valfirst

Description

@valfirst

Describe the bug
This is a regression issue. It's not reproduced on 2.4.11, but reproduced from time to time on 2.4.2. Most likely it is caused by this commit: f073184.

To Reproduce

import net.datafaker.Faker;

public class Main
{
    public static void main(String[] args)
    {
        Faker faker = new Faker();
        String result = faker.expression("#{number.number_between '1000','1000'}");
        System.out.printf(result);
    }
}

Expected behavior
The result should be '1000', but it is '1000.0' from time to time.

Versions:

  • OS: any
  • JDK: no correlation detected
  • Faker Version: 2.4.2

Additional context
The methods order is preserved when they are returned from class:

Method[] classMethods = clazz.getMethods();

But then they are stored in HashMap with unpredictable order:
classMethods.length == 0 ? emptyMap() : new HashMap<>(classMethods.length);

I see 2 possible solutions:

  • rely on method order in the class and use ordered collections to keep sequence - fragile
  • improve the algorithm to distinguish double-s vs long-s vs int-s, and search for the method in priority starting from best matching type: e.g. 10 -> int, 10000000000 -> long, 10.0 -> double

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions