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

BigDecmial generation fails on Java 11 #85

Closed
iljakorneckis opened this issue Mar 2, 2023 · 2 comments
Closed

BigDecmial generation fails on Java 11 #85

iljakorneckis opened this issue Mar 2, 2023 · 2 comments
Assignees
Labels

Comments

@iljakorneckis
Copy link
Collaborator

iljakorneckis commented Mar 2, 2023

When generating BigDecimals on Java 11, JavaFixture populates the stringCache field with a random not-a-number string. This affects the .toString() method at the very least and, by proxy, affects JSON generated by ObjectMapper. The .intValue() also produces zero. On Java 17, the same field is populated by a correct string representation of the decimal.

Could it be that the reason it works on Java17 is the module system preventing access to internals and forcing JF to use a constructor?

Test expected to pass

@Test
void bigDecimalTest() {
     var bigDecimal = Fixture.fixture().create(BigDecimal.class);

     assertThat(bigDecimal.intValue()).isNotZero();
     assertThat(new BigDecimal(bigDecimal.toString())).isEqualTo(bigDecimal);
}

Json generated by Object mapper:

The cpc field is a BigDecimal. It's not even valid Json, which is more of a problem with ObjectMapper than JavaFixture tho.

{
 "cpc":EMxJJRhq3XBJVv8vayJ1ivNOzWoO6tuegfY1DBCMLb9MrfYKdJSq19RhKB3COzqEoEOmfsD8naQbKvzTXLFyKmzYu45wV6YiDbTt2X4OimoFZ3hk7fUm4zmdOYK7TuOC,
 "category_id":-1313760193,
}

Java 11 BigDecimal

Screenshot from 2023-03-02 11-43-41

Java 17 BigDecimal

Screenshot from 2023-03-02 11-45-19

akutschera added a commit to akutschera/JavaFixture that referenced this issue Mar 3, 2023
@akutschera
Copy link
Collaborator

Yes. That is a bug. We should not rely on reflection for a BigDecimal, similar to a BigInteger.
Therefore I propose we add it to the SpecialSpecimen classes and create one using a random value in a constructor.

@Nylle
Copy link
Owner

Nylle commented Mar 3, 2023

Fixed in version 2.9.5

@Nylle Nylle closed this as completed Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants