Conditional import of AtomicInteger added to delegate template. fix #21566 (#21597)

This commit is contained in:
Mikko Maunu
2025-07-21 11:27:33 +02:00
committed by GitHub
parent b7fb3b2107
commit 90001d4fac
3 changed files with 80 additions and 0 deletions

View File

@@ -27,6 +27,9 @@ import java.util.Optional;
{{#async}}
import java.util.concurrent.CompletableFuture;
{{/async}}
{{#returnSuccessCode}}
import java.util.concurrent.atomic.AtomicInteger;
{{/returnSuccessCode}}
import {{javaxPackage}}.annotation.Generated;
{{#operations}}

View File

@@ -5575,4 +5575,36 @@ public class SpringCodegenTest {
.toFileAssert()
.assertMethod("findPetsByStatus").hasReturnType("List<Pet>");
}
@Test
public void testHasRestControllerDoesNotHaveController_issue21156() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_1/issue_21156.yaml");
final SpringCodegen codegen = new SpringCodegen();
codegen.setOpenAPI(openAPI);
codegen.setOutputDir(output.getAbsolutePath());
codegen.setLibrary("spring-boot");
codegen.additionalProperties().put(INTERFACE_ONLY, "false");
codegen.additionalProperties().put(DELEGATE_PATTERN, "true");
codegen.additionalProperties().put(SPRING_CONTROLLER, "true");
codegen.additionalProperties().put(RETURN_SUCCESS_CODE, "true");
ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);
DefaultGenerator generator = new DefaultGenerator();
generator.setGenerateMetadata(false); // skip metadata generation
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));
JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("TestApiDelegate.java"));
javaFileAssert
.hasImports("java.util.concurrent.atomic.AtomicInteger");
}
}

View File

@@ -0,0 +1,45 @@
openapi: 3.1.1
info:
title: Test Bug
version: 1.0.0
servers:
- url: https://where.am.i
paths:
/test/bug:
post:
operationId: testRequest
requestBody:
content:
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/TestRequest'
responses:
'200':
description: Test request successful
content:
application/json:
schema:
$ref: '#/components/schemas/TestResponse'
'500':
$ref: '#/components/responses/InternalServerError'
components:
schemas:
TestRequest:
properties:
test:
type: string
TestResponse:
properties:
test:
type: string
ErrorResponse:
properties:
error:
type: string
responses:
InternalServerError:
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
description: Internal server error