Skip to content

Commit 465c959

Browse files
committed
add unit tests
1 parent b9cb91e commit 465c959

1 file changed

Lines changed: 144 additions & 0 deletions

File tree

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

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6223,4 +6223,148 @@ public void testExtensionsOnSchema_issue9183() throws IOException {
62236223
));
62246224
}
62256225

6226+
@Test
6227+
public void testSpringHttpInterfaceWithBeanValidationEnabled() throws IOException {
6228+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
6229+
output.deleteOnExit();
6230+
String outputPath = output.getAbsolutePath().replace('\\', '/');
6231+
6232+
final OpenAPI openAPI = new OpenAPIParser()
6233+
.readLocation("src/test/resources/3_0/petstore.yaml", null, new ParseOptions()).getOpenAPI();
6234+
6235+
final SpringCodegen codegen = new SpringCodegen();
6236+
codegen.setOutputDir(output.getAbsolutePath());
6237+
codegen.additionalProperties().put(CodegenConstants.LIBRARY, SpringCodegen.SPRING_HTTP_INTERFACE);
6238+
codegen.additionalProperties().put(BeanValidationFeatures.USE_BEANVALIDATION, "true");
6239+
6240+
ClientOptInput input = new ClientOptInput();
6241+
input.openAPI(openAPI);
6242+
input.config(codegen);
6243+
6244+
DefaultGenerator generator = new DefaultGenerator();
6245+
generator.setGenerateMetadata(false);
6246+
Map<String, File> files = generator.opts(input).generate().stream()
6247+
.collect(Collectors.toMap(File::getName, Function.identity()));
6248+
6249+
// Verify that @Validated annotation is present in the generated API interface
6250+
assertFileContains(files.get("PetApi.java").toPath(), "@Validated");
6251+
// Verify that @Valid annotation is present in method parameters
6252+
assertFileContains(files.get("PetApi.java").toPath(), "@Valid");
6253+
}
6254+
6255+
@Test
6256+
public void testSpringHttpInterfaceWithBeanValidationDisabled() throws IOException {
6257+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
6258+
output.deleteOnExit();
6259+
String outputPath = output.getAbsolutePath().replace('\\', '/');
6260+
6261+
final OpenAPI openAPI = new OpenAPIParser()
6262+
.readLocation("src/test/resources/3_0/petstore.yaml", null, new ParseOptions()).getOpenAPI();
6263+
6264+
final SpringCodegen codegen = new SpringCodegen();
6265+
codegen.setOutputDir(output.getAbsolutePath());
6266+
codegen.additionalProperties().put(CodegenConstants.LIBRARY, SpringCodegen.SPRING_HTTP_INTERFACE);
6267+
codegen.additionalProperties().put(BeanValidationFeatures.USE_BEANVALIDATION, "false");
6268+
6269+
ClientOptInput input = new ClientOptInput();
6270+
input.openAPI(openAPI);
6271+
input.config(codegen);
6272+
6273+
DefaultGenerator generator = new DefaultGenerator();
6274+
generator.setGenerateMetadata(false);
6275+
Map<String, File> files = generator.opts(input).generate().stream()
6276+
.collect(Collectors.toMap(File::getName, Function.identity()));
6277+
6278+
// Verify that @Validated annotation is NOT present in the generated API interface
6279+
assertFileNotContains(files.get("PetApi.java").toPath(), "@Validated");
6280+
// Verify that @Valid annotation is NOT present in method parameters
6281+
assertFileNotContains(files.get("PetApi.java").toPath(), "@Valid");
6282+
}
6283+
6284+
@Test
6285+
public void testSpringHttpInterfaceWithBeanValidationNotSpecified() throws IOException {
6286+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
6287+
output.deleteOnExit();
6288+
String outputPath = output.getAbsolutePath().replace('\\', '/');
6289+
6290+
final OpenAPI openAPI = new OpenAPIParser()
6291+
.readLocation("src/test/resources/3_0/petstore.yaml", null, new ParseOptions()).getOpenAPI();
6292+
6293+
final SpringCodegen codegen = new SpringCodegen();
6294+
codegen.setOutputDir(output.getAbsolutePath());
6295+
codegen.additionalProperties().put(CodegenConstants.LIBRARY, SpringCodegen.SPRING_HTTP_INTERFACE);
6296+
// Don't set USE_BEANVALIDATION, verify it defaults to false
6297+
codegen.processOpts();
6298+
6299+
ClientOptInput input = new ClientOptInput();
6300+
input.openAPI(openAPI);
6301+
input.config(codegen);
6302+
6303+
DefaultGenerator generator = new DefaultGenerator();
6304+
generator.setGenerateMetadata(false);
6305+
Map<String, File> files = generator.opts(input).generate().stream().distinct()
6306+
.collect(Collectors.toMap(File::getName, Function.identity()));
6307+
6308+
// Verify that useBeanValidation defaults to false, so no @Validated annotation
6309+
assertFileNotContains(files.get("PetApi.java").toPath(), "@Validated");
6310+
assertFileNotContains(files.get("PetApi.java").toPath(), "@Valid");
6311+
}
6312+
6313+
@Test
6314+
public void testSpringHttpInterfaceWithBeanValidationEnabledContainsAnnotations() throws IOException {
6315+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
6316+
output.deleteOnExit();
6317+
6318+
final OpenAPI openAPI = new OpenAPIParser()
6319+
.readLocation("src/test/resources/3_0/petstore.yaml", null, new ParseOptions()).getOpenAPI();
6320+
6321+
final SpringCodegen codegen = new SpringCodegen();
6322+
codegen.setOutputDir(output.getAbsolutePath());
6323+
codegen.additionalProperties().put(CodegenConstants.LIBRARY, SpringCodegen.SPRING_HTTP_INTERFACE);
6324+
codegen.additionalProperties().put(BeanValidationFeatures.USE_BEANVALIDATION, "true");
6325+
6326+
ClientOptInput input = new ClientOptInput();
6327+
input.openAPI(openAPI);
6328+
input.config(codegen);
6329+
6330+
DefaultGenerator generator = new DefaultGenerator();
6331+
generator.setGenerateMetadata(false);
6332+
Map<String, File> files = generator.opts(input).generate().stream()
6333+
.collect(Collectors.toMap(File::getName, Function.identity()));
6334+
6335+
// Check that the generated interface contains @Validated at class level
6336+
String apiContent = new String(Files.readAllBytes(files.get("PetApi.java").toPath()));
6337+
assertThat(apiContent).contains("@Validated");
6338+
assertThat(apiContent).contains("import jakarta.validation");
6339+
}
6340+
6341+
@Test
6342+
public void testSpringHttpInterfaceWithBeanValidationDisabledNoValidationImports() throws IOException {
6343+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
6344+
output.deleteOnExit();
6345+
6346+
final OpenAPI openAPI = new OpenAPIParser()
6347+
.readLocation("src/test/resources/3_0/petstore.yaml", null, new ParseOptions()).getOpenAPI();
6348+
6349+
final SpringCodegen codegen = new SpringCodegen();
6350+
codegen.setOutputDir(output.getAbsolutePath());
6351+
codegen.additionalProperties().put(CodegenConstants.LIBRARY, SpringCodegen.SPRING_HTTP_INTERFACE);
6352+
codegen.additionalProperties().put(BeanValidationFeatures.USE_BEANVALIDATION, "false");
6353+
6354+
ClientOptInput input = new ClientOptInput();
6355+
input.openAPI(openAPI);
6356+
input.config(codegen);
6357+
6358+
DefaultGenerator generator = new DefaultGenerator();
6359+
generator.setGenerateMetadata(false);
6360+
Map<String, File> files = generator.opts(input).generate().stream()
6361+
.collect(Collectors.toMap(File::getName, Function.identity()));
6362+
6363+
// Check that the generated interface does NOT contain @Validated
6364+
String apiContent = new String(Files.readAllBytes(files.get("PetApi.java").toPath()));
6365+
assertThat(apiContent).doesNotContain("@Validated");
6366+
// Should still have proper formatting
6367+
assertThat(apiContent).contains("public interface PetApi");
6368+
}
6369+
62266370
}

0 commit comments

Comments
 (0)