Skip to content

Commit ba7fc23

Browse files
etherealjoywing328
authored andcommitted
[C++] [Qt5] Add initial version of File upload and download for Qt5 client (#3853)
* Add initial version of File upload and download for Qt5 client * Update after reviews * Remove unused header
1 parent 3f9d1b1 commit ba7fc23

Some content is hidden

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

70 files changed

+1435
-255
lines changed

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class CppQt5AbstractCodegen extends AbstractCppCodegen implements Codegen
2626
protected Set<String> systemIncludes = new HashSet<String>();
2727

2828
protected Set<String> nonFrameworkPrimitives = new HashSet<String>();
29-
29+
3030
public CppQt5AbstractCodegen() {
3131
super();
3232
// set modelNamePrefix as default for QHttpEngine Server
@@ -61,10 +61,10 @@ public CppQt5AbstractCodegen() {
6161
"double")
6262
);
6363
nonFrameworkPrimitives.addAll(languageSpecificPrimitives);
64-
64+
6565
foundationClasses.addAll(
6666
Arrays.asList(
67-
"QString",
67+
"QString",
6868
"QDate",
6969
"QDateTime",
7070
"QByteArray")
@@ -78,7 +78,7 @@ public CppQt5AbstractCodegen() {
7878
typeMapping.put("integer", "qint32");
7979
typeMapping.put("long", "qint64");
8080
typeMapping.put("boolean", "bool");
81-
typeMapping.put("number", "double");
81+
typeMapping.put("number", "double");
8282
typeMapping.put("array", "QList");
8383
typeMapping.put("map", "QMap");
8484
typeMapping.put("object", PREFIX + "Object");
@@ -90,8 +90,8 @@ public CppQt5AbstractCodegen() {
9090
// modifications on multiple templates)
9191
typeMapping.put("UUID", "QString");
9292
typeMapping.put("URI", "QString");
93-
typeMapping.put("file", "QIODevice");
94-
typeMapping.put("binary", "QIODevice");
93+
typeMapping.put("file", "QByteArray");
94+
typeMapping.put("binary", "QByteArray");
9595
importMapping = new HashMap<String, String>();
9696
namespaces = new HashMap<String, String>();
9797

@@ -101,7 +101,6 @@ public CppQt5AbstractCodegen() {
101101
systemIncludes.add("QDate");
102102
systemIncludes.add("QDateTime");
103103
systemIncludes.add("QByteArray");
104-
systemIncludes.add("QIODevice");
105104
}
106105
@Override
107106
public void processOpts() {
@@ -119,7 +118,7 @@ public void processOpts() {
119118
additionalProperties().put("prefix", modelNamePrefix);
120119
}
121120
}
122-
121+
123122
@Override
124123
public String toModelImport(String name) {
125124
if( name.isEmpty() ) {
@@ -140,7 +139,7 @@ public String toModelImport(String name) {
140139

141140
return "#include \"" + folder + name + ".h\"";
142141
}
143-
142+
144143
/**
145144
* Optional - type declaration. This is a String which is used by the templates to instantiate your
146145
* types. There is typically special handling for different property types
@@ -160,9 +159,9 @@ public String getTypeDeclaration(Schema p) {
160159
Schema inner = ModelUtils.getAdditionalProperties(p);
161160
return getSchemaType(p) + "<QString, " + getTypeDeclaration(inner) + ">";
162161
} else if (ModelUtils.isBinarySchema(p)) {
163-
return getSchemaType(p) + "*";
162+
return getSchemaType(p);
164163
} else if (ModelUtils.isFileSchema(p)) {
165-
return getSchemaType(p) + "*";
164+
return getSchemaType(p);
166165
}
167166
if (foundationClasses.contains(openAPIType)) {
168167
return openAPIType;
@@ -174,7 +173,7 @@ public String getTypeDeclaration(Schema p) {
174173
}
175174

176175
@Override
177-
@SuppressWarnings("rawtypes")
176+
@SuppressWarnings("rawtypes")
178177
public String toDefaultValue(Schema p) {
179178
if (ModelUtils.isBooleanSchema(p)) {
180179
return "false";
@@ -211,15 +210,15 @@ public String toDefaultValue(Schema p) {
211210
public String toModelFilename(String name) {
212211
return toModelName(name);
213212
}
214-
213+
215214
/**
216215
* Optional - OpenAPI type conversion. This is used to map OpenAPI types in a `Schema` into
217216
* either language specific types via `typeMapping` or into complex models if there is not a mapping.
218217
*
219218
* @return a string value of the type or complex model for this property
220219
*/
221220
@Override
222-
@SuppressWarnings("rawtypes")
221+
@SuppressWarnings("rawtypes")
223222
public String getSchemaType(Schema p) {
224223
String openAPIType = super.getSchemaType(p);
225224

@@ -242,7 +241,7 @@ public String getSchemaType(Schema p) {
242241
public String toVarName(String name) {
243242
// sanitize name
244243
String varName = name;
245-
varName = sanitizeName(name);
244+
varName = sanitizeName(name);
246245

247246
// if it's all uppper case, convert to lower case
248247
if (varName.matches("^[A-Z_]*$")) {
@@ -270,7 +269,7 @@ public String toParamName(String name) {
270269
public String getTypeDeclaration(String str) {
271270
return str;
272271
}
273-
272+
274273
@Override
275274
protected boolean needToImport(String type) {
276275
return StringUtils.isNotBlank(type) && !defaultIncludes.contains(type)
@@ -283,7 +282,7 @@ protected boolean needToImport(String type) {
283282
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
284283
Map<String, Object> objectMap = (Map<String, Object>) objs.get("operations");
285284
List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");
286-
285+
287286
List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports");
288287
Map<String, CodegenModel> codegenModels = new HashMap<String, CodegenModel> ();
289288
for(Object moObj : allModels) {
@@ -298,7 +297,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
298297
operation.vendorExtensions.put("returnsEnum", true);
299298
}
300299
}
301-
// Check all return parameter baseType if there is a necessity to include, include it if not
300+
// Check all return parameter baseType if there is a necessity to include, include it if not
302301
// already done
303302
if (operation.returnBaseType != null && needToImport(operation.returnBaseType)) {
304303
if(!isIncluded(operation.returnBaseType, imports)) {
@@ -308,7 +307,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
308307
List<CodegenParameter> params = new ArrayList<CodegenParameter>();
309308
if (operation.allParams != null)params.addAll(operation.allParams);
310309

311-
// Check all parameter baseType if there is a necessity to include, include it if not
310+
// Check all parameter baseType if there is a necessity to include, include it if not
312311
// already done
313312
for(CodegenParameter param : params) {
314313
if(param.isPrimitiveType && needToImport(param.baseType)) {
@@ -321,7 +320,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
321320
// We use QString to pass path params, add it to include
322321
if(!isIncluded("QString", imports)) {
323322
imports.add(createMapping("import", "QString"));
324-
}
323+
}
325324
}
326325
}
327326
if(isIncluded("QMap", imports)) {
@@ -332,7 +331,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
332331
}
333332
return objs;
334333
}
335-
334+
336335
@Override
337336
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
338337
return postProcessModelsEnum(objs);
@@ -342,18 +341,18 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
342341
public String toEnumValue(String value, String datatype) {
343342
return escapeText(value);
344343
}
345-
344+
346345
@Override
347346
public boolean isDataTypeString(String dataType) {
348347
return "QString".equals(dataType);
349348
}
350-
349+
351350
private Map<String, String> createMapping(String key, String value) {
352351
Map<String, String> customImport = new HashMap<String, String>();
353352
customImport.put(key, toModelImport(value));
354353
return customImport;
355354
}
356-
355+
357356
private boolean isIncluded(String type, List<Map<String, String>> imports) {
358357
boolean included = false;
359358
String inclStr = toModelImport(type);

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ public CppQt5ClientCodegen() {
7676
supportingFiles.add(new SupportingFile("helpers-body.mustache", sourceFolder, PREFIX + "Helpers.cpp"));
7777
supportingFiles.add(new SupportingFile("HttpRequest.h.mustache", sourceFolder, PREFIX + "HttpRequest.h"));
7878
supportingFiles.add(new SupportingFile("HttpRequest.cpp.mustache", sourceFolder, PREFIX + "HttpRequest.cpp"));
79+
supportingFiles.add(new SupportingFile("HttpFileElement.h.mustache", sourceFolder, PREFIX + "HttpFileElement.h"));
80+
supportingFiles.add(new SupportingFile("HttpFileElement.cpp.mustache", sourceFolder, PREFIX + "HttpFileElement.cpp"));
7981
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder, PREFIX + "Object.h"));
80-
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, PREFIX + "Enum.h"));
82+
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, PREFIX + "Enum.h"));
8183
if (optionalProjectFileFlag) {
8284
supportingFiles.add(new SupportingFile("Project.mustache", sourceFolder, "client.pri"));
8385
}
84-
typeMapping.put("file", PREFIX + "HttpRequestInputFileElement");
85-
typeMapping.put("binary", PREFIX +"HttpRequestInputFileElement");
86-
importMapping.put(PREFIX + "HttpRequestInputFileElement", "#include \"" + PREFIX + "HttpRequest.h\"");
86+
typeMapping.put("file", PREFIX + "HttpFileElement");
87+
importMapping.put(PREFIX + "HttpFileElement", "#include \"" + PREFIX + "HttpFileElement.h\"");
8788
}
8889

8990
@Override
@@ -95,19 +96,18 @@ public void processOpts() {
9596
} else {
9697
additionalProperties.put(CodegenConstants.OPTIONAL_PROJECT_FILE, optionalProjectFileFlag);
9798
}
98-
99+
99100
if (additionalProperties.containsKey("modelNamePrefix")) {
100101
supportingFiles.clear();
101102
supportingFiles.add(new SupportingFile("helpers-header.mustache", sourceFolder, modelNamePrefix + "Helpers.h"));
102103
supportingFiles.add(new SupportingFile("helpers-body.mustache", sourceFolder, modelNamePrefix + "Helpers.cpp"));
103104
supportingFiles.add(new SupportingFile("HttpRequest.h.mustache", sourceFolder, modelNamePrefix + "HttpRequest.h"));
104105
supportingFiles.add(new SupportingFile("HttpRequest.cpp.mustache", sourceFolder, modelNamePrefix + "HttpRequest.cpp"));
105106
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder, modelNamePrefix + "Object.h"));
106-
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, modelNamePrefix + "Enum.h"));
107+
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, modelNamePrefix + "Enum.h"));
107108

108-
typeMapping.put("file", modelNamePrefix + "HttpRequestInputFileElement");
109-
typeMapping.put("binary", modelNamePrefix + "HttpRequestInputFileElement");
110-
importMapping.put(modelNamePrefix + "HttpRequestInputFileElement", "#include \"" + modelNamePrefix + "HttpRequest.h\"");
109+
typeMapping.put("file", modelNamePrefix + "HttpFileElement");
110+
importMapping.put(modelNamePrefix + "HttpFileElement", "#include \"" + modelNamePrefix + "HttpFileElement.h\"");
111111
if (optionalProjectFileFlag) {
112112
supportingFiles.add(new SupportingFile("Project.mustache", sourceFolder, modelNamePrefix + "client.pri"));
113113
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public CppQt5QHttpEngineServerCodegen() {
7676
apiTemplateFiles.put(
7777
"apirequest.h.mustache", // the template to use
7878
".h"); // the extension for each file to write
79-
79+
8080
apiTemplateFiles.put(
8181
"apirequest.cpp.mustache", // the template to use
8282
".cpp"); // the extension for each file to write
@@ -86,11 +86,13 @@ public CppQt5QHttpEngineServerCodegen() {
8686
* will use the resource stream to attempt to read the templates.
8787
*/
8888
embeddedTemplateDir = templateDir = "cpp-qt5-qhttpengine-server";
89-
89+
9090
supportingFiles.add(new SupportingFile("helpers-header.mustache", sourceFolder + MODEL_DIR, PREFIX + "Helpers.h"));
9191
supportingFiles.add(new SupportingFile("helpers-body.mustache", sourceFolder + MODEL_DIR, PREFIX + "Helpers.cpp"));
92-
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder + MODEL_DIR, PREFIX + "Object.h"));
92+
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder + MODEL_DIR, PREFIX + "Object.h"));
9393
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder + MODEL_DIR, PREFIX + "Enum.h"));
94+
supportingFiles.add(new SupportingFile("HttpFileElement.h.mustache", sourceFolder + MODEL_DIR, PREFIX + "HttpFileElement.h"));
95+
supportingFiles.add(new SupportingFile("HttpFileElement.cpp.mustache", sourceFolder + MODEL_DIR, PREFIX + "HttpFileElement.cpp"));
9496
supportingFiles.add(new SupportingFile("apirouter.h.mustache", sourceFolder + APIHANDLER_DIR, PREFIX + "ApiRouter.h"));
9597
supportingFiles.add(new SupportingFile("apirouter.cpp.mustache", sourceFolder + APIHANDLER_DIR, PREFIX + "ApiRouter.cpp"));
9698

@@ -102,6 +104,8 @@ public CppQt5QHttpEngineServerCodegen() {
102104
supportingFiles.add(new SupportingFile("CMakeLists.txt.mustache", sourceFolder, "CMakeLists.txt"));
103105
supportingFiles.add(new SupportingFile("Dockerfile.mustache", sourceFolder, "Dockerfile"));
104106
supportingFiles.add(new SupportingFile("LICENSE.txt.mustache", sourceFolder, "LICENSE.txt"));
107+
typeMapping.put("file", PREFIX + "HttpFileElement");
108+
importMapping.put(PREFIX + "HttpFileElement", "#include \"" + PREFIX + "HttpFileElement.h\"");
105109

106110
}
107111

@@ -115,16 +119,21 @@ public void processOpts() {
115119
supportingFiles.add(new SupportingFile("helpers-body.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "Helpers.cpp"));
116120
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "Object.h"));
117121
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "Enum.h"));
122+
supportingFiles.add(new SupportingFile("HttpFileElement.h.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "HttpFileElement.h"));
123+
supportingFiles.add(new SupportingFile("HttpFileElement.cpp.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "HttpFileElement.cpp"));
118124
supportingFiles.add(new SupportingFile("apirouter.h.mustache", sourceFolder + APIHANDLER_DIR, modelNamePrefix + "ApiRouter.h"));
119-
supportingFiles.add(new SupportingFile("apirouter.cpp.mustache", sourceFolder + APIHANDLER_DIR, modelNamePrefix + "ApiRouter.cpp"));
120-
125+
supportingFiles.add(new SupportingFile("apirouter.cpp.mustache", sourceFolder + APIHANDLER_DIR, modelNamePrefix + "ApiRouter.cpp"));
126+
127+
121128
supportingFiles.add(new SupportingFile("main.cpp.mustache", sourceFolder + SRC_DIR, "main.cpp"));
122129
supportingFiles.add(new SupportingFile("src-CMakeLists.txt.mustache", sourceFolder + SRC_DIR, "CMakeLists.txt"));
123130
supportingFiles.add(new SupportingFile("README.md.mustache", sourceFolder, "README.MD"));
124131
supportingFiles.add(new SupportingFile("Makefile.mustache", sourceFolder, "Makefile"));
125132
supportingFiles.add(new SupportingFile("CMakeLists.txt.mustache", sourceFolder, "CMakeLists.txt"));
126133
supportingFiles.add(new SupportingFile("Dockerfile.mustache", sourceFolder, "Dockerfile"));
127134
supportingFiles.add(new SupportingFile("LICENSE.txt.mustache", sourceFolder, "LICENSE.txt"));
135+
typeMapping.put("file", modelNamePrefix + "HttpFileElement");
136+
importMapping.put(modelNamePrefix + "HttpFileElement", "#include \"" + modelNamePrefix + "HttpFileElement.h\"");
128137
}
129138
}
130139

@@ -160,7 +169,7 @@ public String getName() {
160169
public String getHelp() {
161170
return "Generates a Qt5 C++ Server using the QHTTPEngine HTTP Library.";
162171
}
163-
172+
164173
/**
165174
* Location to write model files. You can use the modelPackage() as defined when the class is
166175
* instantiated
@@ -182,7 +191,7 @@ public String apiFileFolder() {
182191
private String requestFileFolder() {
183192
return outputFolder + "/" + sourceFolder + APIREQUEST_DIR + "/" + apiPackage().replace("::", File.separator);
184193
}
185-
194+
186195
@Override
187196
public String apiFilename(String templateName, String tag) {
188197
String result = super.apiFilename(templateName, tag);
@@ -193,7 +202,7 @@ public String apiFilename(String templateName, String tag) {
193202
}
194203
return result;
195204
}
196-
205+
197206
@Override
198207
public String toApiFilename(String name) {
199208
return modelNamePrefix + sanitizeName(camelize(name)) + "ApiHandler";

0 commit comments

Comments
 (0)