Skip to content

Commit 5f83d16

Browse files
renato-tvrenatomameli
authored andcommitted
fix(java-spring): handle UUID type in enum values generation
1 parent e6ef8ee commit 5f83d16

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,8 @@ public String toEnumValue(String value, String datatype) {
23012301
return "new BigDecimal(\"" + value + "\")";
23022302
} else if ("URI".equals(datatype)) {
23032303
return "URI.create(\"" + escapeText(value) + "\")";
2304+
} else if ("UUID".equals(datatype)) {
2305+
return "UUID.fromString(\"" + escapeText(value) + "\")";
23042306
} else {
23052307
return "\"" + escapeText(value) + "\"";
23062308
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,6 +3094,16 @@ public void contractWithResolvedInnerEnumContainsEnumConverter() throws IOExcept
30943094
.assertMethod("ponyTypeConverter");
30953095
}
30963096

3097+
@Test
3098+
public void contractWithUuidEnumShouldGenerateValidEnum() throws IOException {
3099+
Map<String, File> output = generateFromContract("src/test/resources/3_0/enum_uuid.yaml", SPRING_BOOT);
3100+
3101+
JavaFileAssert.assertThat(output.get("ExampleUuidEnum.java"))
3102+
.fileContains("UUID.fromString(\"d6a8f2b0-1c34-4e56-a789-0abcdef12345\")")
3103+
.fileContains("UUID.fromString(\"e7b9c3d1-2d45-5f67-b890-1bcdef023456\")")
3104+
.fileContains("private final UUID value");
3105+
}
3106+
30973107
@Test
30983108
public void shouldUseTheSameTagNameForTheInterfaceAndTheMethod_issue11570() throws IOException {
30993109
final Map<String, File> output = generateFromContract(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Sample API
4+
description: API with UUID enum.
5+
version: 1.0.0
6+
paths:
7+
/resources:
8+
get:
9+
summary: Returns resources.
10+
responses:
11+
200:
12+
description: OK
13+
content:
14+
application/json:
15+
schema:
16+
type: array
17+
items:
18+
$ref: '#/components/schemas/Resource'
19+
components:
20+
schemas:
21+
Resource:
22+
type: object
23+
properties:
24+
source:
25+
$ref: '#/components/schemas/ExampleUuidEnum'
26+
ExampleUuidEnum:
27+
type: string
28+
format: uuid
29+
description: Example of an Enum with UUID values
30+
enum:
31+
- "d6a8f2b0-1c34-4e56-a789-0abcdef12345"
32+
- "e7b9c3d1-2d45-5f67-b890-1bcdef023456"

0 commit comments

Comments
 (0)