Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[BUG][JAVA][native] Bug: empty deepObject can produce invalid query #18736

Closed
5 of 6 tasks
marcel-huber-ptv opened this issue May 22, 2024 · 0 comments
Closed
5 of 6 tasks

Comments

@marcel-huber-ptv
Copy link
Contributor

marcel-huber-ptv commented May 22, 2024

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

Given a endpoint with a deep query object and at least one other parameter, it is possible to create an invalid query string, where multiple "&" are generated without parameters and values in between.

openapi-generator version

7.6.0, introduced in 6.3.0

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: title
  version: 0.0.1
paths:
  /test:
    get:
      parameters:
        - name: deepObject
          in: query
          schema:
            $ref: '#/components/schemas/DeepObject'
          explode: true
          style: deepObject
        - name: bar
          in: query
          schema:
            type: string
      responses:
        204:
          description: no content
components:
  schemas:
    DeepObject:
      type: object
      properties:
        foo:
          type: string
Generation Details

This produces

    StringJoiner localVarQueryStringJoiner = new StringJoiner("&");
    String localVarQueryParameterBaseName;
    localVarQueryParameterBaseName = "deepObject";
    if (deepObject != null) {
      localVarQueryStringJoiner.add(deepObject.toUrlQueryString("deepObject"));
    }
    localVarQueryParameterBaseName = "bar";
    localVarQueryParams.addAll(ApiClient.parameterToPairs("bar", bar));

which may add empty strings to the localVarQueryStringJoiner.

This would be the case for example with

    defaultApi.testGet(new DeepObject(), "foo");

producing
/test?&bar=foo

Instead I would expect something like

    StringJoiner localVarQueryStringJoiner = new StringJoiner("&");
    String localVarQueryParameterBaseName;
    localVarQueryParameterBaseName = "deepObject";
    if (deepObject != null) {
      String queryString = options.toUrlQueryString("deepObject");
      if (!queryString.isBlank()) {
        localVarQueryStringJoiner.add(queryString);
      }
    }
    localVarQueryParameterBaseName = "bar";
    localVarQueryParams.addAll(ApiClient.parameterToPairs("bar", bar));

to avoid such cases by not adding empty strings to the string builder.

Steps to reproduce

Generate the clients for the above openapi.yaml:
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i openapi.yaml -g java --library native -o out

Related issues/PRs

I couldn't find similar issure, but it seems to have been introduced with d269a2a

Suggest a fix

Don't add the result of {{paramName}}.toUrlQueryString("{{baseName}}") to localVarQueryStringJoiner if it is an empty string (modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache:371)

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

No branches or pull requests

1 participant