Skip to content

Commit e05e4a1

Browse files
committed
[Java][Spring] Insert Annotations to be TYPE_USE Compatible
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
1 parent 8ef4a48 commit e05e4a1

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

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
@@ -28,6 +28,7 @@
2828
import org.apache.commons.lang3.StringUtils;
2929
import org.assertj.core.api.Assertions;
3030
import org.assertj.core.api.MapAssert;
31+
import org.junit.jupiter.api.DisplayName;
3132
import org.openapitools.codegen.*;
3233
import org.openapitools.codegen.config.CodegenConfigurator;
3334
import org.openapitools.codegen.config.GlobalSettings;
@@ -6637,4 +6638,33 @@ public void shouldNotHaveDocumentationAnnotationWhenUsingLibrarySpringHttpInterf
66376638
JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/api/PetApi.java"))
66386639
.assertMethod("addPet").assertParameter("pet").assertParameterAnnotations().doesNotContainWithName("Parameter");
66396640
}
6641+
6642+
@Test
6643+
@DisplayName("Testing Issue #23206: Support JSpecify with SchemaMappings")
6644+
public void testIssue23206() throws IOException {
6645+
final SpringCodegen codegen = new SpringCodegen();
6646+
6647+
codegen.setLibrary("spring-boot");
6648+
6649+
codegen.importMapping().put("org.springframework.lang.Nullable", "org.jspecify.annotations.Nullable");
6650+
codegen.schemaMapping().put("PersonCountValue", "a.b.c.PersonCountValue");
6651+
6652+
codegen.additionalProperties().put(OPENAPI_NULLABLE, "false");
6653+
codegen.additionalProperties().put(SKIP_DEFAULT_INTERFACE, "true");
6654+
codegen.additionalProperties().put(USE_SPRING_BOOT4, "true");
6655+
codegen.additionalProperties().put(USE_JACKSON_3, "true");
6656+
codegen.additionalProperties().put(USE_TAGS, "true");
6657+
codegen.additionalProperties().put(USE_BEANVALIDATION, "false");
6658+
6659+
final Map<String, File> files = generateFiles(codegen, "src/test/resources/3_0/spring/issue_23206.yaml");
6660+
final var javaFileAssert = JavaFileAssert.assertThat(files.get("MyDto.java"));
6661+
6662+
javaFileAssert
6663+
.hasImports("org.jspecify.annotations.Nullable")
6664+
.assertProperty("men");
6665+
6666+
// Should Fail without any changes to the mustache templates!
6667+
javaFileAssert.fileContains("private a.b.c.@Nullable PersonCountValue men;"); // Actual: private @Nullable a.b.c.PersonCountValue men;
6668+
javaFileAssert.fileContains("private a.b.c.@Nullable PersonCountValue women;"); // Actual: private @Nullable a.b.c.PersonCountValue women;
6669+
}
66406670
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: "3.0.3"
2+
info:
3+
title: Move Annotations in Generated POJOs
4+
description: "Test Placement of Annotations, to allow TYPE_USE Annotations to compile correctly. See #23206"
5+
version: 1.0.0
6+
7+
paths:
8+
/some/endpoint:
9+
get:
10+
responses:
11+
"200":
12+
description: OK
13+
14+
components:
15+
schemas:
16+
MyDto:
17+
type: object
18+
properties:
19+
men:
20+
nullable: true
21+
$ref: '#/components/schemas/PersonCountValue'
22+
women:
23+
nullable: true
24+
$ref: '#/components/schemas/PersonCountValue'
25+
PersonCountValue:
26+
type: number
27+
format: personCountValue
28+
example: 1234

0 commit comments

Comments
 (0)