Skip to content

[BUG][JAVA][SPRING] Deprecated enum schemas are not annotated with @Deprecated #24694

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 generated enum classes when their OpenAPI schema specifies deprecated: true.

Deprecated object schemas are correctly annotated with @Deprecated. Enum schemas are not, although the generator exposes the schema deprecation state through CodegenModel.isDeprecated.

As a result, Java consumers receive no compiler warning when using an enum type that is deprecated in the OpenAPI contract.

The issue was reproduced locally from master at commit 66f697325c1273ce1aa26813afd286a984acdb49.

The Java Spring enum templates at that commit do not include conditional deprecation handling:

openapi-generator version

The issue occurs with OpenAPI Generator 7.24.0.

It was also reproduced with version 7.25.0-SNAPSHOT built from the latest master commit at the time of testing:

66f697325c1273ce1aa26813afd286a984acdb49

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Deprecated enum example
  version: 1.0.0
paths:
  /status:
    get:
      operationId: getStatus
      responses:
        "200":
          description: Current status
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/DeprecatedStatus"
components:
  schemas:
    DeprecatedStatus:
      type: string
      deprecated: true
      description: A deprecated status type.
      enum:
        - ACTIVE
        - INACTIVE
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

The latest 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

The specification passes validation without issues.

No additional generator options are required to reproduce the issue.

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

Relevant actual output:

/**
 * A deprecated status type.
 */
public enum DeprecatedStatus {
    ACTIVE("ACTIVE"),
    INACTIVE("INACTIVE");
}

Expected output:

/**
 * A deprecated status type.
 */
@Deprecated
public enum DeprecatedStatus {
    ACTIVE("ACTIVE"),
    INACTIVE("INACTIVE");
}

Enum schemas without deprecated: true must remain unchanged.

Related issues/PRs
  • #22311 reported the same general problem for Java enum generation.
  • #22312 fixed it for several Java generators but did not update the Java Spring templates.
Suggest a fix

Use CodegenModel.isDeprecated in enumOuterClass.mustache and CodegenProperty.deprecated in enumClass.mustache to emit @Deprecated when the corresponding enum schema is deprecated.

For top level enums:

{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}

For inline enums:

{{#deprecated}}
@Deprecated
{{/deprecated}}

Apply the corresponding condition to each Java Spring enum template, with tests confirming that:

  1. Deprecated top level enum schemas receive @Deprecated.
  2. Deprecated inline enum schemas receive @Deprecated.
  3. Enum schemas that are not deprecated remain 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