OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Gson truncates Long values for Object

Open
#14,372 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When an endpoint's response type is set to an Object, there are issues de-serializing Long values using Gson in the generated ApiClient class because of the default objectToNumberStrategy that Gson uses. By default Gson uses ToNumberPolicy.DOUBLE. as the objectToNumberStrategy and this is problematic when trying to de-serialize a value that is larger than a double and causes inaccuracies.

openapi-generator version

6.0.1

OpenAPI declaration file content or url

Problematic endpoint where the GET has an Object return type.
https://github.com/candlepin/candlepin/blob/master/api/candlepin-api-spec.yaml#L1122-L1160

Steps to reproduce
  1. Define an endpoint that returns an object for GET requests.
  2. Generate the java api classes.
  3. Use the api class to make the GET request for the endpoint.
  4. Return a json with a Long value like the following example.
{"value" : 7076632681529943151}

This example will be de-serialized to:
{value=7.076632681529943E18}

Related issues/PRs

I was unable to find any related open or closed issues/PRs.

Suggest a fix/enhancement

As a suggestion, you could set the Gson objectToNumberStrategy to ToNumberPolicy.LONG_OR_DOUBLE in the generated ApiClient class.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated Java ApiClient and reproduce the issue using the candlepin endpoint described in the report, returning the provided JSON value. Inspect the Java generator area that controls Gson deserialization and verify the completed change preserves the Long value without converting it to an imprecise Double.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.