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

Invalid JSON on GET request due to rounding error of decimal numbers #4346

Closed
FWuellhorst opened this issue May 30, 2023 · 7 comments
Closed
Labels
Milestone

Comments

@FWuellhorst
Copy link

Bug description

  • Orion version: 3.6.0
  • MongoDB version 4.4
  • Env variables or CLI parameters
      - ME_CONFIG_OPTIONS_EDITORTHEME=${ME_CONFIG_OPTIONS_EDITORTHEME:-ambiance}
      - ME_CONFIG_MONGODB_SERVER=${ME_CONFIG_MONGODB_SERVER:-mongodb}
      - ME_CONFIG_MONGODB_PORT=${ME_CONFIG_MONGODB_PORT:-27017}
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=${ME_CONFIG_MONGODB_ENABLE_ADMIN:-true}
      - ME_CONFIG_MONGODB_AUTH_DATABASE=${ME_CONFIG_MONGODB_AUTH_DATABASE:-admin}
  • Operating System or Docker Image used: docker v 20.10.16; fiware/orion:3.6.0

How to reproduce it

  1. Create an entity with an attribute containing a number
  2. Update the value of said attribute to e.g. 7.999999999536117
  3. Get the entity via requests. I used the python requests package with the following settings:
url: 'http://some_ipv4:1026/v2/entities/abc'
{'options': <AttrsFormat.NORMALIZED: 'normalized'>}
{'User-Agent': 'python-requests/2.27.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'fiware-service': 'abc', 'fiware-servicepath': '/abc'}
  1. Try to parse the return as json and get the JSONDecodeError:
....
"pressure": {
    "type": "Number",
    "value": 8.,
    "metadata": {
      "TimeInstant": {
        "type": "DateTime",
        "value": "2023-05-30T07:12:41.210Z"
      }
    }
  }

I know this is not a clear script to reproduce, but maybe it is already sufficient to reproduce. If the error does not occur on your side, I can invest the time and give a detailed script to reproduce.

Expected behavior
Either return 7.999999999536117 or 8.0 as the value for the attribute.

@fgalan
Copy link
Member

fgalan commented May 30, 2023

Thanks for the report!

Reproduced with newest Orion version (3.8.0) in language-agnostic way. Creating entity this way:

$ curl localhost:1026/v2/entities -s -S -H 'Content-Type: application/json' -d @- <<EOF
{
  "id": "Room1",
  "type": "Room",
  "temperature": {
    "value": 7.999999999536117,
    "type": "Number"
  }
}
EOF

Getting the entity, wrong JSON:

$ curl localhost:1026/v2/entities/Room1 -s -S
{"id":"Room1","type":"Room","temperature":{"type":"Number","value":8.,"metadata":{}}}

Entity at DB is fine, so it is a problem in the rendering logic:

{
	"_id" : {
		"id" : "Room1",
		"type" : "Room",
		"servicePath" : "/"
	},
	"attrNames" : [
		"temperature"
	],
	"attrs" : {
		"temperature" : {
			"value" : 7.999999999536117,
			"type" : "Number",
			"creDate" : 1685460364.503991,
			"modDate" : 1685460364.503991,
			"mdNames" : [ ]
		}
	},
	"creDate" : 1685460364.503991,
	"modDate" : 1685460364.503991,
	"lastCorrelator" : "4a9735a6-fefe-11ed-bfdf-000c29583ca5"
}

@fgalan fgalan added this to the 3.9.0 milestone May 30, 2023
@FWuellhorst
Copy link
Author

Thanks for providing the code to reproduce this!

@fgalan fgalan modified the milestones: 3.9.0, 3.10.0 Jun 2, 2023
@fgalan
Copy link
Member

fgalan commented Jun 5, 2023

Problem is here

void JsonObjectHelper::addNumber(const std::string& key, double value)
{
  if (!empty)
  {
    ss += ',';
  }
  ss += '"';
  ss += toJsonString(key);
  ss += "\":";
  ss +=  double2string(value);

  empty = false;

}

value is ok, but it is wronly rendered in the ss string,

imagen

So it seems double2string() function has flaws.

@fgalan
Copy link
Member

fgalan commented Jun 5, 2023

PR #4354

@fgalan
Copy link
Member

fgalan commented Jun 5, 2023

This issue should be fixed now in master branch.

@FWuellhorst could you tests using telefonicaiot/fiware-orion:latest dockerhub image to check it and provide feedback on this issue, please? Thanks!

@FWuellhorst
Copy link
Author

@fgalan Yes, it does work for me! Thanks for the quick fix!
A short follow-up question: Is this orion version compatible with mongo:4.4 and iotagent-json:1.21.1?
It seems to work, but I don't want to run into troubles later on.

@fgalan
Copy link
Member

fgalan commented Jun 6, 2023

A short follow-up question: Is this orion version compatible with mongo:4.4 and iotagent-json:1.21.1?

For questions about Orion, please use the proper channel, which is https://stackoverflow.com (using the "fiware-orion" label). Github issues are for other kind of items (bugs, feature requests, etc.).

Given the positive feedback in the previous comment (thanks!), I'm going to close this issue.

@fgalan fgalan closed this as completed Jun 6, 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

2 participants