Skip to content

Commit

Permalink
JetBrains HTTP Client - Adds support for query and header params and …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jlengrand authored and welshm committed Jun 5, 2024
1 parent a106437 commit eb60626
Show file tree
Hide file tree
Showing 72 changed files with 16,548 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.io.IOException;
import java.io.Writer;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
Expand Down Expand Up @@ -66,6 +68,9 @@ public class JetbrainsHttpClientClientCodegen extends DefaultCodegen implements

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

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


public CodegenType getTag() {
return CodegenType.CLIENT;
Expand Down Expand Up @@ -96,6 +101,7 @@ public JetbrainsHttpClientClientCodegen() {
embeddedTemplateDir = templateDir = "jetbrains-http-client";
apiPackage = "Apis";
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("http-client.template.env.mustache", "Apis", "http-client.template.env.json"));


cliOptions.clear();
Expand All @@ -116,6 +122,14 @@ public void processOpts() {
if (additionalProperties.containsKey(CUSTOM_HEADERS)) {
customHeaders = Arrays.asList(additionalProperties.get(CUSTOM_HEADERS).toString().split("&"));
}

bodyVariables.forEach(variable -> customVariables.put(variable, ""));
for(String header: customHeaders) {
List<String> variables = extractDoubleCurlyBraces(header);
if(!variables.isEmpty()) {
variables.forEach(v -> customVariables.put(v, ""));
}
}
}

@Override
Expand Down Expand Up @@ -152,9 +166,17 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
codegenOperation.vendorExtensions.put("customHeaders", customHeaders);
}
}

return results;
}

@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
var variables = new ArrayList<>(customVariables.keySet());
objs.put("vendorExtensionsVariables", variables);
return objs;
}

List<RequestItem> getRequests(CodegenOperation codegenOperation) {
List<RequestItem> items = new ArrayList<>();

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

codegenOperation.headerParams.forEach(param -> customVariables.put(param.baseName, ""));
codegenOperation.queryParams.forEach(param -> customVariables.put(param.paramName, ""));

// I also need to grab the parameters from the path
List<String> pathVariables = extractSingleCurlyBraces(codegenOperation.path);
pathVariables.forEach(pv -> customVariables.put(pv, ""));

// Handling custom variables now
return handleCustomVariablesInRequests(items);
}

public static List<String> extractDoubleCurlyBraces(String input) {
List<String> result = new ArrayList<>();
Pattern pattern = Pattern.compile("\\{\\{([^}]+)\\}\\}");
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
result.add(matcher.group(1));
}

return result;
}

public static List<String> extractSingleCurlyBraces(String input) {
List<String> result = new ArrayList<>();
Pattern pattern = Pattern.compile("\\{([^}]+)\\}");
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
result.add(matcher.group(1));
}

return result;
}


private List<RequestItem> handleCustomVariablesInRequests(List<RequestItem> items) {
if (!bodyVariables.isEmpty()) {
for (var item : items) {
Expand All @@ -220,7 +274,7 @@ private List<RequestItem> handleCustomVariablesInRequests(List<RequestItem> item
public void postProcess() {
System.out.println("##########################################################################################");
System.out.println("# Thanks for using OpenAPI Generator. #");
System.out.println("# Please consider donation to help us maintain this project \uD83D\uDE4F #");
System.out.println("# Please consider donation to help us maintain this project \uD83D\uDE4F #");
System.out.println("# https://opencollective.com/openapi_generator/donate #");
System.out.println("# #");
System.out.println("# This generator was written by Julien Lengrand-Lambert (https://github.com/jlengrand) #");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
{{#vendorExtensions.requests}}
### {{#summary}}{{summary}}{{/summary}}
## {{name}}
{{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}{{#authMethods}}{{#isKeyInQuery}}?{{keyParamName}}={{#lambda.doubleMustache}}{queryKey}{{/lambda.doubleMustache}}{{/isKeyInQuery}}{{/authMethods}}
{{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}{{>queryParams}}
{{#consumes}}
Content-Type: {{{mediaType}}}
{{/consumes}}
{{#produces}}
Accept: {{{mediaType}}}
{{/produces}}
{{#headerParams}}
{{baseName}}: {{=<% %>=}}{{<%paramName%>}}<%={{ }}=%>
{{/headerParams}}
{{#vendorExtensions.customHeaders}}
{{{.}}}
{{/vendorExtensions.customHeaders}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dev": {
{{#vendorExtensionsVariables}}
"{{.}}" : ""{{^-last}},{{/-last}}
{{/vendorExtensionsVariables}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +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}}
Loading

0 comments on commit eb60626

Please sign in to comment.