Skip to content

Commit 882417f

Browse files
committed
[[BUG][C][cpp-restsdk] Missing Set.h when trying to generate from Twitter OpenAPI JSON #9969](#9969)
- Handling `std::set` in cpp-restdsk [cpp-pistache-server] taking into account a remark on this issue about cpp-pistache-server and its set management - Switching `std::vector` to `std::set` for openapi set type in cpp-pistache-server
1 parent 4e61738 commit 882417f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+9534
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
generatorName: cpp-restsdk
2+
outputDir: samples/client/petstore/cpp-restsdk/client-everything
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/issues-anytype-object-set-petstore-everything.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/cpp-rest-sdk-client
5+
additionalProperties:
6+
packageName: CppRestPetstoreClient

docs/generators/cpp-pistache-server.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3030
| ---------- | ------- |
3131
|nlohmann::json|#include <nlohmann/json.hpp>|
3232
|std::map|#include <map>|
33+
|std::set|#include <set>|
3334
|std::string|#include <string>|
3435
|std::vector|#include <vector>|
3536

docs/generators/cpp-restsdk.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3636
|HttpContent|#include "HttpContent.h"|
3737
|Object|#include "Object.h"|
3838
|std::map|#include <map>|
39+
|std::set|#include <set>|
3940
|std::string|#include <string>|
4041
|std::vector|#include <vector>|
4142
|utility::datetime|#include <cpprest/details/basic_types.h>|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
7070
/** std:map (for map) */
7171
private static final String STD_MAP = "std::map";
7272

73-
/** std:vector (for array, set) */
73+
/** std:set (for set) */
74+
private static final String STD_SET = "std::set";
75+
76+
/** std:vector (for array) */
7477
private static final String STD_VECTOR = "std::vector";
7578

7679
@Override
@@ -148,7 +151,7 @@ public CppPistacheServerCodegen() {
148151
typeMapping.put("boolean", "bool");
149152
typeMapping.put("array", STD_VECTOR);
150153
typeMapping.put("map", STD_MAP);
151-
typeMapping.put("set", STD_VECTOR);
154+
typeMapping.put("set", STD_SET);
152155
typeMapping.put("file", STD_STRING);
153156
typeMapping.put("object", NLOHMANN_JSON);
154157
typeMapping.put("binary", STD_STRING);
@@ -161,6 +164,7 @@ public CppPistacheServerCodegen() {
161164
super.importMapping = new HashMap<>();
162165
importMapping.put(STD_VECTOR, "#include <vector>");
163166
importMapping.put(STD_MAP, "#include <map>");
167+
importMapping.put(STD_SET, "#include <set>");
164168
importMapping.put(STD_STRING, "#include <string>");
165169
importMapping.put(NLOHMANN_JSON, "#include <nlohmann/json.hpp>");
166170

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppRestSdkClientCodegen.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.common.collect.ArrayListMultimap;
2121
import com.google.common.collect.Multimap;
2222
import io.swagger.v3.oas.models.Operation;
23-
import io.swagger.v3.oas.models.media.ArraySchema;
2423
import io.swagger.v3.oas.models.media.Schema;
2524
import io.swagger.v3.oas.models.responses.ApiResponse;
2625
import io.swagger.v3.oas.models.servers.Server;
@@ -161,6 +160,7 @@ public CppRestSdkClientCodegen() {
161160
typeMapping.put("long", "int64_t");
162161
typeMapping.put("boolean", "bool");
163162
typeMapping.put("array", "std::vector");
163+
typeMapping.put("set", "std::set");
164164
typeMapping.put("map", "std::map");
165165
typeMapping.put("file", "HttpContent");
166166
typeMapping.put("object", "Object");
@@ -173,6 +173,7 @@ public CppRestSdkClientCodegen() {
173173
super.importMapping = new HashMap<>();
174174
importMapping.put("std::vector", "#include <vector>");
175175
importMapping.put("std::map", "#include <map>");
176+
importMapping.put("std::set", "#include <set>");
176177
importMapping.put("std::string", "#include <string>");
177178
importMapping.put("HttpContent", "#include \"HttpContent.h\"");
178179
importMapping.put("Object", "#include \"Object.h\"");

modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-header.mustache

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <sstream>
1414
#include <vector>
1515
#include <map>
16+
#include <set>
1617

1718
namespace {{helpersNamespace}}
1819
{
@@ -82,6 +83,15 @@ namespace {{helpersNamespace}}
8283
return true;
8384
}
8485

86+
/// <summary>
87+
/// Determine if the given vector<T> only has unique elements. T must provide the == operator.
88+
/// </summary>
89+
template <typename T>
90+
bool hasOnlyUniqueItems(const std::set<T>& set)
91+
{
92+
return true;
93+
}
94+
8595
std::string toStringValue(const std::string &value);
8696
std::string toStringValue(const int32_t value);
8797
std::string toStringValue(const int64_t value);

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-header.mustache

+52
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <cpprest/json.h>
1818

1919
#include <map>
20+
#include <set>
2021
#include <vector>
2122

2223
{{#modelNamespaceDeclarations}}
@@ -52,6 +53,8 @@ public:
5253
static utility::string_t toString( const std::shared_ptr<T>& val );
5354
template <typename T>
5455
static utility::string_t toString( const std::vector<T> & val );
56+
template <typename T>
57+
static utility::string_t toString( const std::set<T> & val );
5558
5659
static web::json::value toJson( bool val );
5760
static web::json::value toJson( float val );
@@ -68,6 +71,8 @@ public:
6871
template<typename T>
6972
static web::json::value toJson( const std::vector<T>& val );
7073
template<typename T>
74+
static web::json::value toJson( const std::set<T>& val );
75+
template<typename T>
7176
static web::json::value toJson( const std::map<utility::string_t, T>& val );
7277
7378
static bool fromString( const utility::string_t& val, bool & );
@@ -85,6 +90,8 @@ public:
8590
template<typename T>
8691
static bool fromString( const utility::string_t& val, std::vector<T> & );
8792
template<typename T>
93+
static bool fromString( const utility::string_t& val, std::set<T> & );
94+
template<typename T>
8895
static bool fromString( const utility::string_t& val, std::map<utility::string_t, T> & );
8996
9097
static bool fromJson( const web::json::value& val, bool & );
@@ -102,6 +109,8 @@ public:
102109
template<typename T>
103110
static bool fromJson( const web::json::value& val, std::vector<T> & );
104111
template<typename T>
112+
static bool fromJson( const web::json::value& val, std::set<T> & );
113+
template<typename T>
105114
static bool fromJson( const web::json::value& val, std::map<utility::string_t, T> & );
106115
107116
@@ -120,6 +129,8 @@ public:
120129
template <typename T>
121130
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, const std::vector<T>& value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
122131
template <typename T>
132+
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, const std::set<T>& value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
133+
template <typename T>
123134
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, const std::map<utility::string_t, T>& value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
124135
125136
static bool fromHttpContent( std::shared_ptr<HttpContent> val, bool & );
@@ -136,6 +147,8 @@ public:
136147
template <typename T>
137148
static bool fromHttpContent( std::shared_ptr<HttpContent> val, std::vector<T> & );
138149
template <typename T>
150+
static bool fromHttpContent( std::shared_ptr<HttpContent> val, std::set<T> & );
151+
template <typename T>
139152
static bool fromHttpContent( std::shared_ptr<HttpContent> val, std::map<utility::string_t, T> & );
140153
141154
static utility::string_t toBase64( utility::string_t value );
@@ -155,6 +168,8 @@ utility::string_t ModelBase::toString( const std::shared_ptr<T>& val )
155168
}
156169
return utility::string_t(ss.str());
157170
}
171+
172+
// std::vector to string
158173
template<typename T>
159174
utility::string_t ModelBase::toString( const std::vector<T> & val )
160175
{
@@ -169,6 +184,24 @@ utility::string_t ModelBase::toString( const std::vector<T> & val )
169184
}
170185
return strArray;
171186
}
187+
188+
// std::set to string
189+
template<typename T>
190+
utility::string_t ModelBase::toString( const std::set<T> & val )
191+
{
192+
utility::string_t strArray;
193+
for ( const auto &item : val )
194+
{
195+
strArray.append( toString(item) + "," );
196+
}
197+
if (val.count() > 0)
198+
{
199+
strArray.pop_back();
200+
}
201+
return strArray;
202+
}
203+
204+
172205
template<typename T>
173206
web::json::value ModelBase::toJson( const std::shared_ptr<T>& val )
174207
{
@@ -179,6 +212,8 @@ web::json::value ModelBase::toJson( const std::shared_ptr<T>& val )
179212
}
180213
return retVal;
181214
}
215+
216+
// std::vector to json
182217
template<typename T>
183218
web::json::value ModelBase::toJson( const std::vector<T>& value )
184219
{
@@ -189,6 +224,21 @@ web::json::value ModelBase::toJson( const std::vector<T>& value )
189224
}
190225
return web::json::value::array(ret);
191226
}
227+
228+
// std::set to json
229+
template<typename T>
230+
web::json::value ModelBase::toJson( const std::set<T>& value )
231+
{
232+
// There's no protoype web::json::value::array(...) taking a std::set parameter. Converting to std::vector to get an array.
233+
std::vector<web::json::value> ret;
234+
for ( const auto& x : value )
235+
{
236+
ret.push_back( toJson(x) );
237+
}
238+
return web::json::value::array(ret);
239+
}
240+
241+
192242
template<typename T>
193243
web::json::value ModelBase::toJson( const std::map<utility::string_t, T>& val )
194244
{
@@ -279,6 +329,7 @@ std::shared_ptr<HttpContent> ModelBase::toHttpContent(const utility::string_t& n
279329
}
280330
return content;
281331
}
332+
282333
template <typename T>
283334
std::shared_ptr<HttpContent> ModelBase::toHttpContent( const utility::string_t& name, const std::vector<T>& value, const utility::string_t& contentType )
284335
{
@@ -290,6 +341,7 @@ std::shared_ptr<HttpContent> ModelBase::toHttpContent( const utility::string_t&
290341
content->setData( std::shared_ptr<std::istream>( new std::stringstream( utility::conversions::to_utf8string(json_array.serialize()) ) ) );
291342
return content;
292343
}
344+
293345
template <typename T>
294346
std::shared_ptr<HttpContent> ModelBase::toHttpContent( const utility::string_t& name, const std::map<utility::string_t, T>& value, const utility::string_t& contentType )
295347
{

modules/openapi-generator/src/test/resources/3_0/issues-anytype-object-set-petstore-everything.yaml

+31-8
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,9 @@ components:
763763
- pending
764764
- sold
765765

766-
# ---------------------------------------------------------
767-
# Properties that dedicate this configuration to this issue
768-
# ---------------------------------------------------------
766+
# -----------------------------------------------------------------------
767+
# Properties that dedicate this configuration to some issues or checkings
768+
# -----------------------------------------------------------------------
769769

770770
# https://github.com/OpenAPITools/openapi-generator/issues/2769
771771
# object property (leading to Object.h)
@@ -782,21 +782,44 @@ components:
782782
description: to help you installing your pet at home
783783

784784
# Leading to Set.h
785+
certificates:
786+
description: pedigree and other certificates
787+
type: array
788+
uniqueItems: true
789+
790+
items:
791+
type: string
792+
785793
# https://github.com/OpenAPITools/openapi-generator/issues/14234
786-
bestFriends:
787-
description: Pet best friends!
794+
vaccinationBook:
795+
description: Vaccination book of the pet
788796
type: object
789797

790798
required:
791-
- bestFriends
799+
- vaccines
792800

793801
properties:
794-
bestFriends:
802+
vaccines:
795803
type: array
796804
uniqueItems: true
797805

798806
items:
799-
type: string
807+
title: vaccine
808+
type: object
809+
810+
required:
811+
- date
812+
- boosterRequired
813+
814+
properties:
815+
date:
816+
format: date
817+
description: vaccination date
818+
819+
boosterRequired:
820+
type: boolean
821+
description: true if a booster is still needed to complete the vaccination
822+
800823

801824
xml:
802825
name: Pet
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Compiled Object files
2+
*.slo
3+
*.lo
4+
*.o
5+
*.obj
6+
7+
# Precompiled Headers
8+
*.gch
9+
*.pch
10+
11+
# Compiled Dynamic libraries
12+
*.so
13+
*.dylib
14+
*.dll
15+
16+
# Fortran module files
17+
*.mod
18+
*.smod
19+
20+
# Compiled Static libraries
21+
*.lai
22+
*.la
23+
*.a
24+
*.lib
25+
26+
# Executables
27+
*.exe
28+
*.out
29+
*.app
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md

0 commit comments

Comments
 (0)