[C++][cpp-restsdk] $ref generates codes that does not compile
Created by: knelsonmeister
Description
When using simple types (ex: integer and string) without using $ref the cpp-restsdk generates the appropriate C++ type (ex: int32_t and string_t). However, if a $ref is used to a name that refers to a simple type, then the generated C++ code has the appropriate C++ type wrapped in a shared_ptr. This causes the C++ to not compile.
Additionally, if I try to use $ref to a different file in the same directory, the codegen fails to get the schema name.
On last thing, I haven't verified it, but a colleague of mine tells me that the generated C++ code does not handle the multi-part binary content correctly. I will try to verify that later and update this issue or create a new one.
openapi-generator version
openapi-generator-cli-3.1.0.jar
OpenAPI declaration file content or url
test1a.yaml:
openapi: 3.0.0
info:
version: '1.0.0'
title: 'Test 1a'
description: 'Test 1a'
paths:
/test/{myId}:
put:
summary: Create or update ID
operationId: CreateOrUpdateId
parameters:
- name: myId
in: path
description: My ID
required: true
schema:
format: int32
type: integer
minimum: 0
- name: myName
in: path
description: My Name
required: true
schema:
type: string
responses:
'204':
description: Expected response to a valid request
default:
description: Unexpected Error
test1b.yaml:
openapi: 3.0.0
info:
version: '1.0.0'
title: 'Test 1b'
description: 'Test 1b'
paths:
/test/{myId}:
put:
summary: Create or update ID
operationId: CreateOrUpdateId
parameters:
- name: myId
in: path
description: My ID
required: true
schema:
$ref: '#/components/schemas/Uint32'
- name: myName
in: path
description: My Name
required: true
schema:
$ref: '#/components/schemas/MyString'
responses:
'204':
description: Expected response to a valid request
default:
description: Unexpected Error
components:
schemas:
Uint32:
type: integer
format: int32
minimum: 0
MyString:
type: string
test1c.yaml:
openapi: 3.0.0
info:
version: '1.0.0'
title: 'Test 1c'
description: 'Test 1c'
paths:
/test/{myId}:
put:
summary: Create or update ID
operationId: CreateOrUpdateId
parameters:
- name: myId
in: path
description: My ID
required: true
schema:
$ref: './test1c.extra.yaml#/components/schemas/Uint32'
- name: myName
in: path
description: My Name
required: true
schema:
$ref: './test1c.extra.yaml#/components/schemas/MyString'
- name: myData
in: path
description: My Data
required: true
schema:
$ref: './test1c.extra.yaml#/components/schemas/MyObject'
responses:
'204':
description: Expected response to a valid request
default:
description: Unexpected Error
test1c.extra.yaml:
openapi: 3.0.0
info:
version: '1.0.0'
title: 'Test 1c Extra'
description: 'Test 1c Extra'
components:
schemas:
Uint32:
type: integer
format: int32
minimum: 0
MyString:
type: string
MyObject:
type: object
properties:
myName:
type: string
required:
- myName
Command line used for generation
java -jar ./openapi-generator-cli.jar generate -g cpp-restsdk -i test1a.yaml -o test1a java -jar ./openapi-generator-cli.jar generate -g cpp-restsdk -i test1b.yaml -o test1b java -jar ./openapi-generator-cli.jar generate -g cpp-restsdk -i test1c.yaml -o test1c cd test1a/api g++ -c -std=c++11 -I.. -Wall -lcpprest -o test1a.so DefaultApi.cpp cd ../../test1b/api g++ -c -std=c++11 -I.. -Wall -lcpprest -o test1b.so DefaultApi.cpp
Steps to reproduce
Running commands above:
- test1a succeeds: C++ code is generated, and the C++ codes compiles
- test1b fails: C++ code is generated, but the C++ codes fails to compile
- test1c fails: Codegen fails to generate C++ code
Here is a diff of the C++ header between test1a and test1b:
diff -u test1a/api/DefaultApi.h test1b/api/DefaultApi.h
--- test1a/api/DefaultApi.h 2018-07-13 16:09:28.968686000 -0500
+++ test1b/api/DefaultApi.h 2018-07-13 16:09:29.954677000 -0500
@@ -1,6 +1,6 @@
/**
- * Test 1a
- * Test 1a
+ * Test 1b
+ * Test 1b
*
* OpenAPI spec version: 1.0.0
*
@@ -46,8 +46,8 @@
/// <param name="myId">My ID</param>
/// <param name="myName">My Name</param>
pplx::task<void> createOrUpdateId(
- int32_t myId,
- utility::string_t myName
+ std::shared_ptr<int32_t> myId,
+ std::shared_ptr<utility::string_t> myName
);
protected:
Notice above that the when the $ref is used that the codegen is wrapping the simple type in a shared_ptr. That is causing compile errors. Here is one such error:
DefaultApi.cpp: In member function \u2018pplx::task<void> org::openapitools::client::api::DefaultApi::createOrUpdateId(std::shared_ptr<int>, std::shared_ptr<std::basic_string<char> >)\u2019:
DefaultApi.cpp:56:188: error: no matching function for call to \u2018org::openapitools::client::api::ApiClient::parameterToString(std::shared_ptr<int>&)\u2019
boost::replace_all(path, utility::conversions::to_string_t("{") + utility::conversions::to_string_t("myId") + utility::conversions::to_string_t("}"), ApiClient::parameterToString(myId));
Here is line 56 of DefaultApi.cpp:
boost::replace_all(path, utility::conversions::to_string_t("{") + utility::conversions::to_string_t("myId") + utility::conversions::to_string_t("}"), ApiClient::parameterToString(myId));
In test1c, I'm seeing the following failure in the codegen tool:
[main] WARN org.openapitools.codegen.utils.ModelUtils - Failed to get the schema name: ./test1c.extra.yaml#/components/schemas/Uint32
[main] WARN org.openapitools.codegen.utils.ModelUtils - Failed to get the schema name: ./test1c.extra.yaml#/components/schemas/MyString
[main] WARN org.openapitools.codegen.utils.ModelUtils - Failed to get the schema name: ./test1c.extra.yaml#/components/schemas/MyObject
[main] WARN org.openapitools.codegen.utils.ModelUtils - Failed to get the schema name: ./test1c.extra.yaml#/components/schemas/Uint32
Exception in thread "main" java.lang.RuntimeException: Could not process operation:
Tag: class Tag {
name: default
description: null
externalDocs: null
}
Operation: CreateOrUpdateId
Resource: put /test/{myId}
Schemas: null
Exception: null
at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:934)
at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:827)
at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:460)
at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:786)
at org.openapitools.codegen.cmd.Generate.run(Generate.java:315)
at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:69)
Caused by: java.lang.NullPointerException
at org.openapitools.codegen.languages.CppRestClientCodegen.toModelName(CppRestClientCodegen.java:388)
at org.openapitools.codegen.languages.CppRestClientCodegen.toDefaultValue(CppRestClientCodegen.java:340)
at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:1639)
at org.openapitools.codegen.languages.AbstractCppCodegen.fromProperty(AbstractCppCodegen.java:181)
at org.openapitools.codegen.DefaultCodegen.fromParameter(DefaultCodegen.java:2560)
at org.openapitools.codegen.DefaultCodegen.fromOperation(DefaultCodegen.java:2251)
at org.openapitools.codegen.languages.CppRestClientCodegen.fromOperation(CppRestClientCodegen.java:234)
at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:902)
... 5 more
Note the warnings about failing to get the schema name. This should work.