Skip to content

Commit febd65d

Browse files
Qt5 parameterized server (#8183)
* first Commit parameterzied Server support * fixed serverconfig classes * Defautl constructor f. Config, fixed regex replace * Polosihed Templates, Added MultiServer support * Update Readme. Fixed MultiServer. Fixed def. Value * Passing global Server to mustache. Small fixes * Updated samples, fixed mustache for multi server * added prefixes, removed unused imports * added newly generated samples * missing vendorExtension in mustache. Update smaple * update doc Co-authored-by: William Cheng <wing328hk@gmail.com>
1 parent 0be3fe6 commit febd65d

File tree

34 files changed

+1002
-233
lines changed

34 files changed

+1002
-233
lines changed

docs/generators/cpp-qt5-client.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
187187
### Documentation Feature
188188
| Name | Supported | Defined By |
189189
| ---- | --------- | ---------- |
190-
|Readme||ToolingExtension
190+
|Readme||ToolingExtension
191191
|Model|✓|ToolingExtension
192192
|Api|✓|ToolingExtension
193193

@@ -204,8 +204,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
204204
|ExternalDocumentation|✓|OAS2,OAS3
205205
|Examples|✓|OAS2,OAS3
206206
|XMLStructureDefinitions|✗|OAS2,OAS3
207-
|MultiServer||OAS3
208-
|ParameterizedServer||OAS3
207+
|MultiServer||OAS3
208+
|ParameterizedServer||OAS3
209209
|ParameterStyling|✗|OAS3
210210
|Callbacks|✗|OAS3
211211
|LinkObjects|✗|OAS3

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919

2020
import com.google.common.collect.ImmutableMap.Builder;
2121
import com.samskivert.mustache.Mustache.Lambda;
22+
2223
import io.swagger.v3.oas.models.OpenAPI;
2324
import io.swagger.v3.oas.models.media.Schema;
25+
import io.swagger.v3.oas.models.servers.Server;
26+
import io.swagger.v3.oas.models.servers.ServerVariables;
27+
import io.swagger.v3.oas.models.servers.ServerVariable;
28+
import org.openapitools.codegen.CodegenServer;
29+
import org.openapitools.codegen.CodegenServerVariable;
2430
import org.apache.commons.io.FilenameUtils;
2531
import org.apache.commons.lang3.StringUtils;
2632
import org.openapitools.codegen.CodegenConfig;
@@ -327,6 +333,8 @@ public void postProcessFile(File file, String fileType) {
327333

328334
@Override
329335
public void preprocessOpenAPI(OpenAPI openAPI) {
336+
List<Server> serverList = openAPI.getServers();
337+
List<CodegenServer> CodegenServerList = new ArrayList<CodegenServer>();
330338
URL url = URLPathUtils.getServerURL(openAPI, serverVariableOverrides());
331339
String port = URLPathUtils.getPort(url, "");
332340
String host = url.getHost();
@@ -341,6 +349,28 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
341349
if (!scheme.isEmpty()) {
342350
this.additionalProperties.put("scheme", scheme);
343351
}
352+
if (!serverList.isEmpty()) {
353+
for (Server server : serverList) {
354+
CodegenServer s = new CodegenServer();
355+
s.description = server.getDescription();
356+
s.url = server.getUrl();
357+
s.variables = new ArrayList<CodegenServerVariable>();
358+
ServerVariables serverVars = server.getVariables();
359+
if(serverVars != null){
360+
serverVars.forEach((key,value) -> {
361+
CodegenServerVariable codegenServerVar= new CodegenServerVariable();
362+
ServerVariable ServerVar = value;
363+
codegenServerVar.name = key;
364+
codegenServerVar.description = ServerVar.getDescription();
365+
codegenServerVar.defaultValue = ServerVar.getDefault();
366+
codegenServerVar.enumValues = ServerVar.getEnum();
367+
s.variables.add(codegenServerVar);
368+
});
369+
}
370+
CodegenServerList.add(s);
371+
}
372+
this.vendorExtensions.put("x-codegen-globalServerList", CodegenServerList);
373+
}
344374
}
345375

346376
@Override

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.openapitools.codegen.CodegenConstants;
2222
import org.openapitools.codegen.CodegenType;
2323
import org.openapitools.codegen.SupportingFile;
24+
import org.openapitools.codegen.meta.features.DocumentationFeature;
25+
import org.openapitools.codegen.meta.features.GlobalFeature;
2426

2527
import java.io.File;
2628

@@ -35,6 +37,13 @@ public class CppQt5ClientCodegen extends CppQt5AbstractCodegen implements Codege
3537
public CppQt5ClientCodegen() {
3638
super();
3739

40+
41+
modifyFeatureSet(features -> features
42+
.includeDocumentationFeatures(DocumentationFeature.Readme)
43+
.includeGlobalFeatures(GlobalFeature.ParameterizedServer)
44+
.includeGlobalFeatures(GlobalFeature.MultiServer)
45+
);
46+
3847
// set the output folder here
3948
outputFolder = "generated-code/qt5cpp";
4049

@@ -80,6 +89,8 @@ public CppQt5ClientCodegen() {
8089
supportingFiles.add(new SupportingFile("HttpFileElement.cpp.mustache", sourceFolder, PREFIX + "HttpFileElement.cpp"));
8190
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder, PREFIX + "Object.h"));
8291
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, PREFIX + "Enum.h"));
92+
supportingFiles.add(new SupportingFile("ServerConfiguration.mustache", sourceFolder, PREFIX +"ServerConfiguration.h"));
93+
supportingFiles.add(new SupportingFile("ServerVariable.mustache", sourceFolder, PREFIX +"ServerVariable.h"));
8394
supportingFiles.add(new SupportingFile("README.mustache", "","README.md"));
8495
supportingFiles.add(new SupportingFile("CMakeLists.txt.mustache", sourceFolder, "CMakeLists.txt"));
8596
if (optionalProjectFileFlag) {
@@ -109,6 +120,8 @@ public void processOpts() {
109120
supportingFiles.add(new SupportingFile("HttpFileElement.cpp.mustache", sourceFolder, modelNamePrefix + "HttpFileElement.cpp"));
110121
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder, modelNamePrefix + "Object.h"));
111122
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, modelNamePrefix + "Enum.h"));
123+
supportingFiles.add(new SupportingFile("ServerConfiguration.mustache", sourceFolder, modelNamePrefix + "ServerConfiguration.h"));
124+
supportingFiles.add(new SupportingFile("ServerVariable.mustache", sourceFolder, modelNamePrefix + "ServerVariable.h"));
112125
supportingFiles.add(new SupportingFile("README.mustache", "","README.md"));
113126
supportingFiles.add(new SupportingFile("CMakeLists.txt.mustache", sourceFolder, "CMakeLists.txt"));
114127

modules/openapi-generator/src/main/resources/cpp-qt5-client/Project.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ HEADERS += \
2020
$${PWD}/{{prefix}}HttpRequest.h \
2121
$${PWD}/{{prefix}}Object.h \
2222
$${PWD}/{{prefix}}Enum.h \
23-
$${PWD}/{{prefix}}HttpFileElement.h
24-
23+
$${PWD}/{{prefix}}HttpFileElement.h \
24+
$${PWD}/{{prefix}}ServerConfiguration.h \
25+
$${PWD}/{{prefix}}ServerVariable.h
26+
2527
SOURCES += \
2628
# Models
2729
{{#models}}

modules/openapi-generator/src/main/resources/cpp-qt5-client/README.mustache

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,58 @@ Class | Method | HTTP request | Description
146146
{{#models}}{{#model}} - [{{classname}}]({{modelDocPath}}{{classname}}.md)
147147
{{/model}}{{/models}}
148148

149+
## Documentation for Servers
150+
151+
Parameterized Servers are supported. Define a server in the API for each endpoint with arbitrary numbers of variables:
152+
153+
```
154+
servers:
155+
- url: http://{server}:{port}/{basePath}
156+
description: Description of the Server
157+
variables:
158+
server:
159+
enum:
160+
- 'petstore'
161+
- 'qa-petstore'
162+
- 'dev-petstore'
163+
default: 'petstore'
164+
port:
165+
enum:
166+
- '3000'
167+
- '1000'
168+
default: '3000'
169+
basePath:
170+
default: v1
171+
```
172+
To change the default variable, use this function in each Api:
173+
```
174+
int setDefaultServerValue(int serverIndex,const QString &operation, const QString &variable,const QString &val);
175+
```
176+
The parameter "serverIndex" will choose a server from the server list for each endpoint. There is always at least one server with index 0. The Paramter "operation" should be the desired endpoint operationid.
177+
Variable is the name of the variable you wish to change and the value is the new default Value.
178+
The function will return -1 when the variable does not exists, -2 if value is not defined in the variable enum and -3 if the operation is not found.
179+
180+
If your endpoint has multiple server objects in the servers array, you can set the server that will be used with this function:
181+
```
182+
void setServerIndex(const QString &operation, int serverIndex);
183+
```
184+
Parameter "operation" should be your operationid. "serverIndex" is the index you want to set as your default server. The function will check if there is a server with your index.
185+
Here is an example of multiple servers in the servers array. The first server will have index 0 and the second will have index 1.
186+
```
187+
servers:
188+
- url: http://{server}:8080/
189+
description: Description of the Server
190+
variables:
191+
server:
192+
enum:
193+
- 'petstore'
194+
- 'qa-petstore'
195+
- 'dev-petstore'
196+
default: 'petstore'
197+
- url: https://localhost:8080/v1
198+
```
199+
200+
149201
## Documentation for Authorization
150202

151203
{{^authMethods}}All endpoints do not require authorization.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{{>licenseInfo}}
2+
/**
3+
* Representing a Server configuration.
4+
*/
5+
#ifndef {{prefix}}_SERVERVCONFIGURATION_H
6+
#define {{prefix}}_SERVERVCONFIGURATION_H
7+
#include <QString>
8+
#include <QMap>
9+
#include <QRegularExpression>
10+
#include "{{prefix}}ServerVariable.h"
11+
12+
{{#cppNamespaceDeclarations}}
13+
namespace {{this}} {
14+
{{/cppNamespaceDeclarations}}
15+
16+
class {{prefix}}ServerConfiguration {
17+
public:
18+
/**
19+
* @param URL A URL to the target host.
20+
* @param description A description of the host designated by the URL.
21+
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
22+
*/
23+
{{prefix}}ServerConfiguration(const QString& URL, const QString& description, const QMap<QString, {{prefix}}ServerVariable>& variables)
24+
: _description(description),
25+
_variables(variables),
26+
_URL(URL){}
27+
{{prefix}}ServerConfiguration(){}
28+
~{{prefix}}ServerConfiguration(){}
29+
30+
/**
31+
* Format URL template using given variables.
32+
*
33+
* @param variables A map between a variable name and its value.
34+
* @return Formatted URL.
35+
*/
36+
QString URL() {
37+
QString url = _URL;
38+
if(!_variables.empty()){
39+
// go through variables and replace placeholders
40+
for (auto const& v : _variables.keys()) {
41+
QString name = v;
42+
{{prefix}}ServerVariable serverVariable = _variables.value(v);
43+
QString value = serverVariable._defaultValue;
44+
45+
if (!serverVariable._enumValues.empty() && !serverVariable._enumValues.contains(value)) {
46+
throw std::runtime_error(QString("The variable " + name + " in the server URL has invalid value " + value + ".").toUtf8());
47+
}
48+
QRegularExpression regex(QString("\\{" + name + "\\}"));
49+
url = url.replace(regex, value);
50+
51+
}
52+
return url;
53+
}
54+
return url;
55+
}
56+
57+
int setDefaultValue(const QString& variable,const QString& value){
58+
if(_variables.contains(variable))
59+
return _variables[variable].setDefaultValue(value);
60+
return -1;
61+
}
62+
63+
QString _description;
64+
QMap<QString, {{prefix}}ServerVariable> _variables;
65+
QString _URL;
66+
67+
};
68+
69+
{{#cppNamespaceDeclarations}}
70+
} // namespace {{this}}
71+
{{/cppNamespaceDeclarations}}
72+
73+
#endif // {{prefix}}_SERVERVCONFIGURATION_H
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{{>licenseInfo}}
2+
/**
3+
* Representing a Server Variable for server URL template substitution.
4+
*/
5+
#ifndef {{prefix}}_SERVERVARIABLE_H
6+
#define {{prefix}}_SERVERVARIABLE_H
7+
#include <QString>
8+
#include <QSet>
9+
10+
{{#cppNamespaceDeclarations}}
11+
namespace {{this}} {
12+
{{/cppNamespaceDeclarations}}
13+
14+
class {{prefix}}ServerVariable {
15+
public:
16+
17+
/**
18+
* @param description A description for the server variable.
19+
* @param defaultValue The default value to use for substitution.
20+
* @param enumValues An enumeration of string values to be used if the substitution options are from a limited set.
21+
*/
22+
{{prefix}}ServerVariable(const QString &description, const QString &defaultValue, const QSet<QString> &enumValues)
23+
: _defaultValue(defaultValue),
24+
_description(description),
25+
_enumValues(enumValues){}
26+
27+
{{prefix}}ServerVariable(){}
28+
~{{prefix}}ServerVariable(){}
29+
30+
int setDefaultValue(const QString& value){
31+
if( _enumValues.contains(value)){
32+
_defaultValue = value;
33+
return 0;
34+
}
35+
return -2;
36+
}
37+
38+
QString getDefaultValue(){return _defaultValue;}
39+
QSet<QString> getEnumValues(){return _enumValues;}
40+
41+
42+
QString _defaultValue;
43+
QString _description;
44+
QSet<QString> _enumValues;
45+
46+
};
47+
48+
{{#cppNamespaceDeclarations}}
49+
} // namespace {{this}}
50+
{{/cppNamespaceDeclarations}}
51+
52+
#endif // {{prefix}}_SERVERVARIABLE_H

0 commit comments

Comments
 (0)