Bug Report Checklist
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
Builder deprecation behavior remains unchanged.
Bug Report Checklist
Description
The Java Spring generator does not add
@Deprecatedto fluent model setter methods generated for properties that specifydeprecated: true.For a deprecated property, the generated field, getter and regular setter are correctly annotated with
@Deprecated. WhengenerateBuilders=trueis enabled, the corresponding method in the nestedBuilderclass is also correctly annotated.However, the fluent setter generated directly on the model class is not annotated:
Generated collection helpers such as
addDeprecatedValuesItem(...)andputDeprecatedMapItem(...)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
masterat commit:9a0e7ae1fe7a29bdba364d433febd62541985059The relevant methods are generated by
JavaSpring/pojo.mustache. The template checks thedeprecatedproperty 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
mastercommit:9a0e7ae1fe7a29bdba364d433febd62541985059OpenAPI declaration file content or url
Generation Details
The issue can be reproduced with OpenAPI Generator 7.24.0:
The current master CLI was built and tested locally:
The specification passes validation without issues.
The
generateBuildersoption is not required to reproduce the missing annotation on fluent model setters. It is enabled here to demonstrate that the separate nestedBuildermethods already handle deprecated properties correctly.Steps to reproduce
openapi.yaml.Example.java.Relevant actual output:
The fluent collection helpers are also missing the annotation:
By comparison, the nested builder method is correctly annotated when
generateBuilders=trueis enabled:Expected output for the fluent model setter:
Expected output for the collection helpers:
Methods generated for properties without
deprecated: truemust remain unchanged.Related issues/PRs
No exact issue for Java Spring fluent model setters was found.
Suggest a fix
Use the existing
CodegenProperty.deprecatedvalue inJavaSpring/pojo.mustacheand emit@Deprecatedfor fluent model setters:Apply the same condition to generated array and map helper methods:
Add tests confirming that:
@Deprecated.@Deprecated.@Deprecated.Builderdeprecation behavior remains unchanged.