SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
807 stars 171 forks source link

Type-Converting will append type as a filed in document. #350

Closed StevenJSun closed 5 years ago

StevenJSun commented 5 years ago

Expected Behavior

1、When I GETting Documents, some data will return type-conserving JSON, like data[0]. Is there a way or configuration to make API retrun all documents just like data[1]? [{'uid': {'$numberLong': '5887391606'}}, # data[0] {'uid': 2084824280}, # data[1] {'uid': {'$numberLong': '5668990174'}} # data[2] ]

Current Behavior

Context

Environment

Steps to Reproduce

1. 2. 3.

Possible Implementation

ujibang commented 5 years ago

java int type maximum value is 2147483648; the uid= 5887391606 is therefore parsed as a Long. Storing the long value in mongodb results in {'$numberLong': '5887391606'}

I'm not sure if this is a bug. I'll look at it.

References https://restheart.org/docs/representation-format/#bson-types and https://docs.mongodb.com/manual/reference/mongodb-extended-json/#numberlong

StevenJSun commented 5 years ago

I think it is not a problem, we want to get the repsonse json like {'uid': 2084824280} but not the format: {'uid': {'$numberLong': '5887391606'}, of course, the 'uid' is long type. so it there any config for it?

StevenJSun commented 5 years ago

@ujibang @mkjsix does this problem solved ? we want to get the repsonse json like {'uid': 2084824280} but not the format: {'uid': {'$numberLong': '5887391606'}, of course, the 'uid' is long type. so it there any config for it?

ujibang commented 5 years ago

Hi @StevenJSun

as explained this is an issue with the MongoDB java driver parsing logic.

We are working at it. The point is that RESTHeart is using the default JsonWriterSettings which is EXTENDED and produces that output for java long values.

We are planning to add a query parameter to the request to allow selecting the JsonWriterSettings as EXTENDED, RELAXED and SHELL (default EXTENDED).

The RELAXED mode will output what you are expecting.

StevenJSun commented 5 years ago

@ujibang Okay, and what about the release time for this issue fixed ?

ujibang commented 5 years ago

Hi @StevenJSun

this is a "feature request" and we added it to the roadmap.

I expect it to be included in next release of RESTHeart, probably in September. However we don't commit on a release date.

mkjsix commented 5 years ago

@StevenJSun a clarification on the above reply from @ujibang: this is going to be changed first in the Commercial Edition in September, then it will be moved in the open source edition, probably in October or November. We commit on specific dates only for Enterprise customers with an active commercial subscription.

Of course, meanwhile anybody can open a PR and contribute a solution (with tests and a bit of documentation...) to the open source edition.

ujibang commented 5 years ago

Hi @StevenJSun

I just pushed a commit for jsonMode query parameter handling, that can workaround your problem.

Please note that this feature is going to be added to the upcoming RESTHeart Platform 4.1. More info in the roadmap

Expected Release Date for 4.1: End of September / Early October

An example:

GET locahost:8080/coll/5d7a4b59cf6eeb5fb1686613

{
    "_etag": {
        "$oid": "5d7a4f10af0e1b77a7731d05"
    },
    "_id": {
        "$oid": "5d7a4b59cf6eeb5fb1686613"
    },
    "a": 1,
    "b": 1.0,
    "big": 1568295769260,
    "timestamp": {
        "$date": 1568295769260
    }
}
GET locahost:8080/coll/5d7a4b59cf6eeb5fb1686613?jsonMode=relaxed

{
    "_etag": {
        "$oid": "5d7a6c61bd8a0d69516bbf55"
    },
    "_id": {
        "$oid": "5d7a4b59cf6eeb5fb1686613"
    },
    "a": 1,
    "b": 1.0,
    "big": 1568295769260,
    "timestamp": {
        "$date": "2019-09-12T13:42:49.26Z"
    }
}
GET locahost:8080/coll/5d7a4b59cf6eeb5fb1686613?jsonMode=extended

{
    "_etag": {
        "$oid": "5d7a4f10af0e1b77a7731d05"
    },
    "_id": {
        "$oid": "5d7a4b59cf6eeb5fb1686613"
    },
    "a": {
        "$numberInt": "1"
    },
    "b": {
        "$numberDouble": "1.0"
    },
    "big": {
        "$numberLong": "1568295769260"
    },
    "timestamp": {
        "$date": {
            "$numberLong": "1568295769260"
        }
    }
}
GET locahost:8080/coll/5d7a4b59cf6eeb5fb1686613?jsonMode=strict

{
    "_etag": {
        "$oid": "5d7a4f10af0e1b77a7731d05"
    },
    "_id": {
        "$oid": "5d7a4b59cf6eeb5fb1686613"
    },
    "a": 1,
    "b": 1.0,
    "big": {
        "$numberLong": "1568295769260"
    },
    "timestamp": {
        "$date": 1568295769260
    }
}

finally and very interesting the SHELL mode. This allows to copy the response body and use it directly in the mongoshell!

GET locahost:8080/coll/5d7a4b59cf6eeb5fb1686613?jsonMode=shell
....
Content-Type: application/javascript

{"_id":ObjectId("5d7a4b59cf6eeb5fb1686613"),"_etag":ObjectId("5d7a6d13bd8a0d69516bbf56"),"timestamp":ISODate("2019-09-12T13:42:49.260Z"),"a":1,"b":1.0,"big":NumberLong("1568295769260"),"verybig":NumberLong("5887391606")}