openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

Exclude the discriminator field from getting a nullable_var_annotations

Open Mattias-Sehlstedt opened this issue 8 months ago • 4 comments

Fixes #21154

PR checklist

  • [x] Read the contribution guidelines.
  • [x] 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.
  • [x] 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.
  • [x] 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)
  • [x] 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 avatar Apr 26 '25 15:04 Mattias-Sehlstedt

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.

martin-mfg avatar May 08 '25 10:05 martin-mfg

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.

Mattias-Sehlstedt avatar May 08 '25 15:05 Mattias-Sehlstedt

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?

martin-mfg avatar Jun 11 '25 06:06 martin-mfg

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).

Mattias-Sehlstedt avatar Jun 12 '25 19:06 Mattias-Sehlstedt

Does @wing328 have any input? I would argue against always having @Nullable on the discriminator, since it would make it unclear exactly what is expected. Especially since the setter for the disicriminator will still properly retain setDiscriminator(@Nonnull String discriminator).

A better approach I think would be to add a comment next to the discriminator, that would explain the nullable omission. E.g. "Nullable-annotation omitted since the discriminator is implicitly set by the @JsonTypeInfo-annotation".

Mattias-Sehlstedt avatar Jul 18 '25 15:07 Mattias-Sehlstedt

A better approach I think would be to add a comment next to the discriminator, that would explain the nullable omission. E.g. "Nullable-annotation omitted since the discriminator is implicitly set by the @JsonTypeInfo-annotation".

sounds good to me

wing328 avatar Aug 16 '25 07:08 wing328

thanks for the pr

let's give it a try

wing328 avatar Aug 16 '25 10:08 wing328