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

[C++] Pistache server - ModelBase::toJson prototype is not accorded with its implementation

Open CyrilleBenard opened this issue 8 years ago • 3 comments

Description

The cpp-pistache-server code does not compile anymore (regression). The generator produces two different method prototype between .cpp and .h content :

In model/ModelBase.h :

class  ModelBase
{
public:
    static nlohmann::json toJson(ModelBase const& content );
};

In model/ModelBase.cpp :

nlohmann::json ModelBase::toJson(ModelBase content )
{
    return content.toJson();
}
openapi-generator version

3.1.0-SNAPSHOT

OpenAPI declaration file content or url

All but here is a small example to facilitate ;)

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:
    get:
      operationId: getList
      description: Returns a list of artists 
      responses:
        '200':
          description: Successfully returned a list of artists
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  required:
                    - username
                  properties:
                    artist_name:
                      type: string
                    artist_genre:
                      type: string
                    albums_recorded:
                      type: integer
                    username:
                      type: string

        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                type: object
                properties:   
                  message:
                    type: string

{
  "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": {
      "get": {
        "operationId": "getList",
        "description": "Returns a list of artists",
        "responses": {
          "200": {
            "description": "Successfully returned a list of artists",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "required": [
                      "username"
                    ],
                    "properties": {
                      "artist_name": {
                        "type": "string"
                      },
                      "artist_genre": {
                        "type": "string"
                      },
                      "albums_recorded": {
                        "type": "integer"
                      },
                      "username": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            }
          },
          "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 ./openapi.yaml -g cpp-pistache-server -o .

Steps to reproduce

N/A

Related issues/PRs

N/A

Suggest a fix/enhancement

Declare the method in .cpp in the same way than in the header file

nlohmann::json ModelBase::toJson(ModelBase const & content)
{
    return content.toJson();
}


CyrilleBenard avatar Jun 28 '18 12:06 CyrilleBenard

static nlohmann::json toJson(ModelBase const& content );

I don't know if we should revert this, because once the const is added we might need to implement const methods and stuff.

etherealjoy avatar Jun 28 '18 13:06 etherealjoy

@etherealjoy I'm not sure to understand what you mean (?) The const specifier is only onto the parameter, meaning that the method will not modify the object passed in argument. No more than that. This must to be set to be compliant with its header declaration. Are you sure you do not confuse with the const onto the object (class) itself ?

CyrilleBenard avatar Jun 28 '18 15:06 CyrilleBenard

Fixed with ##497

etherealjoy avatar Jul 07 '18 13:07 etherealjoy