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] validateJsonElement failed when a field is decorated with both nullable and required #18516

Open
5 of 6 tasks
fanqiewanzi opened this issue Apr 27, 2024 · 1 comment · May be fixed by #18518
Open
5 of 6 tasks

Comments

@fanqiewanzi
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
openapi-generator version

(7.5.0)the latest master branch

OpenAPI declaration file content or url
Generation Details

run command to generate:
openapi-generator-cli generate -i test.yaml -g java -o test

Steps to reproduce

after generating the code, you will find out nullable didn't work, there is no nullable judgement in validateJsonElement function. I'm not quiet sure whether this is a bug or something. But according to the OpenAPI YAML specification and the judgment of required in the code of this repository, I prefer this to be a bug.

this is the validateJsonElement function of SomeObject:

image

Except for the yaml example I gave, all other errors involving required and nullable occur at the same time.

Related issues/PRs

Not found yet.

Suggest a fix

before:

{{#required}}
      // ensure the required json array is present
      if (jsonObj.get("{{{baseName}}}") == null) {
        throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
      } else if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
        throw new IllegalArgumentException(String.format("Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
      }
      {{/required}}

after:

{{#required}}
      // ensure the required json array is present
      if (jsonObj.get("{{{baseName}}}") == null) {
        throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
      } else if (!jsonObj.get("{{{baseName}}}").isJsonArray() {{#isNullable}} && !jsonObj.get("{{baseName}}").isJsonNull(){{/isNullable}}) {
        throw new IllegalArgumentException(String.format("Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
      }
      {{/required}}

Other types of processing are also similar。

@wing328
Copy link
Member

wing328 commented Apr 27, 2024

can you please file a PR with the suggested fix when you've time?

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

Successfully merging a pull request may close this issue.

2 participants