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] Generated Lists default to empty List even when using nullable: true and containerDefaultToNull: true #17206

Open
5 of 6 tasks
mimkorn opened this issue Nov 28, 2023 · 1 comment

Comments

@mimkorn
Copy link

mimkorn commented Nov 28, 2023

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

Generated Lists default to empty List even when using nullable: true and containerDefaultToNull: true

We need to allow the end user to not pass a certain non-required list query param and recognize it as null, because we need to distinguish it from when they pass us an empty list -> it means semantically something else than when they do not pass anything, pass null as the query param. The containerDefaultToNull was supposed to affect this, but setting that property leads to exactly same generated code base. Adding nullable: true also doesn't help, as was suggested elsewhere to use.

openapi-generator version

6.5, 7.1, latest docker image, assuming that's 8.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Simple API
  version: 1.0.0
paths:
  /your-endpoint:
    get:
      summary: Endpoint that accepts a list of strings as a query parameter
      parameters:
        - name: items
          in: query
          description: A list of strings
          required: false
          schema:
            type: array
            nullable: true
            items:
              type: string
      responses:
        '200':
          description: Successful response
Generation Details

openapi-generator generate -g java -i open/openapi.yaml -o open2 -c /tmp/config.json

config has only

{
    "containerDefaultToNull":true
}

and you can actually leave it out and compare what's generated and you'll see you get exactly the same code base, which points to the fact, that the parameter is completely ignored.

Steps to reproduce
  1. Use the generator as above with the specified spec above
  2. Do the same but with the configuration above used
  3. Observe that there is no difference in codebase. Calling the endpoint without the query param will result in an empty list for the query param on the server code.
Related issues/PRs

This looks similar, but says the other way around -> that it no longer defaults to list but to null. I have the opposite experience. #15891 My comment there about this issue is: #15891 (comment)

Suggest a fix

The generated ApiClient.java has this part there

public List<Pair> parameterToPairs(String collectionFormat, String name, Object value){
    List<Pair> params = new ArrayList<Pair>();

    // preconditions
    if (name == null || name.isEmpty() || value == null) return params;

Instead of returning empty list params based on those preconditions, the code should check for the presence of the containerDefaultToNull option being set to true and also should check if the field is required or nullable, etc.

@jorgerod
Copy link
Contributor

jorgerod commented Mar 8, 2024

Hi

Any news? Is there a workaround?

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

2 participants