Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #446
Closed
Open
Issue created Jul 03, 2018 by Administrator@rootContributor

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

Created by: CyrilleBenard

Description

The C++ generated code does not compile and I don't think it is localized to the fromJson method but more "global".

One of the generated code looks like below :

void UserLocation::fromJson(nlohmann::json& val)
{
    if(val.find("eutraLocation") != val.end())
    {
        if(!val["eutraLocation"].is_null())
        {
            EutraLocation newItem(EutraLocation());
            newItem.fromJson(val["eutraLocation"]);
            setEutraLocation( newItem );
        }
        
    }

    // others lines are generated below but we don't care for this example
    // ...
}

The error produced by the compiler looks like :

model/UserLocation.cpp: In member function ‘virtual void com::bcom::amf::microservice::server::ms1::model::UserLocation::fromJson(nlohmann::json&)’:
model/UserLocation.cpp:69:21: error: request for member ‘fromJson’ in ‘com::bcom::amf::microservice::server::ms1::model::newItem’, which is of non-class type ‘com::bcom::amf::microservice::server::ms1::model::EutraLocation(com::bcom::amf::microservice::server::ms1::model::EutraLocation (*)())’
             newItem.fromJson(val["eutraLocation"]);

The compiler misunderstand what is expected with the explicit call of the constructor and I think this is not "allowed" to call the constructor like that (explicit call without any parameter).

I'm pretty sure the wanted code was close to that but not fully generated. See my proposal below

openapi-generator version

3.1.0-SNAPSHOT

OpenAPI declaration file content or url

NOTE: One external reference is embedded (but accessible)

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Check fromJson
servers:
  - url: http://localhost:8080
paths:
  /Check:
    get:
      summary: Check fromJson issue - wrong newItem instanciation
      operationId: doit
      tags:
        - Check
      responses:
        '200':
          description: Every things 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/UserLocation'    
{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "Check fromJson"
  },
  "servers": [
    {
      "url": "http://localhost:8080"
    }
  ],
  "paths": {
    "/Check": {
      "get": {
        "summary": "Check fromJson issue - wrong newItem instanciation",
        "operationId": "doit",
        "tags": [
          "Check"
        ],
        "responses": {
          "200": {
            "description": "Every things 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/UserLocation"
          }
        }
      }
    }
  }
}
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/UserLocation.o model/UserLocation.cpp
Steps to reproduce

N/A

Related issues/PRs

N/A

Suggest a fix/enhancement

Unfortunately, my proposal is a C++ fix and not a openapi-generator fix. I mean, I suggest a modification from a C++ developer view not from an openapi-generator developer point of view. One day may be ...

void UserLocation::fromJson(nlohmann::json& val)
{
    if(val.find("eutraLocation") != val.end())
    {
        if(!val["eutraLocation"].is_null())
        {
            std::shared_ptr<EutraLocation> newItem( new EutraLocation() );
            newItem.fromJson(val["eutraLocation"]);
            setEutraLocation( newItem );
        }
    }

    // others lines are generated below but we don't care for this example
    // ...
}

or "better" :

void UserLocation::fromJson(nlohmann::json& val)
{
    if(val.find("eutraLocation") != val.end())
    {
        if(!val["eutraLocation"].is_null())
        {
            std::shared_ptr<EutraLocation> newItem( std::make_shared<EutraLocation>() );
            newItem.fromJson(val["eutraLocation"]);
            setEutraLocation( newItem );
        }
    }

    // others lines are generated below but we don't care for this example
    // ...
}

Obviously, it has a chain reaction 'cause the setEutraLocation method must take a std::shared_ptr in argument and the current class have also to define a std::shared_ptr member (that will be set), etc ...

Assignee
Assign to
Time tracking