Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG][JAVA] ApiClientHttpRequestInterceptor.headersToString throws #12319

Closed
1 task
ohl-nemeses opened this issue May 9, 2022 · 2 comments
Closed
1 task

Comments

@ohl-nemeses
Copy link
Contributor

Bug Report Checklist

  • [-] Have you provided a full/minimal spec to reproduce the issue?
  • [-] Have you validated the input using an OpenAPI validator (example)?
  • [/] Have you tested with the latest master to confirm the issue still exists?
  • [/] Have you searched for related issues/PRs?
  • [/] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Method headersToString throws an StringIndexOutOfBoundsException in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache#L844 if given HttpHeaders are empty.

openapi-generator version

I was using

      <plugin>
        <groupId>io.swagger.codegen.v3</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>3.0.33</version>
      </plugin>
OpenAPI declaration file content or url
Generation Details

Java, RestTemplate

Steps to reproduce
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
        at java.base/java.lang.AbstractStringBuilder.setLength(AbstractStringBuilder.java:275)
        at java.base/java.lang.StringBuilder.setLength(StringBuilder.java:85)
        at com.anypackage.swagger.ApiClient$ApiClientHttpRequestInterceptor.headersToString(ApiClient.java:586)
        at com.anypackage.swagger.ApiClient$ApiClientHttpRequestInterceptor.logResponse(ApiClient.java:572)
        at com.anypackage.swagger.ApiClient$ApiClientHttpRequestInterceptor.intercept(ApiClient.java:558)
...

It is plain to see that the generated utility method is erroneous in corner cases:

        private String headersToString(HttpHeaders headers) {
            StringBuilder builder = new StringBuilder();
            for (Entry<String, List<String>> entry : headers.entrySet()) {
                builder.append(entry.getKey()).append("=[");
                for (String value : entry.getValue()) {
                    builder.append(value).append(",");
                }
                builder.setLength(builder.length() - 1); // Get rid of trailing comma
                builder.append("],");
            }
            builder.setLength(builder.length() - 1); // Get rid of trailing comma
            return builder.toString();
        }
Related issues/PRs

None.

Suggest a fix

Return empty string if headers are empty or null.

        private String headersToString(HttpHeaders headers) {
            if(headers == null || headers.isEmpty()) {
                return "";
            }
            StringBuilder builder = new StringBuilder();
            for (Entry<String, List<String>> entry : headers.entrySet()) {
                builder.append(entry.getKey()).append("=[");
                for (String value : entry.getValue()) {
                    builder.append(value).append(",");
                }
                builder.setLength(builder.length() - 1); // Get rid of trailing comma
                builder.append("],");
            }
            builder.setLength(builder.length() - 1); // Get rid of trailing comma
            return builder.toString();
        }
@wing328
Copy link
Member

wing328 commented May 9, 2022

@ohl-nemeses thanks for reporting the issue. I wonder if you can file a PR with the suggested fix when you've time.

@ohl-nemeses
Copy link
Contributor Author

Here you go: #12327

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants