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

Realm not converting a flat number 1.0 into double correctly #6219

Closed
maichongju opened this issue Oct 27, 2023 · 6 comments
Closed

Realm not converting a flat number 1.0 into double correctly #6219

maichongju opened this issue Oct 27, 2023 · 6 comments
Assignees
Labels
Encryption:Off Frequency:Always More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. O-Community Repro:Always T-Bug Waiting-For-Reporter Waiting for more information from the reporter before we can proceed

Comments

@maichongju
Copy link

How frequently does the bug occur?

Always

Description

When inserting a flat number 1.0 into the MongoDB Atlas. Realm does not convert the number into double. When the schema is defined as only accept double datatype, insertion will failed.

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

  1. Create a collection named test on MongoDB atlas with the following validation schema.
{
  "properties": {
    "geoLocation": {
      "bsonType": "object",
      "properties": {
        "type": {
          "bsonType": "string"
        },
        "coordinates": {
          "bsonType": "array",
          "items": {
            "bsonType": "double"
          }
        }
      },
      "required": [
        "type"
      ]
    },
    "_id": {
      "bsonType": "objectId"
    }
  }
}
  1. On the client side connect to the MongoDB and execute the following code
await mongoUser.mongoClient('mongodb-atlas').db('main').collection('test').insertOne({
    geoLocation: {
      type: 'Point',
      coordinates: [1.0, 2.0],
    }
  });
  1. Client side received insert not permit error
  2. MongoDB Atlas log shows following error message
role "readAndWriteAll" in "main.trip" does not have update permission for document with _id: ObjectID("652f3d4625b1458c017f723e"): could not validate document: 
	departureLocation.geoLocation.coordinates.1: Invalid type. Expected: type: undefined, bsonType: [double,null], given: [integer int long number mixed]

Version

"realm": "^12.2.1",, "@realm/react": "^0.6.0",

What services are you using?

Atlas App Services: Functions or GraphQL or DataAPI etc

Are you using encryption?

No

Platform OS and version(s)

Win11 23H2

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

@kneth
Copy link
Contributor

kneth commented Oct 30, 2023

@maichongju Thank you for reporting. We will try to reproduce in our tests to find the root cause. If you have tried with a version prior to 12, did you observe the same error?

@maichongju
Copy link
Author

I am sorry, but I don't have a setup for pre12.

Is there any update on this issue?

@maichongju
Copy link
Author

For whoever might have the same issue. The workaround I am using right now is to add or subtract a very small number (0.00001) when int is detected.

const flat_number = 1.0
if (Number.isInteger(flat_number)){
  flat_number = flat_number + 0.00001;
}

@HugoBounoua
Copy link

@kneth Did you release a fix for this? It's been more than a year, and Realm is still throwing exceptions for "int," "double," etc., due to the optimization of NodeJS converting to the smallest type possible. We are obliged to set everything as a "number," which is not ideal. Let me know if there's a workaround better than transforming the value itself. Thank you

@nirinchev
Copy link
Member

I believe this is a bug on app services, where we get the number 1 in json and treat it as an integer. Unfortunately, resolving it could impact many customers, so we'll have to be careful about how we approach it. In the meantime, you could do could try and tell the server that you explicitly want a double by passing in the ejson double representation:

await mongoUser.mongoClient('mongodb-atlas').db('main').collection('test').insertOne({
    geoLocation: {
      type: 'Point',
      coordinates: [ { $numberDouble: "1.0" }, { $numberDouble: "2.0" }],
    }
  })

It is obviously less convenient and you could probably add a helper function to avoid having to write the expansion everytime.

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Mar 20, 2024
@sync-by-unito sync-by-unito bot assigned nirinchev and unassigned kneth Mar 20, 2024
@nirinchev nirinchev added the More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. label Mar 22, 2024
Copy link
Contributor

github-actions bot commented Apr 6, 2024

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

@github-actions github-actions bot closed this as completed Apr 6, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Encryption:Off Frequency:Always More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. O-Community Repro:Always T-Bug Waiting-For-Reporter Waiting for more information from the reporter before we can proceed
Projects
None yet
Development

No branches or pull requests

4 participants