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

Object mapper gives error when deserializing Int64 #12

Open
ns-wxin opened this issue Jul 8, 2023 · 2 comments
Open

Object mapper gives error when deserializing Int64 #12

ns-wxin opened this issue Jul 8, 2023 · 2 comments

Comments

@ns-wxin
Copy link

ns-wxin commented Jul 8, 2023

This problem is relatively easy to reproduce. When Object Mapper (default) is trying to decode a serialized string (JSON) of an object containing a field that is a list of Int64 and when one of the number's absolute value is small (-103 in this case). The object mapper would throw exception:

2023-07-08 00:06:29 ERROR localhost 494264 DLPFP 0: UploadController.cpp:00678 trid=342343234 rqid=0 tenantid=2400 user='' Decoding error for: { "simhash" : [ -1374461674807444439, -103, 1803150389721624332, -292347449805 ]}, err: [oatpp::mongo::bson::Utils::readPrimitive()]: Error. Can't deserialize Int64 value. BSON document value type is expected to be Int64.

Sample string:
{ "simhash" : [ -1374461674807444439, -103, 1803150389721624332, -292347449805 ]}"

The condition I found for this error is that the int64 absolute value is small. In the test code, it works for testString2 where I changed the small value to a big one. Looks like when the value is between [-2147483648, 2147483648) ie. 2^31, the deserializer will give this error.

Code snippets:

class TestDto : public oatpp::DTO {
    DTO_INIT(TestDto, DTO)

    DTO_FIELD(List<Int64>, simhash);
};

oatpp::Object<TestDto> decodeTestDto(const std::string &line)
{
    auto doc = bsoncxx::from_json(line);
    auto view = doc.view();
    auto bson = oatpp::String((const char *)view.data(), view.length());
    return m_objectMapper.readFromString<oatpp::Object<TestDto>>(bson);
}

Testing Code and sample string:

std::string testString = "{ \"simhash\" : [ -1374461674807444439, -103, 1803150389721624332, -292347449805 ]}";
// the following string works
// std::string testString2 = "{ \"simhash\" : [ -1374461674807444439, -103446167480744443, 1803150389721624332, -292347449805 ]}";
try {
    oatpp::Object<TestDto> testDto = m_allFpGenerator->decodeTestDto(testString);
    LOG(INFO) << "TestDto decode OK for " << testString;
} catch (const std::exception &exc) {
    LOG(ERROR) << "Decoding error for: " << testString << ", err: " << exc.what(); 
}
@ns-wxin
Copy link
Author

ns-wxin commented Jul 14, 2023

@lganzzzo when you get a chance, could you take a look at the deserializer? Thanks.

@ns-wxin
Copy link
Author

ns-wxin commented Aug 15, 2023

@lganzzzo or @BillyONeal , could you take a quick look at this issue? It's pretty straight-forward to reproduce it. Any number in [-2147483648, 2147483648) can't be deserialized to an Int64. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant