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

[Java] add missing nullable judgement when required property is true #18518

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

fanqiewanzi
Copy link
Contributor

fix #18516

  • Add judgment for nullable field to the required judgment.
  • Add test for java okhttp-gson template to avoid regressions

Tests:
openapi.yaml

openapi: 3.0.3
info:
  title: test
  description: Test API
  version: 1.0.1

paths:
  /test:
    get:
      responses:
        200:
          description: Valid response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SomeObject"

components:
  schemas:
    SomeObject:
      type: object
      required: 
        - ids
        - users
        - user
        - role
        - custom
      properties:
        ids:
          type: array
          nullable: true
          items:
            type: integer
        users:
          type: array
          nullable: true
          items:
            type: object
            properties:
              id:
                type: string
        user:
          type: object
          nullable: true
          properties:
            id:
              type: string
        role:
          type: string
          nullable: true
          enum:
            - admin
            - tenant
        custom:
          $ref: "#/components/schemas/customEnum"
    customEnum:
      type: string
      nullable: true
      enum:
        - custom    

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

And the code compiled successfully.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@@ -398,7 +398,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
{{#items.isModel}}
{{#required}}
// ensure the json data is an array
if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
if (!jsonObj.get("{{{baseName}}}").isJsonArray(){{#isNullable}} && !jsonObj.get("{{baseName}}").isJsonNull(){{/isNullable}}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the fix and the test.

shall the new condition {{#isNullable}} && !jsonObj.get("{{baseName}}").isJsonNull(){{/isNullable}} be put in its own code block instead of adding to the JSON array type check as the error message right below only mentions expecting the field to be an array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the fix and the test.

shall the new condition {{#isNullable}} && !jsonObj.get("{{baseName}}").isJsonNull(){{/isNullable}} be put in its own code block instead of adding to the JSON array type check as the error message right below only mentions expecting the field to be an array?

Hi, I don't quite understand. It doesn't seem to make sense to bring up the nullable judgment alone, because nullable is just an auxiliary judgment. If we put in its own code block, the nullable judgment is set outside the original judgment. From the code point of view, the logic is the same. If you write a judgment to nullable alone to throw an exception, the whole logic will seem even more strange.

     {{^isNullable}}
      if (jsonObj.get("{{baseName}}").isJsonNull()) {
        // throw exception here 
      }
      {{/isNullable}}
      // we will get an exception in the next judgement if json element is null
      // ensure the json data is an array
      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()));
      }

Or this is the change you want.

{{#isNullable}} 
if  (!jsonObj.get("{{baseName}}").isJsonNull()) {
  // ensure the json data is an array
        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()));
        }
}
{{/isNullable}}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok👌

@fanqiewanzi fanqiewanzi force-pushed the fix_issue_18516 branch 3 times, most recently from 6f6a2b0 to 9dd27c9 Compare May 20, 2024 11:05
@fanqiewanzi fanqiewanzi force-pushed the fix_issue_18516 branch 3 times, most recently from 06bb421 to 6375bc9 Compare May 20, 2024 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] [JAVA] validateJsonElement failed when a field is decorated with both nullable and required
2 participants