Skip to content

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

@Mattias-Sehlstedt Mattias-Sehlstedt force-pushed the pojo-model-discriminator-property-optional branch from 43a7342 to 1683239 Compare July 18, 2025 15:42
@Mattias-Sehlstedt
Copy link
Contributor Author

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

@wing328
Copy link
Member

wing328 commented Aug 16, 2025

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

@Mattias-Sehlstedt Mattias-Sehlstedt force-pushed the pojo-model-discriminator-property-optional branch from 1683239 to c8908d8 Compare August 16, 2025 09:34
@wing328 wing328 added this to the 7.15.0 milestone Aug 16, 2025
@wing328 wing328 merged commit 2513d82 into OpenAPITools:master Aug 16, 2025
85 of 86 checks passed
@wing328
Copy link
Member

wing328 commented Aug 16, 2025

thanks for the pr

let's give it a try

@Mattias-Sehlstedt Mattias-Sehlstedt deleted the pojo-model-discriminator-property-optional branch August 16, 2025 13:01
Goopher pushed a commit to Goopher/openapi-generator that referenced this pull request Sep 9, 2025
…ns (OpenAPITools#21155)

* Exclude the discriminator field from getting a nullable_var_annotations

* Update samples

* Manually add annotations since the remote pipeline insists that they should be there

* Add description for why the discriminator does not have a nullability-annotation

* Update samples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants