Skip to content

[BUG] ModelBase.cpp fromJson method for int64 types always returns 0, regardless of JSON value #8450

@shimmerLum

Description

@shimmerLum

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

Hello!
I am attempting to determine if I have found a bug in the openapi-generator for cpprestsdk or if there's just a bug in my project (i.e. JSON or API spec).

I used the openapi-generator to generate cpprest code from a JSON definition given as input. The JSON definition is linked below. My definition uses int64 numbers and the generated bool ModelBase::fromJson( const web::json::value& val, int64_t & outVal ) implementation in ModelBase.cpp always returns 0, regardless of the JSON value.

openapi-generator version

I have used openapi-generator-cli-5.0.0-beta3.jar when I found the bug, but I tested with the latest from master, as well, using the following:

The bug can still be reproduced with the list of generators above.

OpenAPI declaration file content or url

The test.json file used as input to the openapi-generator

Generation Details

This is the code generated for the bool ModelBase::fromJson( const web::json::value& val, int64_t & outVal ) method, from ModelBase.cpp:

bool ModelBase::fromJson( const web::json::value& val, int64_t & outVal )
{
    outVal = !val.is_null() ? std::numeric_limits<int64_t>::quiet_NaN() : val.as_number().to_int64();
    return val.is_number();
}

The line that generates the bug seems to be
outVal = !val.is_null() ? std::numeric_limits<int64_t>::quiet_NaN() : val.as_number().to_int64();
specifically the condition by which the value is assigned: !val.is_null().

This number will never be null, therefore the condition will always be true and the assigned value std::numeric_limits<int64_t>::quiet_NaN() (which, in my case, is 0).

Steps to reproduce

You need to have cpprestsdk and Java installed on your machine.

  1. Copy the test.json definition.
  2. Run the openapi-generator as follows:
java -jar openapi-generator-cli-5.0.1-20210114.161254-35.jar generate -i test.json -g cpprest-sdk -o output_folder
  1. Go to output_folder and open ModelBase.cpp. Line 278 contains the bug.
Suggest a fix

Looking through similar code generated for other data types(such as int32, float, double), I think the condition should be !val.is_number(), because 'number' is the data type that is being used to convert from to int64:

outVal = !val.is_number() ? std::numeric_limits<int64_t>::quiet_NaN() : val.as_number().to_int64();

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions