Skip to content

Commit eb60626

Browse files
jlengrandwelshm
authored andcommitted
JetBrains HTTP Client - Adds support for query and header params and env file (OpenAPITools#18844)
* Adds basic support for query params. * Need to parameterize them * Need to add tests * Need to add option to skip, maybe * Need to add support for header params too * Parameterizes query param values * Need to add tests * Need to add option to skip, maybe * Need to add support for header params too * Remove extra empty line * Adds support for header params * Also fixes extra end of line bug. * Fixing failing test * Adding tests for query params * Adding tests for header params * Adding basic support for env file * Add support for env file for path variables and custom header variables * TODO : Add tests * Adding tests for env generation * Adding generated test files * Fix namefile
1 parent a106437 commit eb60626

File tree

72 files changed

+16548
-296
lines changed

Some content is hidden

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

72 files changed

+16548
-296
lines changed

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.io.IOException;
3232
import java.io.Writer;
3333
import java.util.*;
34+
import java.util.regex.Matcher;
35+
import java.util.regex.Pattern;
3436

3537
import org.openapitools.codegen.meta.GeneratorMetadata;
3638
import org.openapitools.codegen.meta.Stability;
@@ -66,6 +68,9 @@ public class JetbrainsHttpClientClientCodegen extends DefaultCodegen implements
6668

6769
public List<String> customHeaders = new ArrayList<>();
6870

71+
// A map is nice, because that way I easily override variables across APIs, for pagination for example. This should add nice defaults
72+
private final Map<String, Object> customVariables = new HashMap<>();
73+
6974

7075
public CodegenType getTag() {
7176
return CodegenType.CLIENT;
@@ -96,6 +101,7 @@ public JetbrainsHttpClientClientCodegen() {
96101
embeddedTemplateDir = templateDir = "jetbrains-http-client";
97102
apiPackage = "Apis";
98103
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
104+
supportingFiles.add(new SupportingFile("http-client.template.env.mustache", "Apis", "http-client.template.env.json"));
99105

100106

101107
cliOptions.clear();
@@ -116,6 +122,14 @@ public void processOpts() {
116122
if (additionalProperties.containsKey(CUSTOM_HEADERS)) {
117123
customHeaders = Arrays.asList(additionalProperties.get(CUSTOM_HEADERS).toString().split("&"));
118124
}
125+
126+
bodyVariables.forEach(variable -> customVariables.put(variable, ""));
127+
for(String header: customHeaders) {
128+
List<String> variables = extractDoubleCurlyBraces(header);
129+
if(!variables.isEmpty()) {
130+
variables.forEach(v -> customVariables.put(v, ""));
131+
}
132+
}
119133
}
120134

121135
@Override
@@ -152,9 +166,17 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
152166
codegenOperation.vendorExtensions.put("customHeaders", customHeaders);
153167
}
154168
}
169+
155170
return results;
156171
}
157172

173+
@Override
174+
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
175+
var variables = new ArrayList<>(customVariables.keySet());
176+
objs.put("vendorExtensionsVariables", variables);
177+
return objs;
178+
}
179+
158180
List<RequestItem> getRequests(CodegenOperation codegenOperation) {
159181
List<RequestItem> items = new ArrayList<>();
160182

@@ -195,10 +217,42 @@ List<RequestItem> getRequests(CodegenOperation codegenOperation) {
195217
items.add(new RequestItem(codegenOperation.summary, null));
196218
}
197219

220+
codegenOperation.headerParams.forEach(param -> customVariables.put(param.baseName, ""));
221+
codegenOperation.queryParams.forEach(param -> customVariables.put(param.paramName, ""));
222+
223+
// I also need to grab the parameters from the path
224+
List<String> pathVariables = extractSingleCurlyBraces(codegenOperation.path);
225+
pathVariables.forEach(pv -> customVariables.put(pv, ""));
226+
198227
// Handling custom variables now
199228
return handleCustomVariablesInRequests(items);
200229
}
201230

231+
public static List<String> extractDoubleCurlyBraces(String input) {
232+
List<String> result = new ArrayList<>();
233+
Pattern pattern = Pattern.compile("\\{\\{([^}]+)\\}\\}");
234+
Matcher matcher = pattern.matcher(input);
235+
236+
while (matcher.find()) {
237+
result.add(matcher.group(1));
238+
}
239+
240+
return result;
241+
}
242+
243+
public static List<String> extractSingleCurlyBraces(String input) {
244+
List<String> result = new ArrayList<>();
245+
Pattern pattern = Pattern.compile("\\{([^}]+)\\}");
246+
Matcher matcher = pattern.matcher(input);
247+
248+
while (matcher.find()) {
249+
result.add(matcher.group(1));
250+
}
251+
252+
return result;
253+
}
254+
255+
202256
private List<RequestItem> handleCustomVariablesInRequests(List<RequestItem> items) {
203257
if (!bodyVariables.isEmpty()) {
204258
for (var item : items) {
@@ -220,7 +274,7 @@ private List<RequestItem> handleCustomVariablesInRequests(List<RequestItem> item
220274
public void postProcess() {
221275
System.out.println("##########################################################################################");
222276
System.out.println("# Thanks for using OpenAPI Generator. #");
223-
System.out.println("# Please consider donation to help us maintain this project \uD83D\uDE4F #");
277+
System.out.println("# Please consider donation to help us maintain this project \uD83D\uDE4F #");
224278
System.out.println("# https://opencollective.com/openapi_generator/donate #");
225279
System.out.println("# #");
226280
System.out.println("# This generator was written by Julien Lengrand-Lambert (https://github.com/jlengrand) #");

modules/openapi-generator/src/main/resources/jetbrains-http-client/api.mustache

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
{{#vendorExtensions.requests}}
66
### {{#summary}}{{summary}}{{/summary}}
77
## {{name}}
8-
{{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}{{#authMethods}}{{#isKeyInQuery}}?{{keyParamName}}={{#lambda.doubleMustache}}{queryKey}{{/lambda.doubleMustache}}{{/isKeyInQuery}}{{/authMethods}}
8+
{{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}{{>queryParams}}
99
{{#consumes}}
1010
Content-Type: {{{mediaType}}}
1111
{{/consumes}}
1212
{{#produces}}
1313
Accept: {{{mediaType}}}
1414
{{/produces}}
15+
{{#headerParams}}
16+
{{baseName}}: {{=<% %>=}}{{<%paramName%>}}<%={{ }}=%>
17+
{{/headerParams}}
1518
{{#vendorExtensions.customHeaders}}
1619
{{{.}}}
1720
{{/vendorExtensions.customHeaders}}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dev": {
3+
{{#vendorExtensionsVariables}}
4+
"{{.}}" : ""{{^-last}},{{/-last}}
5+
{{/vendorExtensionsVariables}}
6+
}
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{! Case where there is no query params, auth does everything}}{{^queryParams}}{{#authMethods}}{{#isKeyInQuery}}?{{keyParamName}}={{#lambda.doubleMustache}}{queryKey}{{/lambda.doubleMustache}}{{/isKeyInQuery}}{{/authMethods}}{{/queryParams}}{{#queryParams}}{{! If there are query params, auth has no logic}}{{#-first}}?{{/-first}}{{paramName}}={{=<% %>=}}{{<%paramName%>}}<%={{ }}=%>{{^-last}}&{{/-last}}{{#-last}}{{#authMethods}}{{#isKeyInQuery}}&{{keyParamName}}={{#lambda.doubleMustache}}{queryKey}{{/lambda.doubleMustache}}{{/isKeyInQuery}}{{/authMethods}}{{/-last}}{{/queryParams}}

0 commit comments

Comments
 (0)