Skip to content

Commit a63ff87

Browse files
fix(java-spring): add missing imports for converter (#23325) (#23516)
Co-authored-by: Renato Mameli <renato.mameli@teamviewer.com>
1 parent 388f031 commit a63ff87

28 files changed

Lines changed: 225 additions & 0 deletions

File tree

modules/openapi-generator/src/main/resources/JavaSpring/converter.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package {{configPackage}};
22

3+
import java.math.BigDecimal;
4+
import java.net.URI;
5+
import java.util.UUID;
6+
37
{{#models}}
48
{{#model}}
59
{{#isEnum}}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,6 +3058,36 @@ public void shouldHandleSeparatelyInterfaceAndModelAdditionalAnnotations() throw
30583058
.containsWithName("marker.Common");
30593059
}
30603060

3061+
@Test
3062+
public void contractWithUriEnumContainsEnumConverterWithUriImport() throws IOException {
3063+
Map<String, File> output = generateFromContract("src/test/resources/3_0/enum_uri.yaml", SPRING_BOOT);
3064+
3065+
JavaFileAssert.assertThat(output.get("EnumConverterConfiguration.java"))
3066+
.hasImports("java.net.URI")
3067+
.fileContains("Converter<URI, ExampleUriEnum>")
3068+
.assertMethod("exampleUriEnumConverter");
3069+
}
3070+
3071+
@Test
3072+
public void contractWithUuidEnumContainsEnumConverterWithUuidImport() throws IOException {
3073+
Map<String, File> output = generateFromContract("src/test/resources/3_0/enum_uuid.yaml", SPRING_BOOT);
3074+
3075+
JavaFileAssert.assertThat(output.get("EnumConverterConfiguration.java"))
3076+
.hasImports("java.util.UUID")
3077+
.fileContains("Converter<UUID, ExampleUuidEnum>")
3078+
.assertMethod("exampleUuidEnumConverter");
3079+
}
3080+
3081+
@Test
3082+
public void contractWithNumberEnumContainsEnumConverterWithBigDecimalImport() throws IOException {
3083+
Map<String, File> output = generateFromContract("src/test/resources/3_0/enum_number.yaml", SPRING_BOOT);
3084+
3085+
JavaFileAssert.assertThat(output.get("EnumConverterConfiguration.java"))
3086+
.hasImports("java.math.BigDecimal")
3087+
.fileContains("Converter<BigDecimal, ExampleNumberEnum>")
3088+
.assertMethod("exampleNumberEnumConverter");
3089+
}
3090+
30613091
@Test
30623092
public void contractWithoutEnumDoesNotContainEnumConverter() throws IOException {
30633093
Map<String, File> output = generateFromContract("src/test/resources/3_0/generic.yaml", SPRING_BOOT);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Sample API
4+
description: API with number 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/ExampleNumberEnum'
26+
ExampleNumberEnum:
27+
type: number
28+
description: Example of an Enum with number values
29+
enum:
30+
- 1.1
31+
- 2.2
32+
- 3.3
33+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Sample API
4+
description: API with URI 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/ExampleUriEnum'
26+
ExampleUriEnum:
27+
type: string
28+
format: uri
29+
description: Example of an Enum with URI values
30+
enum:
31+
- "https://another-example.com"
32+
- "https://example.com"
33+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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"
33+

samples/openapi3/server/petstore/spring-boot-oneof-interface/src/main/java/org/openapitools/configuration/EnumConverterConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.openapitools.configuration;
22

3+
import java.math.BigDecimal;
4+
import java.net.URI;
5+
import java.util.UUID;
6+
37
import org.openapitools.model.FruitType;
48

59
import org.springframework.context.annotation.Bean;

samples/openapi3/server/petstore/spring-boot-oneof-sealed/src/main/java/org/openapitools/configuration/EnumConverterConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.openapitools.configuration;
22

3+
import java.math.BigDecimal;
4+
import java.net.URI;
5+
import java.util.UUID;
6+
37
import org.openapitools.model.FruitType;
48

59
import org.springframework.context.annotation.Bean;

samples/openapi3/server/petstore/spring-boot-oneof/src/main/java/org/openapitools/configuration/EnumConverterConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.openapitools.configuration;
22

3+
import java.math.BigDecimal;
4+
import java.net.URI;
5+
import java.util.UUID;
6+
37
import org.openapitools.model.FruitType;
48

59
import org.springframework.context.annotation.Bean;

samples/openapi3/server/petstore/springboot-delegate/src/main/java/org/openapitools/configuration/EnumConverterConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.openapitools.configuration;
22

3+
import java.math.BigDecimal;
4+
import java.net.URI;
5+
import java.util.UUID;
6+
37
import org.openapitools.model.EnumClass;
48
import org.openapitools.model.OuterEnum;
59

samples/openapi3/server/petstore/springboot-implicitHeaders/src/main/java/org/openapitools/configuration/EnumConverterConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.openapitools.configuration;
22

3+
import java.math.BigDecimal;
4+
import java.net.URI;
5+
import java.util.UUID;
6+
37
import org.openapitools.model.EnumClass;
48
import org.openapitools.model.OuterEnum;
59

0 commit comments

Comments
 (0)