Skip to content

Commit 2764446

Browse files
committed
improve test
add support for spring declarative interface
1 parent 5780ef7 commit 2764446

4 files changed

Lines changed: 130 additions & 4 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ public void processOpts() {
743743
this.setGeneratePageableConstraintValidation(convertPropertyToBoolean(GENERATE_PAGEABLE_CONSTRAINT_VALIDATION));
744744
}
745745
writePropertyBack(GENERATE_PAGEABLE_CONSTRAINT_VALIDATION, generatePageableConstraintValidation);
746-
if (additionalProperties.containsKey(SUBSTITUTE_GENERIC_PAGED_MODEL) && library.equals(SPRING_BOOT)) {
746+
if (additionalProperties.containsKey(SUBSTITUTE_GENERIC_PAGED_MODEL) && (library.equals(SPRING_BOOT) || library.equals(SPRING_DECLARATIVE_HTTP_INTERFACE_LIBRARY))) {
747747
this.setSubstituteGenericPagedModel(convertPropertyToBoolean(SUBSTITUTE_GENERIC_PAGED_MODEL));
748748
}
749749
writePropertyBack(SUBSTITUTE_GENERIC_PAGED_MODEL, substituteGenericPagedModel);
@@ -1204,7 +1204,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
12041204
}
12051205
}
12061206

1207-
if (SPRING_BOOT.equals(library) && substituteGenericPagedModel) {
1207+
if ((SPRING_BOOT.equals(library) || SPRING_DECLARATIVE_HTTP_INTERFACE_LIBRARY.equals(library)) && substituteGenericPagedModel) {
12081208
pagedModelRegistry = PagedModelScanUtils.scanPagedModels(openAPI);
12091209
if (!pagedModelRegistry.isEmpty()) {
12101210
boolean customMapping = importMapping.containsKey("PagedModel");

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,14 @@ public void processOpts() {
590590

591591
convertPropertyToBooleanAndWriteBack(ADDITIONAL_NOT_NULL_ANNOTATIONS, this::setAdditionalNotNullAnnotations);
592592

593+
if (SPRING_BOOT.equals(library) || SPRING_HTTP_INTERFACE.equals(library)) {
594+
convertPropertyToBooleanAndWriteBack(SUBSTITUTE_GENERIC_PAGED_MODEL, this::setSubstituteGenericPagedModel);
595+
}
596+
593597
if (SPRING_BOOT.equals(library)) {
594598
convertPropertyToBooleanAndWriteBack(AUTO_X_SPRING_PAGINATED, this::setAutoXSpringPaginated);
595599
convertPropertyToBooleanAndWriteBack(GENERATE_SORT_VALIDATION, this::setGenerateSortValidation);
596600
convertPropertyToBooleanAndWriteBack(GENERATE_PAGEABLE_CONSTRAINT_VALIDATION, this::setGeneratePageableConstraintValidation);
597-
convertPropertyToBooleanAndWriteBack(SUBSTITUTE_GENERIC_PAGED_MODEL, this::setSubstituteGenericPagedModel);
598601
}
599602

600603
// override parent one
@@ -869,7 +872,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
869872
}
870873
}
871874

872-
if (SPRING_BOOT.equals(library) && substituteGenericPagedModel) {
875+
if ((SPRING_BOOT.equals(library) || SPRING_HTTP_INTERFACE.equals(library)) && substituteGenericPagedModel) {
873876
pagedModelRegistry = PagedModelScanUtils.scanPagedModels(openAPI);
874877
if (!pagedModelRegistry.isEmpty()) {
875878
boolean customMapping = importMapping.containsKey("PagedModel");

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7373,4 +7373,59 @@ public void substituteGenericPagedModel_respectsCustomImportMappingClassName() t
73737373
.assertMethod("listUsers")
73747374
.hasReturnType("ResponseEntity<MyPagedModel<User>>");
73757375
}
7376+
7377+
// substituteGenericPagedModel — spring-http-interface
7378+
// -------------------------------------------------------------------------
7379+
7380+
@Test
7381+
public void substituteGenericPagedModel_springHttpInterface_replacesReturnTypeInOperation() throws IOException {
7382+
Map<String, File> files = generateFromContract(
7383+
"src/test/resources/3_0/spring/petstore-paged-model.yaml", SPRING_HTTP_INTERFACE,
7384+
springHttpInterfacePagedModelProps());
7385+
7386+
JavaFileAssert.assertThat(files.get("UserApi.java"))
7387+
.assertMethod("listUsers")
7388+
.hasReturnType("ResponseEntity<PagedModel<User>>");
7389+
}
7390+
7391+
@Test
7392+
public void substituteGenericPagedModel_springHttpInterface_generatesPagedModelSupportingFile() throws IOException {
7393+
Map<String, File> files = generateFromContract(
7394+
"src/test/resources/3_0/spring/petstore-paged-model.yaml", SPRING_HTTP_INTERFACE,
7395+
springHttpInterfacePagedModelProps());
7396+
7397+
assertThat(files).containsKey("PagedModel.java");
7398+
}
7399+
7400+
@Test
7401+
public void substituteGenericPagedModel_springHttpInterface_doesNotGeneratePagedModelFileWhenCustomMapping() throws IOException {
7402+
Map<String, File> files = generateFromContract(
7403+
"src/test/resources/3_0/spring/petstore-paged-model.yaml", SPRING_HTTP_INTERFACE,
7404+
springHttpInterfacePagedModelProps(),
7405+
configurator -> configurator.addImportMapping("PagedModel", "com.example.custom.MyPagedModel"));
7406+
7407+
assertThat(files).doesNotContainKey("PagedModel.java");
7408+
}
7409+
7410+
@Test
7411+
public void substituteGenericPagedModel_springHttpInterface_respectsCustomImportMappingClassName() throws IOException {
7412+
Map<String, File> files = generateFromContract(
7413+
"src/test/resources/3_0/spring/petstore-paged-model.yaml", SPRING_HTTP_INTERFACE,
7414+
springHttpInterfacePagedModelProps(),
7415+
configurator -> configurator.addImportMapping("PagedModel", "com.example.custom.MyPagedModel"));
7416+
7417+
JavaFileAssert.assertThat(files.get("UserApi.java"))
7418+
.hasImports("com.example.custom.MyPagedModel")
7419+
.assertMethod("listUsers")
7420+
.hasReturnType("ResponseEntity<MyPagedModel<User>>");
7421+
}
7422+
7423+
/** Common properties for substituteGenericPagedModel tests using spring-http-interface. */
7424+
private Map<String, Object> springHttpInterfacePagedModelProps() {
7425+
Map<String, Object> props = new HashMap<>();
7426+
props.put(SpringCodegen.USE_TAGS, "true");
7427+
props.put(SpringCodegen.USE_SPRING_BOOT3, "true");
7428+
props.put(SpringCodegen.SUBSTITUTE_GENERIC_PAGED_MODEL, "true");
7429+
return props;
7430+
}
73767431
}

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5721,4 +5721,72 @@ public void substituteGenericPagedModel_respectsCustomImportMappingClassName() t
57215721
assertThat(content).contains("MyPagedModel<User>");
57225722
assertThat(content).contains("import com.example.custom.MyPagedModel");
57235723
}
5724+
5725+
// substituteGenericPagedModel — spring-declarative-http-interface
5726+
// -------------------------------------------------------------------------
5727+
5728+
@Test
5729+
public void substituteGenericPagedModel_springDeclarativeHttpInterface_replacesReturnTypeInOperation() throws IOException {
5730+
Map<String, File> files = generateFromContract(
5731+
"src/test/resources/3_0/spring/petstore-paged-model.yaml",
5732+
commonDeclarativeHttpInterfacePagedModelProps(),
5733+
new HashMap<>(),
5734+
configurator -> configurator.setLibrary(SPRING_DECLARATIVE_HTTP_INTERFACE_LIBRARY));
5735+
5736+
File userApi = files.get("UserApi.kt");
5737+
assertThat(userApi).isNotNull();
5738+
String content = Files.readString(userApi.toPath());
5739+
assertThat(content).contains("PagedModel<User>");
5740+
}
5741+
5742+
@Test
5743+
public void substituteGenericPagedModel_springDeclarativeHttpInterface_generatesPagedModelSupportingFile() throws IOException {
5744+
Map<String, File> files = generateFromContract(
5745+
"src/test/resources/3_0/spring/petstore-paged-model.yaml",
5746+
commonDeclarativeHttpInterfacePagedModelProps(),
5747+
new HashMap<>(),
5748+
configurator -> configurator.setLibrary(SPRING_DECLARATIVE_HTTP_INTERFACE_LIBRARY));
5749+
5750+
assertThat(files).containsKey("PagedModel.kt");
5751+
}
5752+
5753+
@Test
5754+
public void substituteGenericPagedModel_springDeclarativeHttpInterface_doesNotGeneratePagedModelFileWhenCustomMapping() throws IOException {
5755+
Map<String, File> files = generateFromContract(
5756+
"src/test/resources/3_0/spring/petstore-paged-model.yaml",
5757+
commonDeclarativeHttpInterfacePagedModelProps(),
5758+
new HashMap<>(),
5759+
configurator -> configurator
5760+
.setLibrary(SPRING_DECLARATIVE_HTTP_INTERFACE_LIBRARY)
5761+
.addImportMapping("PagedModel", "com.example.custom.MyPagedModel"));
5762+
5763+
assertThat(files).doesNotContainKey("PagedModel.kt");
5764+
}
5765+
5766+
@Test
5767+
public void substituteGenericPagedModel_springDeclarativeHttpInterface_respectsCustomImportMappingClassName() throws IOException {
5768+
Map<String, File> files = generateFromContract(
5769+
"src/test/resources/3_0/spring/petstore-paged-model.yaml",
5770+
commonDeclarativeHttpInterfacePagedModelProps(),
5771+
new HashMap<>(),
5772+
configurator -> configurator
5773+
.setLibrary(SPRING_DECLARATIVE_HTTP_INTERFACE_LIBRARY)
5774+
.addImportMapping("PagedModel", "com.example.custom.MyPagedModel"));
5775+
5776+
File userApi = files.get("UserApi.kt");
5777+
assertThat(userApi).isNotNull();
5778+
String content = Files.readString(userApi.toPath());
5779+
assertThat(content).contains("MyPagedModel<User>");
5780+
assertThat(content).contains("import com.example.custom.MyPagedModel");
5781+
}
5782+
5783+
/** Common properties for substituteGenericPagedModel tests using spring-declarative-http-interface. */
5784+
private Map<String, Object> commonDeclarativeHttpInterfacePagedModelProps() {
5785+
Map<String, Object> props = new HashMap<>();
5786+
props.put(USE_TAGS, "true");
5787+
props.put(USE_SPRING_BOOT3, "true");
5788+
props.put(SUBSTITUTE_GENERIC_PAGED_MODEL, "true");
5789+
props.put(USE_RESPONSE_ENTITY, "false");
5790+
return props;
5791+
}
57245792
}

0 commit comments

Comments
 (0)