OpenAPITools / OpenAPITools/openapi-generator

[REQ][Java] Enhance POJO generation with Optional

Open
#6,869 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

Currently a POJO is generated with either direct access returning null or through the proprietary JsonNullable class.
It should also be possible to generate the POJO without the method returning null, that is, use Optional for this case instead.
Since it has been discussed before in other threads, this enhancement does NOT mean replacing the usage of the JsonNullable class, only changing the returned data type from the standard get method to use Optional. The getXXX_JsonNullable class remains.
This enhancement means that in most cases the proprietary JsonNullable class stays hidden from the rest of the code while at the same time offering the full api of Optional.

Describe the solution you'd like

By setting the parameter 'useOptionalInApi' the code generator automatically adds Optional to the get methods.
Note the different name to the already existing 'useOptional' to avoid conflicting/undesired output.

Describe alternatives you've considered

Manually changing the code every time it is generated.
How error prone is that...?

Additional context

The code is ready, including test cases.

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 by locating the Java POJO generation path and the existing handling of the useOptional option. Compare the generated standard getter with the retained getXXX_JsonNullable method, then review the test cases mentioned in the issue. Done means a separate useOptionalInApi parameter generates Optional-returning standard getters without replacing JsonNullable support.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.