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

[C++][Pistache-server] Object containing an Array - model file (.h) is not included and generated code is wrong - Compilation failed #1572

Closed
CyrilleBenard opened this issue Nov 29, 2018 · 7 comments
Milestone

Comments

@CyrilleBenard
Copy link

Description

When the YAML file describes the use of an array inside an object there are two issues :

  • One model file is not included (whereas it as been well generated)
    In the current example see the lack of #include "EpsBearerId.h" inside the file AssignEbi.h

  • The generated code below is wrong because the ::push_back method can't handle an json object

void AssignEbi::fromJson(const nlohmann::json& val)
{
    {
        m_ReleasedEbiList.clear();
        if(val.find("releasedEbiList") != val.end())
        {
            for( auto& item : val["releasedEbiList"] )
            {
                m_ReleasedEbiList.push_back(item);
                
            }
        }
    }
}

It should generate something like this :

void AssignEbi::fromJson(const nlohmann::json& val)
{
    {
        m_ReleasedEbiList.clear();
        if(val.find("releasedEbiList") != val.end())
        {
            for( auto& item : val["releasedEbiList"] )
            {
                if(item.is_null())
                {
                    m_ReleasedEbiList.push_back(EpsBearerId());
                }
                else
                {
                    EpsBearerId newItem ;
                    newItem.fromJson(item);
                    m_ReleasedEbiList.push_back(newItem);
                }
            }
        }
    }
}
openapi-generator version

Current master 3.3.4-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: Check generation of array inside object
  description: Internal ref filename is check_object_containing_array.yaml 

servers:
  - url: http://localhost:8080

paths:
  /ue-contexts/{ueContextId}/assignEbi:
    post:
      summary: Namf_Communication EBI Assignment service Operation
      tags:
        - EBI Assignment
      operationId: EBIAssignment
      parameters:
        - name: ueContextId
          in: path
          description: UE Context Identifier
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AssignEbi'
        required: true
      responses:
        '200':
          description: EBI Assignment successfully performed.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Content'
        default:
          description: Unexpected error

components:
  schemas:
    Content:
      type: string

    AssignEbi:
      type: object
      properties:
        releasedEbiList:
          type: array
          items:
            $ref: '#/components/schemas/EpsBearerId'
          minItems: 0

#
# Generated code is also wrong with this below definition
#
#    AssignEbi:
#      type: array
#      items:
#        $ref: '#/components/schemas/EpsBearerId'
#      minItems: 0


    EpsBearerId:
      $ref: '#/components/schemas/Uinteger'


    Uinteger:
      type: integer
      minimum: 0
Command line used for generation

Generate :

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

Compile :

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

Generate & compile

Related issues/PRs

N/A

Suggest a fix/enhancement

I wrote a proposal of generated code above.

@CyrilleBenard
Copy link
Author

Something remarkable is that I got the exact same issues (lack of include and wrong code) when using the generator cpp-restsdk

@grmcdorman
Copy link

grmcdorman commented Dec 3, 2018

This OpenAPI declaration:

{
  "swagger": "2.0",
  "info": {
    "description": "Test API.",
    "title": "Test",
    "version": "1"
  },
  "paths": {
    "/demo": {
      "get": {
        "tags": [
          "demo"
        ],
        "operationId": "demo",
        "summary": "Test C++ code gen",
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "Successful operation",
            "schema": {
              "$ref": "#/definitions/DemoArray"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "DemoArray": {
      "description": "A list of data",
      "type": "array",
      "items": {
        "type": "string",
        "example": "142aee4a50c03168ffb2d957b158229a"
      }
    }
  }
}

has major problems with cpp-restsdk (uncompilable code generated) as well as appearing to generate the same invalid code as the initial bug report.

For C++, the model header contains this:

class  DemoArray
    : public null<utility::string_t>

which is syntatically invalid.

It appears to be related to this mustache code:

class {{declspec}} {{classname}}
    : public {{#parent}}{{{parent}}}{{/parent}}{{^parent}}ModelBase{{/parent}}

which apparently evaluates #parent to true when it is null instead of the expected false.

EDIT: Fixed code quoting.

@grmcdorman
Copy link

Further investigation: It appears that the cpp-restsdk does not support at all:

  • As specified above, the base class is incorrect; this is in part because CppRestSdkCodegen.java is missing initialization of the instantiationTypes map
  • The code generation also needs the full property type for the array items to generate correct code.
  • The model-header.mustache needs to include ModelBase
  • The model-source.mustache needs to handle arrays

The attached diff includes these fixes, plus support for binary ("string"/'binary") and byte array ("string"/"byte") body data.
diff1.txt

@etherealjoy
Copy link
Contributor

@grmcdorman @CyrilleBenard
For pistache it is already solved on latest master branch.
Please open a different issue for cpprest-sdk, it is lacking quite a bit in such features.

@etherealjoy etherealjoy added this to the 4.0.0 milestone Jan 6, 2019
@grmcdorman
Copy link

Will do when I get appropriate details from my manager. Have also made very extensive changes to the C++ code generation:

  • Support for developer-provided custom content-type encoding/decoding (e.g. BSON or msgpack)
  • Support for reusing the HTTP client connection (current code reopens the connection every time)
  • Support for using std::chrono::system_clock::time_point for date/time values instead of utility::datetime
  • Support for byte array bodies
  • Support for returning API responses with the HTTP response as well as the body (if any)
  • Inline doxygen-style documentation for a lot of methods
  • Some cleanup of generated code, notably using _XPLATSTR instead of converting from char* to the platform string at runtime

@tnmtechnologies
Copy link
Contributor

@CyrilleBenard This workaround #1050 (comment) should work.

@wing328 wing328 modified the milestones: 4.0.0, 4.0.1 May 13, 2019
@wing328 wing328 modified the milestones: 4.0.1, 4.0.2 May 31, 2019
@wing328 wing328 modified the milestones: 4.0.2, 4.0.3 Jun 20, 2019
@wing328 wing328 modified the milestones: 4.0.3, 4.1.0 Jul 9, 2019
@wing328 wing328 modified the milestones: 4.1.0, 4.1.1 Aug 9, 2019
@wing328 wing328 modified the milestones: 4.1.1, 4.1.2 Aug 26, 2019
@wing328 wing328 modified the milestones: 4.1.2, 4.1.3 Sep 11, 2019
@wing328 wing328 modified the milestones: 4.1.3, 4.2.0 Oct 4, 2019
@wing328 wing328 removed this from the 4.2.0 milestone Oct 30, 2019
@etherealjoy
Copy link
Contributor

@CyrilleBenard
This issue is not reproducible since 4.3.1
@grmcdorman
Your issue is not reproducible in 4.3.1/master.

I tested both your specs again on master and everything is fine.

Please open a new issue when there are additional bugs.

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

5 participants