OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA][SPRING] Fluent model setters for deprecated properties are not annotated with @Deprecated
Nobody has claimed this yet.
- 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
- Save the OpenAPI document as
openapi.yaml. - Generate Java Spring sources using one of the commands above.
- 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:
- Fluent setters for deprecated scalar properties receive
@Deprecated. - Fluent setters for deprecated array and map properties receive
@Deprecated. - Collection helper methods for deprecated properties receive
@Deprecated. - Methods for properties that are not deprecated remain unchanged.
- Existing nested
Builderdeprecation behavior remains unchanged.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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