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

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

Closed
5 tasks done
shimmerLum opened this issue Jan 14, 2021 · 1 comment · Fixed by #10300
Closed
5 tasks done

Comments

@shimmerLum
Copy link

shimmerLum commented Jan 14, 2021

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!

@ghost
Copy link

ghost commented Jan 14, 2021

Thanks for putting this together Diana.

If the maintainer/developer investigating this ticket needs additional info, we could likely put together a throwaway Docker script + container to execute the build + run operations that yield the behaviors noted in the report above. I believe I already have a template for that which I use for VIM bug reports.

shayan-eftekhari added a commit to aimms/openapi-generator that referenced this issue Aug 31, 2021
shayan-eftekhari added a commit to aimms/openapi-generator that referenced this issue Aug 31, 2021
shayan-eftekhari added a commit to aimms/openapi-generator that referenced this issue Aug 31, 2021
shayan-eftekhari added a commit to aimms/openapi-generator that referenced this issue Aug 31, 2021
shayan-eftekhari added a commit to aimms/openapi-generator that referenced this issue Aug 31, 2021
wing328 pushed a commit that referenced this issue Sep 9, 2021
…t&) bug which incorrectly returns zero (#10300)

* BUG FIX: A missing semicolon in cpp-pistache-server generated code.

* BUG FIX: Provide default values of schema in cpp-pistache-server generated code.

* BUG FIX: Provide default values of schema in cpp-pistache-server generated code.

* Fix a bug in cpprest-sdk generator (Issue #8450)

* Fix a bug in cpprest-sdk generator (Issue #8450)

* Fix a bug in cpprest-sdk generator (Issue #8450)

* Revert "Fix a bug in cpprest-sdk generator (Issue #8450)"

This reverts commit 7d8f842.

* Fix a bug in cpprest-sdk generator (Issue #8450)
wing328 pushed a commit that referenced this issue Oct 13, 2021
…ead of 1/0 (#10554)

* BUG FIX: A missing semicolon in cpp-pistache-server generated code.

* BUG FIX: Provide default values of schema in cpp-pistache-server generated code.

* BUG FIX: Provide default values of schema in cpp-pistache-server generated code.

* Fix a bug in cpprest-sdk generator (Issue #8450)

* Fix a bug in cpprest-sdk generator (Issue #8450)

* Fix a bug in cpprest-sdk generator (Issue #8450)

* Revert "Fix a bug in cpprest-sdk generator (Issue #8450)"

This reverts commit 7d8f842.

* Fix a bug in cpprest-sdk generator (Issue #8450)

* A new overloaded version of parameterToString method is added to the ApiClient class (mustache file) of cpp-rest-sdk-client generator. This new overload accepts bool value and is added to serialize bool values as boolapha instead of integer representation.

* A new overloaded version of parameterToString method is added to the ApiClient class (mustache file) of cpp-rest-sdk-client generator. This new overload accepts bool value and is added to serialize bool values as boolapha instead of integer representation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment