[Qt5][C++] pass global server as object to mustache to support parameterized and multiple servers.
Created by: basyskom-dege
Is your feature request related to a problem? Please describe.
I will give you some context to understand my problem. I am working on an parameterized and multi server implementation for the Qt5 generator. To archive this, I looked at the java generator and used similar classes. There is a ServerConfiguration class and a ServerVariable class. The first one stores the different servers for each endpoint in each API, the second one describes a variable of a server. Every ServerConfiguration has a map of <QString,ServerVariable> that maps the name of the variable to its value. Each API has a map of <QString QList<ServerConfiguration>> that maps different operationsIDs to one or more ServerConfigurations. Furthermore there is another map <QString,int> called serverIndices that maps for every operationID the default server index in the QList<ServerConfiguration> that should be used when there are multiple servers. An example server config in an API could be this:
servers:
- url: http://{server}:8080/
description: Description of the Server
variables:
server:
enum:
- 'petstore'
- 'qa-petstore'
- 'dev-petstore'
default: 'petstore'
- url: https://localhost:8080/v1
If this server object is defined for an endpoint, everything works fine. Standard server is the first one with the different hosts and you can choose to use the other server or change the default variable with suitable getters and setters.
However, the global server will only be passed as three mustache variables: {{serverPort}} {{serverHost}} {{scheme}}
Describe the solution you'd like
My question is, where do i have to change the generator code on java level so that the global server will be passed as a server object, just like the ones that are passed for each endpoint. I would like to have a {{globalServer}} object with an {{url}} {{description}} and an array of {{variables}} that each contain a {{name}} {{description}} {{defaultValue}} and an array {{enumValue}}
Describe alternatives you've considered
Right now I am parsing the global server as following:
QList<ServerConfiguration> defaultConf = QList<ServerConfiguration>{ServerConfiguration(
"{protocol}://{server}:{port}/{basePath}",
"Default Server",
QMap<QString, ServerVariable> {
{"server", ServerVariable("Default Server","{{serverHost}}",
QSet<QString>{ {"{{serverHost}}"} })},
{"port", ServerVariable("Default Port","{{serverPort}}",
QSet<QString>{ {"{{serverPort}}"} })},
{"basePath", ServerVariable("Default BasePath","{{basePathWithoutHost}}",
QSet<QString>{ {"{{basePathWithoutHost}}"} })},
{"protocol", ServerVariable("Default Protocol","{{scheme}}",
QSet<QString>{ {"{{scheme}}"} })},
})};
I will just hardcode the url and parse the three mustache variables to the missing variables.
Additional context
I found this function in AbstactCppCodegen.java on line 328f. I guess this is the function that does the magic. Should i just change this to fit my expectations?
@Override
public void preprocessOpenAPI(OpenAPI openAPI) {
URL url = URLPathUtils.getServerURL(openAPI, serverVariableOverrides());
String port = URLPathUtils.getPort(url, "");
String host = url.getHost();
String scheme = url.getProtocol();
if (!port.isEmpty()) {
this.additionalProperties.put("serverPort", port);
}
if (!host.isEmpty()) {
this.additionalProperties.put("serverHost", host);
}
if (!scheme.isEmpty()) {
this.additionalProperties.put("scheme", scheme);
}
}