openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open vbs96 opened this issue 8 years ago • 8 comments

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>
  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

vbs96 avatar Aug 06 '18 14:08 vbs96

This is currently not handled. This is a map of array. But it is a small update to the mustache file. It should be something like this

ModelMapHelper::fromJson

etherealjoy avatar Aug 06 '18 17:08 etherealjoy

@etherealjoy Any way to either have this built automatically from the OpenAPI file, either using Pistache or Restbed?

I'm not sure what exactly you mean by "mustache file".

Having a manual step in an automatic build process would not be ideal, even if the update is small :)

bocse avatar Aug 06 '18 17:08 bocse

We need to add some conditions in the mustache file to handle this , so that it will be automatically generated. I could help you later in the week once I find the time.

etherealjoy avatar Aug 06 '18 17:08 etherealjoy

Thank you very much for your support, @etherealjoy

bocse avatar Aug 14 '18 07:08 bocse

The version of the DeepVISS specs that reproduces this issue is https://raw.githubusercontent.com/deepviss/deepviss-server-example/f5f6bf736b5cc82c4c99fd8f1903017e66d6bc8f/src/main/resources/deep-viss.json

Do you have any update regarding a fix?

bocse avatar Aug 30 '18 11:08 bocse

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

etherealjoy avatar Aug 30 '18 11:08 etherealjoy

Any update on this?

bocse avatar Oct 31 '18 09:10 bocse

I tested it with #1359 and it worked.

etherealjoy avatar Nov 03 '18 08:11 etherealjoy