question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[C++][Pistache-server] Wrong "toJson" and "fromJson" method generation - compilation failed

See original GitHub issue
Description

The C++ generated code does not compile because of the following errors:

  1. there is no matching function for ModelBase::toJson taking as argument std::map<std::string, std::vector<Point2D>>
  2. the generated code is calling “fromJson” method as it is a member of std::map type
  3. the generated code is calling a set method that is not generated

These error occur in model/Detections.cpp file.

Detection.h:

#include "ModelBase.h"

#include "BoundingRectangle.h"
#include <string>
#include "Point2D.h"
#include <map>
#include <vector>

namespace org {
namespace openapitools {
namespace server {
namespace model {

class  Detection
    : public ModelBase
{
public:
    Detection();
    virtual ~Detection();

    nlohmann::json toJson() const override;
    void fromJson(nlohmann::json& json) override;
 
    std::map<std::string, std::vector<Point2D>>& getKeyPoints();
    //setKeyPoints is missing
    bool keyPointsIsSet() const;
    void unsetKeyPoints();


protected:
   
    std::map<std::string, std::vector<Point2D>> m_KeyPoints;
    bool m_KeyPointsIsSet;
 
};

}
}
}
}

#endif /* Detection_H_ */

Detection.cpp:

nlohmann::json Detection::toJson() const
{
    //
    //
    if(m_KeyPointsIsSet)
    {
          //this call
        val["keyPoints"] = ModelBase::toJson(m_KeyPoints);
    }
   //
   //
    return val;
}

void Detection::fromJson(nlohmann::json& val)
{
    //
    //
    if(val.find("keyPoints") != val.end())
    {
        if(!val["keyPoints"].is_null())
        {
            std::map<std::string, std::vector<Point2D>> newItem;
            //and these 2 lines below
            newItem.fromJson(val["keyPoints"]);
            setKeyPoints( newItem );
        }
        
    }
}
openapi-generator version

3.2.0 (I cloned the latest master branch 2 days ago)

OpenAPI declaration file content or url

https://raw.githubusercontent.com/deepviss/deepviss-server-example/master/src/main/resources/deep-viss.json

Command line used for generation

Inside cloned repository, after mvn clean package: java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate / -i deep-viss.json / -g cpp-pistache-server / -o …/openapi-pistache-server

Steps to reproduce

Note: Assuming Pistache is installed

  1. Go to output folder
  2. copy json.hpp (from https://github.com/nlohmann/json) to model folder
  3. mkdir build
  4. cd build
  5. cmake …
  6. make

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
etherealjoycommented, Aug 30, 2018

I will take a look at this once I finish the other issue which is assigned to me.

0reactions
etherealjoycommented, Nov 3, 2018

I tested it with #1359 and it worked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[C++][Pistache-server] Wrong "fromJson" method generation ...
Description The C++ generated code does not compile and I don't think it is localized to the fromJson method but more "global".
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
The JavaScript exceptions thrown by JSON.parse() occur when string failed to be parsed as JSON.
Read more >
JSON and serialization - Flutter documentation
This approach is covered in more detail in the code generation libraries section. Serializing JSON manually using dart:convert. Basic JSON serialization in ...
Read more >
How to Deserialize a list of objects from json in flutter
The next step, is to turn that iterable of JSON objects into an instance of your object. This is done by creating fromJson...
Read more >
How to Parse JSON in Dart/Flutter with Code Generation ...
Tired of writing JSON parsing code by hand? Here's how to automate this with code generation and the Freezed package.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found