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

[pistache-server] (C++ stub) - Optional type unknown - compile failed

Open CyrilleBenard opened this issue 8 years ago • 4 comments

Description

I tried to follow the OpenAPI V3 tutorial but as soon as my openapi file uses the "Query parameters " my generated C++ code does not compile anymore.

See

  parameters:
    - name: limit
      in: query
      description: Limits the number of items on a page
      schema:
        type: integer
    - name: offset
      in: query
      description: Specifies the page number of the artists to be displayed
      schema:
        type: integer

Compile command line and error looks like :

  g++ -c  -I./api -I./model -I./impl -Wall -g -std=c++11 -o obj/api/DefaultApi.o api/DefaultApi.cpp
  In file included from api/DefaultApi.cpp:13:0:
  api/DefaultApi.h:72:36: error: ‘Optional’ does not name a type
       virtual void artists_get(const Optional<int32_t> &limit, const Optional<int32_t> &offset, Pistache::

My g++ version is 5.4.0 running on Gnome UBuntu 16.04

Is there any g++ version requirement or something I missed ?

openapi-generator version

v3.0.0

OpenAPI declaration file content or url

The openapi file is the one posted here See Parameters (sorry, the URL seems to be not fully handled)

Command line used for generation
  openapi-generator-cli.sh generate -i ../../api-ms1/openapi.yaml -g cpp-pistache-server -c ./config.json -o .
Steps to reproduce

Just generate and compile the code

Compile line is almost the standard one :

  g++ -c  -I./api -I./model -I./impl -Wall -g -std=c++11 -o obj/api/DefaultApi.o api/DefaultApi.cpp
Related issues/PRs

n/a

Suggest a fix/enhancement

No additional information

CyrilleBenard avatar Jun 08 '18 13:06 CyrilleBenard

Seems like there is an include missing for optional.h.

stkrwork avatar Jun 09 '18 14:06 stkrwork

pr #264 fixes the Optional usage :+1: but it is not yet enough :

1)

Below a compilation result overview

Building the object file obj/api/DefaultApi.o from api/DefaultApi.cpp
g++ -c  -I./api -I./model -I./impl -I../client/client-ms2/gen-cpp/api -I../client/client-ms3/gen-cpp/api -Wall -g -std=c++11 -o obj/api/DefaultApi.o api/DefaultApi.cpp
api/DefaultApi.cpp: In member function ‘void com::bcom::amf::microservice::server::ms1::api::DefaultApi::artists_get_handler(const Pistache::Rest::Request&, Pistache::Http::ResponseWriter)’:
api/DefaultApi.cpp:62:48: error: no matching function for call to ‘com::bcom::amf::microservice::server::ms1::api::DefaultApi::artists_get(Pistache::Optional<std::__cxx11::basic_string<char> >&, Pistache::Optional<std::__cxx11::basic_string<char> >&, Pistache::Http::ResponseWriter&)’
       this->artists_get(limit, offset, response);
                                                ^
In file included from api/DefaultApi.cpp:13:0:
api/DefaultApi.h:70:18: note: candidate: virtual void com::bcom::amf::microservice::server::ms1::api::DefaultApi::artists_get(const Pistache::Optional<int>&, const Pistache::Optional<int>&, Pistache::Http::ResponseWriter&)
     virtual void artists_get(const Pistache::Optional<int32_t> &limit, const Pistache::Optional<int32_t> &offset, Pistache::Http::ResponseWriter &response) = 0;
                  ^
api/DefaultApi.h:70:18: note:   no known conversion for argument 1 from ‘Pistache::Optional<std::__cxx11::basic_string<char> >’ to ‘const Pistache::Optional<int>&’
../rules.mk:55: recipe for target 'obj/api/DefaultApi.o' failed

The prototype match with the expected types (int32_t) virtual void artists_get(const Pistache::Optional<int32_t> &limit, const Pistache::Optional<int32_t> &offset, Pistache::Http::ResponseWriter &response) = 0;
BUT function is called with a string argument :

void DefaultApi::artists_get_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {

    // Getting the query params
    auto limit = request.query().get("limit");
    auto offset = request.query().get("offset");
    
    try {
      this->artists_get(limit, offset, response);
    } catch (std::runtime_error & e) {
      //send a 400 error
      response.send(Pistache::Http::Code::Bad_Request, e.what());
      return;
    }
}

One proposal : The method artists_get_handle should convert the parameters before calling the method from the API (artists_get)

2) A regression appeared in almost all ModelBase methods declaration :

g++ -c  -I./api -I./model -I./impl -I../client/client-ms2/gen-cpp/api -I../client/client-ms3/gen-cpp/api -Wall -g -std=c++11 -o obj/api/DefaultApi.o api/DefaultApi.cpp
In file included from ./model/Inline_response_200.h:22:0,
                 from api/DefaultApi.h:29,
                 from api/DefaultApi.cpp:13:
./model/ModelBase.h:45:60: error: static member function ‘static std::__cxx11::string com::bcom::amf::microservice::server::ms1::model::ModelBase::toJson(const string&)’ cannot have cv-qualifier
     static std::string toJson(  std::string const& value ) const;

The last const at the end of the method declaration means the method will not modify the object integrity (no members will be modified) and the static keyword in the front of the method means the method does not refer to any instance but only to the class. These information are in opposition. One of them have to be selected, and the two possibilities are : static std::string toJson( std::string const& value ) ; or std::string toJson( std::string const& value ) const;

As these methods refer to conversions, there is no need to instantiate an object and so, the static solution is probably "better".

My proposal : static std::string toJson( std::string const& value ) ;

CyrilleBenard avatar Jun 12 '18 10:06 CyrilleBenard

I agree

My proposal : static std::string toJson( std::string const& value ) ;

I had this issue with my previous PR for const https://github.com/swagger-api/swagger-codegen/pull/7340

@fvarose @stkrwork Ok, it is not so small change... The object is accessed and it has to have a const specifier in the methods being invoked as well. So the changes are cascading. If you agree I will drop the const too. Right now I need to prevent the copy and the reference is working. Maybe I do this after I make the other PRs.

etherealjoy avatar Jun 12 '18 12:06 etherealjoy

We have encountered the same issue with the specification of DeepVISS

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

bocse avatar Aug 30 '18 09:08 bocse