OpenAPITools / OpenAPITools/openapi-generator

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

Open Beginner friendly
#24,704 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

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 in modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache and reproduce the issue with the provided OpenAPI document and Spring generator command. Verify the generated Example.java methods for deprecated scalar, array, and map properties, then add or update generator tests so deprecated fluent setters and collection helpers are annotated while non-deprecated methods and nested Builder behavior remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
84/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.