|
19 | 19 |
|
20 | 20 | import io.swagger.v3.oas.models.Components; |
21 | 21 | import io.swagger.v3.oas.models.OpenAPI; |
| 22 | +import io.swagger.v3.oas.models.Operation; |
22 | 23 | import io.swagger.v3.oas.models.media.*; |
| 24 | +import io.swagger.v3.oas.models.parameters.Parameter; |
| 25 | +import io.swagger.v3.oas.models.parameters.RequestBody; |
23 | 26 | import org.mockito.Answers; |
| 27 | +import org.openapitools.codegen.*; |
24 | 28 | import org.openapitools.codegen.CodegenConstants; |
25 | 29 | import org.openapitools.codegen.languages.AbstractGoCodegen; |
| 30 | +import org.openapitools.codegen.model.OperationMap; |
| 31 | +import org.openapitools.codegen.model.OperationsMap; |
| 32 | +import org.openapitools.codegen.model.WebhooksMap; |
26 | 33 | import org.openapitools.codegen.utils.ModelUtils; |
27 | 34 | import org.testng.Assert; |
28 | 35 | import org.testng.annotations.BeforeMethod; |
29 | 36 | import org.testng.annotations.Test; |
30 | 37 |
|
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | + |
31 | 43 | import static org.mockito.Mockito.mock; |
32 | 44 | import static org.mockito.Mockito.withSettings; |
33 | 45 |
|
@@ -108,4 +120,91 @@ public void getTypeDeclarationTest() { |
108 | 120 | defaultValue = codegen.getTypeDeclaration(schema); |
109 | 121 | Assert.assertEquals(defaultValue, "map[string]interface{}"); |
110 | 122 | } |
| 123 | + |
| 124 | + @Test(description = "test that os import is added for array of binary parameters in operations") |
| 125 | + public void testOsImportForArrayOfBinaryParametersInOperations() { |
| 126 | + // Create OpenAPI spec with array of binary files |
| 127 | + OpenAPI openAPI = new OpenAPI(); |
| 128 | + openAPI.setComponents(new Components()); |
| 129 | + |
| 130 | + // Create operation with array of binary parameter in request body |
| 131 | + Operation operation = new Operation(); |
| 132 | + RequestBody requestBody = new RequestBody(); |
| 133 | + Content content = new Content(); |
| 134 | + MediaType mediaType = new MediaType(); |
| 135 | + |
| 136 | + Schema<?> arraySchema = new ArraySchema().items( |
| 137 | + new BinarySchema() |
| 138 | + ); |
| 139 | + |
| 140 | + ObjectSchema objectSchema = new ObjectSchema(); |
| 141 | + objectSchema.addProperty("files", arraySchema); |
| 142 | + mediaType.setSchema(objectSchema); |
| 143 | + content.addMediaType("multipart/form-data", mediaType); |
| 144 | + requestBody.setContent(content); |
| 145 | + operation.setRequestBody(requestBody); |
| 146 | + |
| 147 | + codegen.setOpenAPI(openAPI); |
| 148 | + |
| 149 | + // Convert to CodegenOperation |
| 150 | + CodegenOperation codegenOperation = codegen.fromOperation("/upload", "post", operation, null); |
| 151 | + |
| 152 | + // Create OperationsMap structure |
| 153 | + OperationMap operationMap = new OperationMap(); |
| 154 | + operationMap.setOperation(codegenOperation); |
| 155 | + OperationsMap operationsMap = new OperationsMap(); |
| 156 | + operationsMap.setOperation(operationMap); |
| 157 | + operationsMap.setImports(new ArrayList<>()); |
| 158 | + |
| 159 | + // Post-process the operations |
| 160 | + OperationsMap result = codegen.postProcessOperationsWithModels(operationsMap, Collections.emptyList()); |
| 161 | + |
| 162 | + // Assert that "os" import was added |
| 163 | + List<Map<String, String>> imports = result.getImports(); |
| 164 | + boolean hasOsImport = imports.stream() |
| 165 | + .anyMatch(imp -> "os".equals(imp.get("import"))); |
| 166 | + |
| 167 | + Assert.assertTrue(hasOsImport, "Expected 'os' import to be added for array of binary files"); |
| 168 | + } |
| 169 | + |
| 170 | + @Test(description = "test that time import is added for array of date-time parameters in operations") |
| 171 | + public void testTimeImportForArrayOfDateTimeParametersInOperations() { |
| 172 | + // Create OpenAPI spec with array of date-time parameter |
| 173 | + OpenAPI openAPI = new OpenAPI(); |
| 174 | + openAPI.setComponents(new Components()); |
| 175 | + |
| 176 | + Operation operation = new Operation(); |
| 177 | + Parameter parameter = new Parameter(); |
| 178 | + parameter.setName("timestamps"); |
| 179 | + parameter.setIn("query"); |
| 180 | + |
| 181 | + ArraySchema arraySchema = new ArraySchema(); |
| 182 | + DateTimeSchema dateTimeSchema = new DateTimeSchema(); |
| 183 | + arraySchema.setItems(dateTimeSchema); |
| 184 | + parameter.setSchema(arraySchema); |
| 185 | + |
| 186 | + operation.addParametersItem(parameter); |
| 187 | + |
| 188 | + codegen.setOpenAPI(openAPI); |
| 189 | + |
| 190 | + // Convert to CodegenOperation |
| 191 | + CodegenOperation codegenOperation = codegen.fromOperation("/events", "get", operation, null); |
| 192 | + |
| 193 | + // Create OperationsMap structure |
| 194 | + OperationMap operationMap = new OperationMap(); |
| 195 | + operationMap.setOperation(codegenOperation); |
| 196 | + OperationsMap operationsMap = new OperationsMap(); |
| 197 | + operationsMap.setOperation(operationMap); |
| 198 | + operationsMap.setImports(new ArrayList<>()); |
| 199 | + |
| 200 | + // Post-process the operations |
| 201 | + OperationsMap result = codegen.postProcessOperationsWithModels(operationsMap, Collections.emptyList()); |
| 202 | + |
| 203 | + // Assert that "time" import was added |
| 204 | + List<Map<String, String>> imports = result.getImports(); |
| 205 | + boolean hasTimeImport = imports.stream() |
| 206 | + .anyMatch(imp -> "time".equals(imp.get("import"))); |
| 207 | + |
| 208 | + Assert.assertTrue(hasTimeImport, "Expected 'time' import to be added for array of date-time parameters"); |
| 209 | + } |
111 | 210 | } |
0 commit comments