Skip to content

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

@kevinferrare

Description

@kevinferrare

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions