OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java] Required fields not honored in @JsonProperty annotations

Open
#21,060 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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?
Description

The Pojo and Jackson annotation templates are not honoring required fields in the spec. Adjacent @JsonBProperty annotations do.

Expected annotations with required fields:

@JsonProperty(value = JSON_PROPERTY_NAME, required = true)

Actual annotations

@JsonProperty(JSON_PROPERTY_NAME)

openapi-generator version

master

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: 'Some API'
  version: 1.0.0
servers:
  - url: 'https://localhost'
paths:
  /v1/some_path:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                type: object
                properties:
                  foo:
                    type: string
                required:
                  - foo
          description: "some description"
Steps to reproduce
  1. Have a spec with any required field
  2. Run the Java codegen with Jackson serialization
  3. See the generated model is not honoring required in the @JsonProperty annotation
Related issues/PRs

Have searched but not found any.

Suggest a fix

I will submit a PR with changes for further consideration.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Java codegen's Pojo and Jackson annotation templates, then run Java codegen with Jackson serialization against the YAML reproduction. Done means the generated required field uses @JsonProperty(value = JSON_PROPERTY_NAME, required = true) rather than the current shorter annotation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.