Skip to content

[BUG][JAVA][SPRING] Fluent model setters for deprecated properties are not annotated with @Deprecated #24704

Description

@R3TRO04

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The Java Spring generator does not add @Deprecated to fluent model setter methods generated for properties that specify deprecated: true.

For a deprecated property, the generated field, getter and regular setter are correctly annotated with @Deprecated. When generateBuilders=true is enabled, the corresponding method in the nested Builder class is also correctly annotated.

However, the fluent setter generated directly on the model class is not annotated:

example.deprecatedProperty("value");

Generated collection helpers such as addDeprecatedValuesItem(...) and putDeprecatedMapItem(...) are also not annotated when their property is deprecated.

This means that consumers using the fluent model API receive no method specific compiler warning for deprecated properties.

The problem was observed with OpenAPI Generator 7.24.0 and reproduced with version 7.25.0-SNAPSHOT built from master at commit:

9a0e7ae1fe7a29bdba364d433febd62541985059

The relevant methods are generated by JavaSpring/pojo.mustache. The template checks the deprecated property for fields, getters and regular setters but not for fluent setters or collection helper methods.

openapi-generator version

The issue occurs with OpenAPI Generator 7.24.0.

It was also reproduced with OpenAPI Generator 7.25.0-SNAPSHOT built from master commit:

9a0e7ae1fe7a29bdba364d433febd62541985059

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Deprecated property example
  version: 1.0.0
paths:
  /example:
    get:
      operationId: getExample
      responses:
        "200":
          description: Example response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Example"
components:
  schemas:
    Example:
      type: object
      properties:
        currentProperty:
          type: string
        deprecatedProperty:
          type: string
          deprecated: true
        deprecatedValues:
          type: array
          deprecated: true
          items:
            type: string
        deprecatedMap:
          type: object
          deprecated: true
          additionalProperties:
            type: string
Generation Details

The issue can be reproduced with OpenAPI Generator 7.24.0:

java -jar openapi-generator-cli-7.24.0.jar generate \
  --generator-name spring \
  --library spring-boot \
  --input-spec openapi.yaml \
  --output generated \
  --additional-properties generateBuilders=true

The current master CLI was built and tested locally:

./mvnw -pl modules/openapi-generator-cli -am package \
  -Dmaven.test.skip=true \
  -Dmaven.javadoc.skip=true

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar \
  validate \
  --input-spec openapi.yaml

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar \
  generate \
  --generator-name spring \
  --library spring-boot \
  --input-spec openapi.yaml \
  --output generated \
  --additional-properties generateBuilders=true

The specification passes validation without issues.

The generateBuilders option is not required to reproduce the missing annotation on fluent model setters. It is enabled here to demonstrate that the separate nested Builder methods already handle deprecated properties correctly.

Steps to reproduce
  1. Save the OpenAPI document as openapi.yaml.
  2. Generate Java Spring sources using one of the commands above.
  3. Inspect the generated Example.java.

Relevant actual output:

@Deprecated
private String deprecatedProperty;

public Example deprecatedProperty(String deprecatedProperty) {
    this.deprecatedProperty = deprecatedProperty;
    return this;
}

@Deprecated
public String getDeprecatedProperty() {
    return deprecatedProperty;
}

@Deprecated
public void setDeprecatedProperty(String deprecatedProperty) {
    this.deprecatedProperty = deprecatedProperty;
}

The fluent collection helpers are also missing the annotation:

public Example addDeprecatedValuesItem(String deprecatedValuesItem) {
    // ...
    return this;
}

public Example putDeprecatedMapItem(String key, String deprecatedMapItem) {
    // ...
    return this;
}

By comparison, the nested builder method is correctly annotated when generateBuilders=true is enabled:

@Deprecated
public Example.Builder deprecatedProperty(String deprecatedProperty) {
    this.instance.deprecatedProperty(deprecatedProperty);
    return this;
}

Expected output for the fluent model setter:

@Deprecated
public Example deprecatedProperty(String deprecatedProperty) {
    this.deprecatedProperty = deprecatedProperty;
    return this;
}

Expected output for the collection helpers:

@Deprecated
public Example addDeprecatedValuesItem(String deprecatedValuesItem) {
    // ...
    return this;
}

@Deprecated
public Example putDeprecatedMapItem(String key, String deprecatedMapItem) {
    // ...
    return this;
}

Methods generated for properties without deprecated: true must remain unchanged.

Related issues/PRs
  • #15286 reported similar missing annotations for deprecated properties in the Java OkHttp Gson generator.
  • #15287 fixed that issue for the OkHttp Gson templates but did not update the Java Spring templates.

No exact issue for Java Spring fluent model setters was found.

Suggest a fix

Use the existing CodegenProperty.deprecated value in JavaSpring/pojo.mustache and emit @Deprecated for fluent model setters:

{{#deprecated}}
  @Deprecated
{{/deprecated}}
  public {{classname}} {{name}}(...) {

Apply the same condition to generated array and map helper methods:

{{#deprecated}}
  @Deprecated
{{/deprecated}}
  public {{classname}} add{{nameInPascalCase}}Item(...) {
{{#deprecated}}
  @Deprecated
{{/deprecated}}
  public {{classname}} put{{nameInPascalCase}}Item(...) {

Add tests confirming that:

  1. Fluent setters for deprecated scalar properties receive @Deprecated.
  2. Fluent setters for deprecated array and map properties receive @Deprecated.
  3. Collection helper methods for deprecated properties receive @Deprecated.
  4. Methods for properties that are not deprecated remain unchanged.
  5. Existing nested Builder deprecation behavior remains unchanged.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions