Skip to content

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

@ohl-nemeses

Description

@ohl-nemeses

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();
        }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions