|
6 | 6 | import static org.hamcrest.Matchers.is; |
7 | 7 | import static org.hamcrest.Matchers.isA; |
8 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
9 | 10 | import static org.junit.jupiter.api.Assertions.assertTrue; |
10 | 11 | import static org.mockito.Mockito.mock; |
11 | 12 | import static org.mockito.Mockito.when; |
@@ -139,6 +140,45 @@ void multipartWithFileAndObject() throws IOException { |
139 | 140 | assertThat(filesParam.getTypeAsString(), equalTo("List<MultipartFile>")); |
140 | 141 | } |
141 | 142 |
|
| 143 | + @Test |
| 144 | + void shouldGenerateValidExampleObjectAnnotation() throws IOException { |
| 145 | + var codegen = new BoatSpringCodeGen(); |
| 146 | + var input = new File("src/test/resources/openapi-with-examples/openapi-with-multiple-permissions.yaml"); |
| 147 | + codegen.setLibrary("spring-boot"); |
| 148 | + codegen.setInterfaceOnly(true); |
| 149 | + codegen.setSkipDefaultInterface(true); |
| 150 | + codegen.setOutputDir(TEST_OUTPUT + "/example-object"); |
| 151 | + codegen.setInputSpec(input.getAbsolutePath()); |
| 152 | + codegen.additionalProperties().put(SpringCodegen.USE_SPRING_BOOT3, Boolean.TRUE.toString()); |
| 153 | + |
| 154 | + var openApiInput = new OpenAPIParser().readLocation(input.getAbsolutePath(), null, new ParseOptions()) |
| 155 | + .getOpenAPI(); |
| 156 | + var clientOptInput = new ClientOptInput(); |
| 157 | + clientOptInput.config(codegen); |
| 158 | + clientOptInput.openAPI(openApiInput); |
| 159 | + |
| 160 | + List<File> files = new DefaultGenerator().opts(clientOptInput).generate(); |
| 161 | + |
| 162 | + File apiFile = files.stream() |
| 163 | + .filter(file -> file.getName().endsWith("Api.java")) |
| 164 | + .filter(file -> { |
| 165 | + try { |
| 166 | + return Files.readString(file.toPath()).contains("@ExampleObject("); |
| 167 | + } catch (IOException e) { |
| 168 | + throw new UnhandledException(e); |
| 169 | + } |
| 170 | + }) |
| 171 | + .findFirst() |
| 172 | + .orElseThrow(); |
| 173 | + |
| 174 | + String apiContent = Files.readString(apiFile.toPath()); |
| 175 | + assertTrue(apiContent.contains("@ExampleObject(")); |
| 176 | + assertTrue(apiContent.contains("Value Exceeded. Must be between {min} and {max}.")); |
| 177 | + assertTrue(apiContent.contains("Bad Request")); |
| 178 | + assertFalse(apiContent.contains("value = \"\\\"{")); |
| 179 | + StaticJavaParser.parse(apiFile); |
| 180 | + } |
| 181 | + |
142 | 182 | @Test |
143 | 183 | void testReplaceBeanValidationCollectionType() { |
144 | 184 | var codegen = new BoatSpringCodeGen(); |
|
0 commit comments