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

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

Closed
CyrilleBenard opened this issue Jun 8, 2018 · 6 comments

Comments

@CyrilleBenard
Copy link

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

@stkrwork
Copy link
Contributor

stkrwork commented Jun 9, 2018

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
Copy link
Contributor

stkrwork commented Jun 9, 2018

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

@CyrilleBenard
Copy link
Author

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

@CyrilleBenard
Copy link
Author

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

@stkrwork
Copy link
Contributor

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

@etherealjoy
Copy link
Contributor

Fixed in #497

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants