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

Truncation of significant digits in floating point numbers in json formatting #1993

Open
bogatikov opened this issue Dec 26, 2024 · 0 comments
Labels

Comments

@bogatikov
Copy link

When a JSON body contains a float value like 0.40000000000000002, the log records it as 0.4.
This occurs when the content type is application/json and the request is processed by JacksonJsonFieldBodyFilter.

Content-Type: application/json

{
  "amount": 0.40000000000000002
}

Log output:

{
  "origin": "remote",
  "type": "request",
  "correlation": "9c6416830b522967",
  "protocol": "HTTP/1.1",
  "remote": "/127.0.0.1:53879",
  "method": "POST",
  "uri": "http://localhost:8080/helloworld",
  "host": "localhost",
  "path": "/helloworld",
  "scheme": "http",
  "port": "8080",
  "headers": {
    ....
    "Content-Type": [
      "application/json"
    ],
    ....
  },
  "body": {
    "amount": 0.4
  }
}

The original float value is truncated, making the logs inconsistent with the actual request. This can lead to confusion when analyzing logs, as the exact input is not preserved

Expected Behavior

The log should retain and display the original float value exactly as it appeared in the request body, without altering the data.

Actual Behavior

Float values are truncated to 16 significant digits when logged

Possible Fix

The issue stems from how Jackson processes and copies JSON tokens. When Jackson encounters a float value, it parses it as a double, resulting in a loss of precision:

generator.copyCurrentEvent(parser);

Starting from Jackson 2.15+, the following method can be used to handle float values more accurately:

    public void copyCurrentEventExact(JsonParser p) throws IOException

This method treats float values as BigDecimal, preserving their full precision.

Note:
• This change may impact performance.
• It is recommended to make this behavior configurable, allowing users to choose between exact precision or standard copying for performance reasons.

Steps to Reproduce

  1. Send a request with a JSON body containing a float value with more than 16 significant digits.

Context

The current behavior results in misleading log entries when float values are involved, potentially affecting debugging and auditing processes

Your Environment

• Framework: Spring WebFlux
• Logging Library: Logbook 3.10.0
• Spring Versions: 3.0.4 / 3.4.0
• Jackson Versions: 2.14, 2.15, 2.18

@bogatikov bogatikov added the Bug label Dec 26, 2024
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

1 participant