Skip to content

Exclude the discriminator field from getting a nullable_var_annotations #21155

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

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

Conversation

Mattias-Sehlstedt
Copy link
Contributor

@Mattias-Sehlstedt Mattias-Sehlstedt commented Apr 26, 2025

Fixes #21154

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 || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (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.x.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.

@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)

@Mattias-Sehlstedt Mattias-Sehlstedt force-pushed the pojo-model-discriminator-property-optional branch from 3ab01f8 to a8b2adf Compare April 26, 2025 15:48
@martin-mfg
Copy link
Contributor

Hi @Mattias-Sehlstedt, could you please provide an example where this problem occurs? I just used IntelliJ's "Inspect Code" functionality to analyze https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/java/apache-httpclient/src/main/java/org/openapitools/client/model/Animal.java. And IntelliJ didn't complain about the className field. Same result when using SonarLint.

@Mattias-Sehlstedt
Copy link
Contributor Author

Mattias-Sehlstedt commented May 8, 2025

Hi @martin-mfg,

An issue would occur if the client for example uses Lombok's @SuperBuilder to construct an object with

Cat.builder().color("red").build();

The intent would then be that this would be serialized at:

{
  "className": "CAT",
  "color": "red"
}

Since "red" will be from the object property, while className will be set based upon the Jackson annotation:

@JsonIgnoreProperties(
  value = "className", // ignore manually set className, it will be automatically generated by Jackson during serialization
  allowSetters = true // allows the className to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "className", visible = true)

But since lombok verifies the object's annotations before it is instantiated, it rejected it since not all required fields are set (className not being set).

It is also not me having this issue, but rather the client that generates code based upon my specification.

I am myself aware of several ways to handle this (only customize my own mustache file, use the standard parameter-building instead of lombok, ...). But since lombok is so well established, and the generated code makes an attempt to assist the programmer so they do not have to set the type themselves, I though it could maybe make sense to omit it from the NonNull-annotation.

So the issue is basically a lighter version of the issues raised here.

@martin-mfg
Copy link
Contributor

I partially understand the problem in general, but I don't understand the example with @SuperBuilder yet. When I try to reproduce the problem with @SuperBuilder public class Cat extends Animal {, the code doesn't even compile, because AnimalBuilder doesn't exist. And indeed the docs say @SuperBuilder "requires that all superclasses also have the @SuperBuilder annotation." Which is not the case for Animal. So I am still wondering how this problem occurs in practice. Maybe you can provide a reproducible example?

Apart from this, I guess if the generated annotation is Nullable instead of Nonnull then we can keep it, right?

@Mattias-Sehlstedt
Copy link
Contributor Author

I have created an example of a case here where it is not possible to build the model with the Lombok builder (unless you manually set the discriminator value). This is due to it reacting to the @Nonnull annotation and the discriminator value being null since I have not set it explicitly. The repository also includes the specification used for generating the model.

So here the issue is partially due to the generated model being an extension (the root object with the discriminator is extended by the children). But this issue would also occur if you had a flat model and you had @Validated on the method signature that takes the generated model (which is something you might be interested in if you want to conduct the validation on the client's end instead of trying to rely on a descriptive 400 response from the server).

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.

[REQ] Do not add a null-annotation on a discriminator property
2 participants