From 12e4ce179de179a64cc61c75e93ecd346362b33e Mon Sep 17 00:00:00 2001 From: ANUSH2510 Date: Mon, 24 Aug 2026 11:36:40 +0530 Subject: [PATCH] fix(java-spring): add @Deprecated annotation to fluent setters for deprecated properties Fixes #24704 - fluent setter methods (both for own properties and inherited properties, including array/map item adders) were missing the @Deprecated annotation that the getter and standard setter already carried for properties marked deprecated: true. --- .../main/resources/JavaSpring/pojo.mustache | 20 +++++++++++++++++-- .../java/spring/SpringCodegenTest.java | 18 +++++++++++++++++ .../main/java/org/openapitools/model/Pet.java | 2 ++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache index 47b1fd30dae0..b04c20104790 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache @@ -154,6 +154,9 @@ public {{>sealed}}class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}} {{^lombok.Data}} {{! begin feature: fluent setter methods }} + {{#deprecated}} + @Deprecated + {{/deprecated}} public {{classname}} {{name}}({{#useJspecify}}{{#lambda.jSpecifyNullable}}{{^required}}{{^useOptional}}@Nullable {{/useOptional}}{{#useOptional}}{{#optionalAcceptNullable}}@Nullable {{/optionalAcceptNullable}}{{/useOptional}}{{/required}}{{/lambda.jSpecifyNullable}}{{#lambda.jSpecifyDatatype}}{{{datatypeWithEnum}}}{{/lambda.jSpecifyDatatype}}{{/useJspecify}}{{^useJspecify}}{{>nullableAnnotation_default}}{{{datatypeWithEnum}}}{{/useJspecify}} {{name}}) { {{#openApiNullable}} this.{{name}} = {{#isNullable}}JsonNullable.of({{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}Optional.of{{#optionalAcceptNullable}}Nullable{{/optionalAcceptNullable}}({{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}}{{name}}{{#isNullable}}){{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}){{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}}; @@ -165,6 +168,9 @@ public {{>sealed}}class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}} } {{#isArray}} + {{#deprecated}} + @Deprecated + {{/deprecated}} public {{classname}} add{{nameInPascalCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) { {{#openApiNullable}} if (this.{{name}} == null{{#isNullable}} || !this.{{name}}.isPresent(){{/isNullable}}) { @@ -183,6 +189,9 @@ public {{>sealed}}class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}} {{/isArray}} {{#isMap}} + {{#deprecated}} + @Deprecated + {{/deprecated}} public {{classname}} put{{nameInPascalCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) { {{#openApiNullable}} if (this.{{name}} == null{{#isNullable}} || !this.{{name}}.isPresent(){{/isNullable}}) { @@ -270,19 +279,26 @@ public {{>sealed}}class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}} {{^lombok.Setter}} {{! begin feature: fluent setter methods for inherited properties }} + {{#deprecated}} + @Deprecated + {{/deprecated}} public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) { super.{{name}}({{name}}); return this; } {{#isArray}} - + {{#deprecated}} + @Deprecated + {{/deprecated}} public {{classname}} add{{nameInPascalCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) { super.add{{nameInPascalCase}}Item({{name}}Item); return this; } {{/isArray}} {{#isMap}} - + {{#deprecated}} + @Deprecated + {{/deprecated}} public {{classname}} put{{nameInPascalCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) { super.put{{nameInPascalCase}}Item(key, {{name}}Item); return this; diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java index e5c1ad926aa6..027eeacb3588 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java @@ -5833,6 +5833,24 @@ public void shouldGenerateSingleDeprecatedAnnotation() { .assertMethod("build") .doesNotHaveAnnotation("Deprecated"); } + /** + * Regression test for #24704 + */ + @Test + public void shouldGenerateDeprecatedAnnotationOnFluentSetter() { + final var tempDir = TestUtils.newTempFolder(); + final CodegenConfigurator configurator = new CodegenConfigurator() + .addAdditionalProperty(GENERATE_BUILDERS, true) + .addGlobalProperty(CodegenConstants.MODELS, "Pet") + .setInputSpec("src/test/resources/3_0/petstore.yaml") + .setGeneratorName("spring") + .setOutputDir(tempDir.toString()); + + new DefaultGenerator().opts(configurator.toClientOptInput()).generate(); + + JavaFileAssert.assertThat(tempDir.resolve("src/main/java/org/openapitools/model/Pet.java")) + .assertMethod("status", "StatusEnum").hasAnnotation("Deprecated"); + } @Test public void shouldAnnotateNonRequiredFieldsAsNullable() throws IOException { diff --git a/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/model/Pet.java b/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/model/Pet.java index 53b64044c6e3..06afff132f2e 100644 --- a/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/model/Pet.java +++ b/samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/model/Pet.java @@ -161,11 +161,13 @@ public void setName(JsonNullable name) { this.name = name; } + @Deprecated public Pet photoUrls(List photoUrls) { this.photoUrls = photoUrls; return this; } + @Deprecated public Pet addPhotoUrlsItem(String photoUrlsItem) { if (this.photoUrls == null) { this.photoUrls = new ArrayList<>();