Skip to content
New issue

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

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

Already on GitHub? # to your account

[BUG] Generated code does not fail when required fields not present (generator jaxrs-spec, library jackson) #19577

Open
5 of 6 tasks
kevinferrare opened this issue Sep 13, 2024 · 0 comments · May be fixed by #19578
Open
5 of 6 tasks

Comments

@kevinferrare
Copy link

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

Parsing should fail when required fields are not present in input.

Generated code with jaxrs-spec / jackson accepts json with missing required fields.

For this to work with jackson, the following are needed:

  • required fields need be passed in parameter of a constructor annotated with @JsonCreator
  • each constructor parameter representing a required field need to be annotated with @JsonProperty(value = "fieldName", required = true)

See sections 2.1 and 2.2 of this doc: https://www.novixys.com/blog/jackson-annotations-guide/#21_Confusion_with_required / https://www.novixys.com/blog/jackson-annotations-guide/#22_JsonProperty_and_JsonCreator

openapi-generator version

7.9.0-SNAPSHOT

This is not a regression as far as I can tell

OpenAPI declaration file content or url

If you post the code inline, please wrap it with

openapi: 3.0.0
info:
  title: Test
  version: '1.0'
paths:
  '/test':
    get:
      requestBody:
        content:
          application/vnd.amadeus+json:
            schema:
              type: object
              title: TestObject
              required:
                - id
              properties:
                id:
                  type: string
      responses:
        '200':
          description: OK

Here TestObject.id is a required field

Generation Details

java -jar openapi-generator-cli.jar generate -o ./generated -i ./test-api.yaml -g jaxrs-spec --additional-properties=generateBuilders=true,serializationLibrary=jackson

Steps to reproduce

Save the API declaration in a file and generate it.

Deserializing empty json with generated code does not fail

objectMapper.readValue("{}", TestObject.class)

This code is supposed to thow MismatchedInputException since we didnt give "id" property in input.

Related issues/PRs

No, but other generators like "java" generate code with JsonCreator / JsonProperty

Suggest a fix

I will submit a PR with the template override with use for pojo.mustache

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
1 participant