Skip to content

[C++][Pistache-server] Array of string does not compile whereas Array of object of string does #1273

@CyrilleBenard

Description

@CyrilleBenard
Description

The generator fails in case the openapi file describes an Array of Strings whereas, if I insert a stair of object between Array and String, the generated code is correct.

The compilation error looks like :

g++ -c  -I./api -I./model -I./impl -Wall -g -std=c++11 -o obj/model/UeContext.o model/UeContext.cpp
model/UeContext.cpp: In member function ‘virtual void com::bcom::amf::microservice::server::ms1::model::UeContext::fromJson(nlohmann::json&)’:
model/UeContext.cpp:77:25: error: ‘std::__cxx11::string {aka class std::__cxx11::basic_string<char>}’ has no member named ‘fromJson’
                 newItem.fromJson(item);
                         ^
../rules.mk:61: recipe for target 'obj/model/UeContext.o' failed

It is a well known error already fixed in some others conditions. See suggest below.

NOTE1: As previously wrote, if I insert a stair between Array and String, the string type is well handled.
NOTE2: Switch between the two UeContext type I wrote inside the openapi.yaml example to reproduce the error and the correct behavior.

openapi-generator version

Current master 3.3.2-SNAPSHOT got the issue but I also used the PR #1230 to take advantage of the fix #1168

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

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

paths:
  /stair1:
    post:
      summary: blabla
      operationId: check_generation
      tags:
        - Stair1
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UeContext'
        required: true
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                type: string
        default:
          description: Unexpected error

components:
  schemas:
# Fail
    UeContext:
      type: object
      properties:
        groups:
          type: array
          items:
            $ref: '#/components/schemas/GroupId'
          minItems: 0

# Success
#    UeContext:
#      type: object
#      properties:
#        groups:
#          type: array
#          items:
#            $ref: '#/components/schemas/ObjectId'
#          minItems: 0

    ObjectId:
      type: object
      properties:
        events:
          $ref: '#/components/schemas/GroupId'

    GroupId:
      type: string
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/UeContext.o model/UeContext.cpp

Steps to reproduce

Generate & compile

Related issues/PRs
Suggest a fix/enhancement

Generated code looks like below. I inserted a #ifdef to explain which code should be generated, but I'm sure @etherealjoy already know what should be done ;)

void UeContext::fromJson(nlohmann::json& val)
{
    {
        m_Groups.clear();
        nlohmann::json jsonArray;
        if(val.find("groups") != val.end())
        {
        for( auto& item : val["groups"] )
        {
             if(item.is_null())
            {
                m_Groups.push_back( std::string() );
            }
            else
            {
                std::string newItem;
#ifdef CURRENT_GENERATION
                // This is the current code generation
                newItem.fromJson(item);
#else
                // This is what should be generated
                newItem = ModelBase::fromJson(item);
#endif
                m_Groups.push_back( newItem );
            }
         }
        }
    }
    
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions