[C++][Pistache-server] OpenApi properties starting with a number are not well handled
Created by: CyrilleBenard
Description
If the OpenApi file (YAML or JSON) defines a property name starting with a number, for example 5gItem, the generator produces a class member named m_5gItem (that's OK) BUT a method named 5gItemIsSet(). This syntax is not allowed in almost all languages, not only C++. No symbol names should start with a number.
One workaround would be to avoid the OpenApi property name starting with a number but I'm not sure of that. I read nothing in the standard about this constrain and I'm not convince with this. This wouldn't be my proposal.
As far as I know, the Java generator (lang : jaxrs-resteasy) uses the prefix underscore on all the members that start with a number. In C++ there is no issue on the members name 'cause they are prefixed with the characters 'm_' (for member). The issue scope is on the associated access method <property name>IsSet(). The accessors are not impacted because they are named get<property name> and set<property name>
Compile error looks like :
In file included from ./model/Content.h:24:0,
from api/CheckApi.h:29,
from api/CheckApi.cpp:13:
./model/_5gmmCapability.h:74:10: error: expected unqualified-id before numeric constant
bool 5gItemIsSet() const;
^
Additional related issue : In case an OpenApi schema is defined also with a name starting with a number (under components/schemas), the pretty same issue is also generated :
- The protection against circular headers inclusions is impacted (see #ifndef xxxxx)
- The class name is generated with a number in the first position
To reproduce this current issue part, you can use the below yaml/json file after modifying the schema name (and reference) _5gmmCapability in 5gmmCapability
Errors look like :
In file included from ./model/Content.h:24:0,
from ./api/CheckApi.h:29,
from impl/CheckApiImpl.h:28,
from impl/CheckApiImpl.cpp:13:
./model/5gmmCapability.h:18:9: error: macro names must be identifiers
#ifndef 5gmmCapability_H_
^
In file included from api/CheckApi.h:29:0,
from api/CheckApi.cpp:13:
./model/Content.h:58:5: error: expected unqualified-id before numeric constant
5gmmCapability getType() const;
^
./model/Content.h:59:18: error: expected identifier before numeric constant
void setType(5gmmCapability const& value);
^
openapi-generator version
branch pr/497
OpenAPI declaration file content or url
openapi: "3.0.0"
info:
version: 1.0.0
title: Check 5gmmCapability
servers:
- url: http://localhost:8080
paths:
/Check5gmmCapability:
get:
summary: Check property beginning with a number
operationId: list
tags:
- Check
responses:
'200':
description: Everythings gonna be alright
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Content"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Content"
components:
schemas:
_5gmmCapability:
type: object
properties:
item1:
type: string
item2:
type: integer
5gItem:
type: integer
Content:
type: object
properties:
type:
$ref: '#/components/schemas/_5gmmCapability'
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Check 5gmmCapability"
},
"servers": [
{
"url": "http://localhost:8080"
}
],
"paths": {
"/Check5gmmCapability": {
"get": {
"summary": "Check property beginning with a number",
"operationId": "list",
"tags": [
"Check"
],
"responses": {
"200": {
"description": "Everythings gonna be alright",
"headers": {
"x-next": {
"description": "A link to the next page of responses",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Content"
}
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Content"
}
}
}
}
}
}
},
"components": {
"schemas": {
"_5gmmCapability": {
"type": "object",
"properties": {
"item1": {
"type": "string"
},
"item2": {
"type": "integer"
},
"5gItem": {
"type": "integer"
}
}
},
"Content": {
"type": "object",
"properties": {
"type": {
"$ref": "#/components/schemas/_5gmmCapability"
}
}
}
}
}
}
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/api/CheckApi.o api/CheckApi.cpp
Steps to reproduce
Generate & compile has previously described
Related issues/PRs
N/A
Suggest a fix/enhancement
For all the access methods <symbol name>IsSet() one solution should be to rename them into a more "classical" way : Is<symbol name>Set() but I'm not really convinced (>_<)
Another solution would be to add the prefix underscore (like in Java) to all class/method/symbol that would have been generated with a number as a first character. It's not so elegant to read this kind of symbol name inside the code, but this situation may be encounter not so often.