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

[pistache-server](C++ stub) - POST not handled correctly - compile failed

Open CyrilleBenard opened this issue 8 years ago • 6 comments

Description

I'm still trying to use the openAPI v3 Tutorial and I notice the openapi-generator tool seems to generate a wrong C++ code when handling a POST.

The compile command line and its stdout is :

g++ -c  -I./api -I./model -I./impl -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_post_handler(const Pistache::Rest::Request&, Pistache::Http::ResponseWriter)’:
api/DefaultApi.cpp:63:40: error: no matching function for call to ‘com::bcom::amf::microservice::server::ms1::api::DefaultApi::artists_post(com::bcom::amf::microservice::server::ms1::model::Body&, Pistache::Http::ResponseWriter&)’
       this->artists_post(body, response);
                                        ^
In file included from api/DefaultApi.cpp:13:0:
api/DefaultApi.h:67:18: note: candidate: virtual void com::bcom::amf::microservice::server::ms1::api::DefaultApi::artists_post(const std::shared_ptr<com::bcom::amf::microservice::server::ms1::model::Body>&, Pistache::Http::ResponseWriter&)
     virtual void artists_post(const std::shared_ptr<Body> &body, Pistache::Http::ResponseWriter &respons
                  ^
api/DefaultApi.h:67:18: note:   no known conversion for argument 1 from ‘com::bcom::amf::microservice::server::ms1::model::Body’ to ‘const std::shared_ptr<com::bcom::amf::microservice::server::ms1::model::Body>&’

Indeed, after having a look into the two generated files DefaultApi.h and DefaultApi.cpp I can notice that one method is called passing wrong arguments. See below.

The DefaultApi.cpp describes this ( see the line ==> this->artists_post(body, response); )

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

    // Getting the body param
    Body body;
    
    try {
      nlohmann::json request_body = nlohmann::json::parse(request.body());
      body.fromJson(request_body);
      this->artists_post(body, response);
    } catch (std::runtime_error & e) {
      //send a 400 error
      response.send(Pistache::Http::Code::Bad_Request, e.what());
      return;
    }
}

whereas the DefaultApi.h describes the prototype :

virtual void artists_post(const std::shared_ptr<Body> &body, Pistache::Http::ResponseWriter &response) = 0;

In other words, the method artists_post is called with the first argument as a Body type, whereas it should be called with a shared_ptr<Body> type. Or the opposite, I mean, may be the method call is correct and the prototype should be modified.

NOTE: The second solution is used by the swagger-codegen tool

openapi-generator version

v3.0.0

OpenAPI declaration file content or url

Based on the published openAPI V3 tutorial, I extract the short lines that generate the wrong behavior

openapi: 3.0.0
info:
  version: 1.0.0
  title: Simple API
  description: A simple API to illustrate OpenAPI concepts

servers:
  - url: https://example.io/v1

components:
  securitySchemes:
    BasicAuth:
      type: http
      scheme: basic
security:
  - BasicAuth: []

paths:
  /artists:
    post:
      description: Lets a user post a new artist
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object 
              required:
                - username
              properties:
                artist_name:
                  type: string
                artist_genre:
                  type: string
                albums_recorded:
                  type: integer
                username:
                  type: string

      responses:
        '200':
          description: Successfully created a new artist

        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                type: object
                properties:   
                  message:
                    type: string
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

config.json content is :

{
    "modelPackage"   : "com.bcom.amf.microservice.server.ms1.model",
    "apiPackage"     : "com.bcom.amf.microservice.server.ms1.api"
}

Call the openapi-generator-cli as shown previously and then compile the code, for example :

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

Described above

CyrilleBenard avatar Jun 08 '18 15:06 CyrilleBenard

I looked into it a bit.

Seems like there is an error in the mustache files. https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/pistache-server/api-impl-source.mustache https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/pistache-server/api-impl-header.mustache https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/pistache-server/api-header.mustache

void {{classname}}Impl::{{operationIdSnakeCase}}({{#allParams}}const {{{dataType}}} &{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response) {
    response.send(Pistache::Http::Code::Ok, "Do some magic\n");
}

If you adapt this code piece to this

void {{classname}}Impl::{{operationIdSnakeCase}}({{#allParams}}const {{#isPrimitiveType}}{{{dataType}}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{{baseType}}}{{/isPrimitiveType}} &{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response) {
    response.send(Pistache::Http::Code::Ok, "Do some magic\n");
}

Your problem is resolved, but then there is the issue with the arrays, as std::vector<..> changes to std::vector.

stkrwork avatar Jun 09 '18 14:06 stkrwork

Can you check if it works with the changes in the pr #264

stkrwork avatar Jun 09 '18 15:06 stkrwork

I just checked with the pr #264 and I notice nothing new :/

CyrilleBenard avatar Jun 12 '18 09:06 CyrilleBenard

Any news on the future fix ? All POST messages can't work without a fix, that means I'm totally stuck :'(

CyrilleBenard avatar Jun 28 '18 08:06 CyrilleBenard

@Schrigga said he was working on a solution, and i was really busy in the last two weeks. See #264 for reference

stkrwork avatar Jun 28 '18 10:06 stkrwork

Fixed in #497

etherealjoy avatar Jul 10 '18 14:07 etherealjoy