|
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; |
|
48 | 49 | import java.util.Set; |
49 | 50 | import java.util.UUID; |
50 | 51 | import java.util.stream.Collectors; |
| 52 | +import java.util.stream.Stream; |
51 | 53 | import org.apache.commons.io.FileUtils; |
52 | 54 | import org.apache.commons.lang.UnhandledException; |
53 | 55 | import org.hamcrest.Matchers; |
54 | 56 | import org.jetbrains.annotations.NotNull; |
55 | 57 | import org.junit.jupiter.api.BeforeAll; |
56 | 58 | import org.junit.jupiter.api.Test; |
57 | 59 | import org.junit.jupiter.params.ParameterizedTest; |
| 60 | +import org.junit.jupiter.params.provider.Arguments; |
58 | 61 | import org.junit.jupiter.params.provider.CsvSource; |
| 62 | +import org.junit.jupiter.params.provider.MethodSource; |
59 | 63 | import org.openapitools.codegen.CliOption; |
60 | 64 | import org.openapitools.codegen.ClientOptInput; |
61 | 65 | import org.openapitools.codegen.CodegenOperation; |
@@ -97,6 +101,20 @@ void newLineIndent() throws IOException { |
97 | 101 | assertThat(output.toString(), equalTo(String.format("__%n__Good%n__ morning,%n__ Dave%n"))); |
98 | 102 | } |
99 | 103 |
|
| 104 | + @ParameterizedTest |
| 105 | + @MethodSource("unwrapEscapedQuotesCases") |
| 106 | + void unwrapEscapedQuotes_execute_shouldHandleAllScenarios(String input, String expectedOutput) throws IOException { |
| 107 | + final BoatSpringCodeGen.UnwrapEscapedQuotes lambda = new BoatSpringCodeGen.UnwrapEscapedQuotes(); |
| 108 | + final StringWriter output = new StringWriter(); |
| 109 | + final Fragment frag = mock(Fragment.class); |
| 110 | + |
| 111 | + when(frag.execute()).thenReturn(input); |
| 112 | + |
| 113 | + lambda.execute(frag, output); |
| 114 | + |
| 115 | + assertThat(output.toString(), equalTo(expectedOutput)); |
| 116 | + } |
| 117 | + |
100 | 118 | @Test |
101 | 119 | void addServletRequestTestFromOperation(){ |
102 | 120 | final BoatSpringCodeGen gen = new BoatSpringCodeGen(); |
@@ -139,6 +157,45 @@ void multipartWithFileAndObject() throws IOException { |
139 | 157 | assertThat(filesParam.getTypeAsString(), equalTo("List<MultipartFile>")); |
140 | 158 | } |
141 | 159 |
|
| 160 | + @Test |
| 161 | + void shouldGenerateValidExampleObjectAnnotation() throws IOException { |
| 162 | + var codegen = new BoatSpringCodeGen(); |
| 163 | + var input = new File("src/test/resources/openapi-with-examples/openapi-with-multiple-permissions.yaml"); |
| 164 | + codegen.setLibrary("spring-boot"); |
| 165 | + codegen.setInterfaceOnly(true); |
| 166 | + codegen.setSkipDefaultInterface(true); |
| 167 | + codegen.setOutputDir(TEST_OUTPUT + "/example-object"); |
| 168 | + codegen.setInputSpec(input.getAbsolutePath()); |
| 169 | + codegen.additionalProperties().put(SpringCodegen.USE_SPRING_BOOT3, Boolean.TRUE.toString()); |
| 170 | + |
| 171 | + var openApiInput = new OpenAPIParser().readLocation(input.getAbsolutePath(), null, new ParseOptions()) |
| 172 | + .getOpenAPI(); |
| 173 | + var clientOptInput = new ClientOptInput(); |
| 174 | + clientOptInput.config(codegen); |
| 175 | + clientOptInput.openAPI(openApiInput); |
| 176 | + |
| 177 | + List<File> files = new DefaultGenerator().opts(clientOptInput).generate(); |
| 178 | + |
| 179 | + File apiFile = files.stream() |
| 180 | + .filter(file -> file.getName().endsWith("Api.java")) |
| 181 | + .filter(file -> { |
| 182 | + try { |
| 183 | + return Files.readString(file.toPath()).contains("@ExampleObject("); |
| 184 | + } catch (IOException e) { |
| 185 | + throw new UnhandledException(e); |
| 186 | + } |
| 187 | + }) |
| 188 | + .findFirst() |
| 189 | + .orElseThrow(); |
| 190 | + |
| 191 | + String apiContent = Files.readString(apiFile.toPath()); |
| 192 | + assertTrue(apiContent.contains("@ExampleObject(")); |
| 193 | + assertTrue(apiContent.contains("Value Exceeded. Must be between {min} and {max}.")); |
| 194 | + assertTrue(apiContent.contains("Bad Request")); |
| 195 | + assertFalse(apiContent.contains("value = \"\\\"{")); |
| 196 | + StaticJavaParser.parse(apiFile); |
| 197 | + } |
| 198 | + |
142 | 199 | @Test |
143 | 200 | void testReplaceBeanValidationCollectionType() { |
144 | 201 | var codegen = new BoatSpringCodeGen(); |
@@ -552,4 +609,12 @@ private static void assertMethodCollectionReturnType(MethodDeclaration method, S |
552 | 609 | .getTypeArguments().get().getFirst().get(); |
553 | 610 | assertEquals(itemType, collectionItemType.getName().toString()); |
554 | 611 | } |
| 612 | + |
| 613 | + static Stream<Arguments> unwrapEscapedQuotesCases() { |
| 614 | + return Stream.of( |
| 615 | + Arguments.of((String) null, ""), |
| 616 | + Arguments.of("\\\"{\\\"message\\\":\\\"Bad Request\\\"}\\\"", "{\\\"message\\\":\\\"Bad Request\\\"}"), |
| 617 | + Arguments.of("prefix\\\\\"quoted\\\\\"suffix", "prefix\\\"quoted\\\"suffix"), |
| 618 | + Arguments.of("\\\"", "\\\"")); |
| 619 | + } |
555 | 620 | } |
0 commit comments