Skip to content

fix(java-spring): handle UUID type in enum values generation#23589

Merged
wing328 merged 2 commits intoOpenAPITools:masterfrom
renatomameli:fix-uuid-enum-value-gen
Apr 23, 2026
Merged

fix(java-spring): handle UUID type in enum values generation#23589
wing328 merged 2 commits intoOpenAPITools:masterfrom
renatomameli:fix-uuid-enum-value-gen

Conversation

@renatomameli
Copy link
Copy Markdown
Contributor

@renatomameli renatomameli commented Apr 18, 2026

Problem

When generating Spring enums with format: uuid, the generated code incorrectly uses string literals instead of the proper UUID.fromString(...) method.

This issue came up here: #23516

// Before (broken)
D6A8F2B0_1C34_4E56_A789_0ABCDEF12345("d6a8f2b0-...")

// After (fixed)
D6A8F2B0_1C34_4E56_A789_0ABCDEF12345(UUID.fromString("d6a8f2b0-..."))

Solution

Add UUID handling in AbstractJavaCodegen.toEnumValue() following the existing URI pattern:

} else if ("UUID".equals(datatype)) {
    return "UUID.fromString(\"" + escapeText(value) + "\")";
}

Test

Added contractWithUuidEnumShouldGenerateValidEnum() test that verifies UUID.fromString() is used for enum values

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    ``` <!-- Enter details of the change here. Include additional tests that have been done, reference to the issue for tracking, etc. -->
    
    

(For Windows users, please run the script in WSL)
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.

  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fix enum generation for format: uuid in Java/Spring. Enums now call UUID.fromString(...) and store real UUID values instead of strings.

  • Bug Fixes
    • Handle UUID in AbstractJavaCodegen.toEnumValue() by returning UUID.fromString("...").
    • Add test contractWithUuidEnumShouldGenerateValidEnum and sample spec to verify UUID.fromString(...) and a private final UUID value.

Written for commit 6cb572a. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

@renatomameli
Copy link
Copy Markdown
Contributor Author

@wing328 result of your comment #23516 (comment)

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 19, 2026

cc @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 20, 2026

I did a test with

 java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g spring --library spring-boot -i https://raw.githubusercontent.com/renatomameli/openapi-generator/5f83d1672a5a2047c4f2c624ebb64838fbae8693/modules/openapi-generator/src/test/resources/3_0/enum_uuid.yaml -o /tmp/spring2

but the output won't compile.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile (default-compile) on project openapi-spring: Compilation failure: Compilation failure:
[ERROR] /C:/Users/User/AppData/Local/Temp/spring2/src/main/java/org/openapitools/configuration/EnumConverterConfiguration.java:[20,15] cannot find symbol
[ERROR]   symbol:   class UUID
[ERROR]   location: class org.openapitools.configuration.EnumConverterConfiguration
[ERROR] /C:/Users/User/AppData/Local/Temp/spring2/src/main/java/org/openapitools/api/ResourcesApiController.java:[34,6] cannot find symbol
[ERROR]   symbol:   class Nullable
[ERROR]   location: class org.openapitools.api.ResourcesApiController
[ERROR] /C:/Users/User/AppData/Local/Temp/spring2/src/main/java/org/openapitools/api/ResourcesApiController.java:[38,36] cannot find symbol
[ERROR]   symbol:   class Nullable
[ERROR]   location: class org.openapitools.api.ResourcesApiController
[ERROR] /C:/Users/User/AppData/Local/Temp/spring2/src/main/java/org/openapitools/configuration/EnumConverterConfiguration.java:[21,30] cannot find symbol
[ERROR]   symbol:   class UUID
[ERROR]   location: class org.openapitools.configuration.EnumConverterConfiguration
[ERROR] /C:/Users/User/AppData/Local/Temp/spring2/src/main/java/org/openapitools/configuration/EnumConverterConfiguration.java:[23,44] cannot find symbol
[ERROR]   symbol: class UUID
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.

am I testing this change correctly?

@renatomameli
Copy link
Copy Markdown
Contributor Author

renatomameli commented Apr 20, 2026

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g spring --library spring-boot -i https://raw.githubusercontent.com/renatomameli/openapi-generator/5f83d1672a5a2047c4f2c624ebb64838fbae8693/modules/openapi-generator/src/test/resources/3_0/enum_uuid.yaml -o /tmp/spring

For EnumConverterConfiguration I get the same result. But that is fixed by my other "original" PR: #23516 which I mentioned here.

I cannot confirm the errors in ResourcesApiController. I am not sure why Nullable should cause an error out of a sudden there. My output for this class is the following:

package org.openapitools.codegen.languages;

import org.openapitools.model.Resource;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.context.request.NativeWebRequest;

import jakarta.validation.constraints.*;
import jakarta.validation.Valid;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import jakarta.annotation.Generated;

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2026-04-20T14:02:03.229211979+02:00[Europe/Vienna]", comments = "Generator version: 7.22.0-SNAPSHOT")
@Controller
@RequestMapping("${openapi.sample.base-path:}")
public class ResourcesApiController implements ResourcesApi {

    private final NativeWebRequest request;

    @Autowired
    public ResourcesApiController(NativeWebRequest request) {
        this.request = request;
    }

    @Override
    public Optional<NativeWebRequest> getRequest() {
        return Optional.ofNullable(request);
    }

}

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 21, 2026

cc @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)

@jpfinne
Copy link
Copy Markdown
Contributor

jpfinne commented Apr 21, 2026

@renatomameli @wing328 The compilation error for class Nullable will be fixed when #23576 is merged.

I've introduced a regression with the introduction of jspecify, sorry for the inconvenience .

The missing UUID import is due to modules/openapi-generator/src/main/resources/JavaSpring/converter.mustache

It does not import UUID, URI, BigDecimal... It makes sense to add them as well.

Either you unconditionally import them in the template, either you add them programmatically in additionalProperties

{{#enumConverter.imports}}import {{import}};
{{/enumConverter.imports}}

@renatomameli
Copy link
Copy Markdown
Contributor Author

@renatomameli @wing328 The compilation error for class Nullable will be fixed when #23576 is merged.

I've introduced a regression with the introduction of jspecify, sorry for the inconvenience .

The missing UUID import is due to modules/openapi-generator/src/main/resources/JavaSpring/converter.mustache

It does not import UUID, URI, BigDecimal... It makes sense to add them as well.

Either you unconditionally import them in the template, either you add them programmatically in additionalProperties

{{#enumConverter.imports}}import {{import}};
{{/enumConverter.imports}}

Yes the missing imports are fixed here: #23516

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 23, 2026

For EnumConverterConfiguration I get the same result. But that is fixed by my other "original" PR: #23516 which I mentioned here.

confirmed the output compiles without issues when tested with both PRs merged into master.

@wing328 wing328 added this to the 7.22.0 milestone Apr 23, 2026
@wing328 wing328 merged commit 62328b6 into OpenAPITools:master Apr 23, 2026
14 of 15 checks passed
@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 23, 2026

FYI. samples updated via bf51147

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants