[C++][Pistache-server] Object containing an Array - model file (.h) is not included and generated code is wrong - Compilation failed
Created by: CyrilleBenard
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.