Skip to content

[C++][Pistache-server] Wrong code generation : prototype does not exist #482

@CyrilleBenard

Description

@CyrilleBenard
Description

With the example I submitted below, the code generator produces a call to a constructor that does not exist

The compile error looks like :

model/ProblemDetails.cpp: In member function ‘virtual void com::bcom::amf::microservice::server::ms1::model::ProblemDetails::fromJson(nlohmann::json&)’:
model/ProblemDetails.cpp:126:64: error: no matching function for call to ‘com::bcom::amf::microservice::server::ms1::model::InvalidParam::InvalidParam(std::nullptr_t)’
                 m_InvalidParams.push_back( InvalidParam(nullptr) );
                                                                ^
In file included from model/ProblemDetails.h:25:0,
                 from model/ProblemDetails.cpp:14:
model/InvalidParam.h:41:5: note: candidate: com::bcom::amf::microservice::server::ms1::model::InvalidParam::InvalidParam()
     InvalidParam();
     ^
model/InvalidParam.h:41:5: note:   candidate expects 0 arguments, 1 provided

Indeed, the InvalidParam::InvalidParam(std::nullptr_t) method does not exist and should it ?

Extract of the code :

void ProblemDetails::fromJson(nlohmann::json& val)
{
   // ... previous code we don't care in this example

    {
        m_InvalidParams.clear();
        nlohmann::json jsonArray;
        if(val.find("invalidParams") != val.end())
        {
        for( auto& item : val["invalidParams"] )
        {
            if(item.is_null())
            {
                m_InvalidParams.push_back( InvalidParam(nullptr) );
            }
            else
            {
                InvalidParam newItem(InvalidParam());
                newItem.fromJson(item);
                m_InvalidParams.push_back( newItem );
            }
            
        }
        }
    } 
}

Why does the condition if(item.is_null()) should push a "null" object ?
Another generation code (that looks looks like this one) does not consider this kind of condition in the same way (see #446) : null object ? => does not push / save it

openapi-generator version

3.1.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Check InvalidParam instanciation
servers:
  - url: http://localhost:8080
paths:
  /Check:
    get:
      summary: Check call to an unkown constructor
      operationId: doit
      tags:
        - Check
      responses:
        '200':
          description: Everythings gonna be alright
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:    
              schema:
                type: object
                properties:   
                  message:
                    type: string
      default:
        description: unexpected error
        content:
          application/json:
            schema:
              type: object
              properties:   
                message:
                  type: string

components:
  schemas:
    Report:
      type: object
      properties:
        location:
          $ref: 'https://raw.githubusercontent.com/jdegre/5GC_APIs/master/TS29571_CommonData.yaml#/components/schemas/ProblemDetails'
{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "Check InvalidParam instanciation"
  },
  "servers": [
    {
      "url": "http://localhost:8080"
    }
  ],
  "paths": {
    "/Check": {
      "get": {
        "summary": "Check call to an unkown constructor",
        "operationId": "doit",
        "tags": [
          "Check"
        ],
        "responses": {
          "200": {
            "description": "Everythings gonna be alright",
            "headers": {
              "x-next": {
                "description": "A link to the next page of responses",
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "message": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "default": {
          "description": "unexpected error",
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "message": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Report": {
        "type": "object",
        "properties": {
          "location": {
            "$ref": "https://raw.githubusercontent.com/jdegre/5GC_APIs/master/TS29571_CommonData.yaml#/components/schemas/ProblemDetails"
          }
        }
      }
    }
  }
}
Command line used for generation

Generation :

openapi-generator-cli.sh generate -i ./openapi.yaml -g cpp-pistache-server -c ./config.json -o .

Compilation :

g++ -c  -I./api -I./model -I./impl -Wall -g -std=c++11 -o obj/model/ProblemDetails.o model/ProblemDetails.cpp
Steps to reproduce

Just generate and compile

Related issues/PRs

Not exactly the same issue but the code generation should take into account the proposal described in issue #446 (see std::shared_ptr usage)

Suggest a fix/enhancement

Do not push_back a "null object" and generalize the std::shared_ptr usage (?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions