|
31 | 31 | import java.io.File;
|
32 | 32 | import java.util.*;
|
33 | 33 |
|
| 34 | +import static org.openapitools.codegen.utils.StringUtils.camelize; |
| 35 | + |
34 | 36 | public class GoServerCodegen extends AbstractGoCodegen {
|
35 | 37 |
|
36 | 38 | /**
|
@@ -322,34 +324,77 @@ public ModelsMap postProcessModels(ModelsMap objs) {
|
322 | 324 |
|
323 | 325 | @Override
|
324 | 326 | public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
|
325 |
| - objs = super.postProcessOperationsWithModels(objs, allModels); |
| 327 | + // TODO: refactor abstractGoCodegen, decouple go client only code and remove this |
326 | 328 | OperationMap objectMap = objs.getOperations();
|
327 | 329 | List<CodegenOperation> operations = objectMap.getOperation();
|
328 | 330 |
|
| 331 | + for (CodegenOperation operation : operations) { |
| 332 | + // http method verb conversion (e.g. PUT => Put) |
| 333 | + operation.httpMethod = camelize(operation.httpMethod.toLowerCase(Locale.ROOT)); |
| 334 | + } |
| 335 | + |
| 336 | + // remove model imports to avoid error |
329 | 337 | List<Map<String, String>> imports = objs.getImports();
|
330 | 338 | if (imports == null)
|
331 | 339 | return objs;
|
332 | 340 |
|
333 |
| - // override imports to only include packages for interface parameters |
334 |
| - imports.clear(); |
| 341 | + Iterator<Map<String, String>> iterator = imports.iterator(); |
| 342 | + while (iterator.hasNext()) { |
| 343 | + String _import = iterator.next().get("import"); |
| 344 | + if (_import.startsWith(apiPackage)) |
| 345 | + iterator.remove(); |
| 346 | + } |
335 | 347 |
|
336 | 348 | boolean addedTimeImport = false;
|
337 | 349 | boolean addedOSImport = false;
|
| 350 | + boolean addedReflectImport = false; |
338 | 351 | for (CodegenOperation operation : operations) {
|
339 | 352 | for (CodegenParameter param : operation.allParams) {
|
340 | 353 | // import "os" if the operation uses files
|
341 |
| - if (!addedOSImport && ("*os.File".equals(param.dataType) || ("[]*os.File".equals(param.dataType)))) { |
| 354 | + if (!addedOSImport && ("*os.File".equals(param.dataType) || "[]*os.File".equals(param.dataType))) { |
342 | 355 | imports.add(createMapping("import", "os"));
|
343 | 356 | addedOSImport = true;
|
344 | 357 | }
|
345 | 358 |
|
346 |
| - // import "time" if the operation has a required time parameter |
347 |
| - if (param.required) { |
348 |
| - if (!addedTimeImport && "time.Time".equals(param.dataType)) { |
349 |
| - imports.add(createMapping("import", "time")); |
350 |
| - addedTimeImport = true; |
351 |
| - } |
| 359 | + // import "time" if the operation has a time parameter. |
| 360 | + if (!addedTimeImport && "time.Time".equals(param.dataType)) { |
| 361 | + imports.add(createMapping("import", "time")); |
| 362 | + addedTimeImport = true; |
| 363 | + } |
| 364 | + |
| 365 | + // import "reflect" package if the parameter is collectionFormat=multi |
| 366 | + if (!addedReflectImport && param.isCollectionFormatMulti) { |
| 367 | + imports.add(createMapping("import", "reflect")); |
| 368 | + addedReflectImport = true; |
352 | 369 | }
|
| 370 | + |
| 371 | + // set x-exportParamName |
| 372 | + char nameFirstChar = param.paramName.charAt(0); |
| 373 | + if (Character.isUpperCase(nameFirstChar)) { |
| 374 | + // First char is already uppercase, just use paramName. |
| 375 | + param.vendorExtensions.put("x-export-param-name", param.paramName); |
| 376 | + } else { |
| 377 | + // It's a lowercase first char, let's convert it to uppercase |
| 378 | + StringBuilder sb = new StringBuilder(param.paramName); |
| 379 | + sb.setCharAt(0, Character.toUpperCase(nameFirstChar)); |
| 380 | + param.vendorExtensions.put("x-export-param-name", sb.toString()); |
| 381 | + } |
| 382 | + } |
| 383 | + |
| 384 | + } |
| 385 | + |
| 386 | + // recursively add import for mapping one type to multiple imports |
| 387 | + List<Map<String, String>> recursiveImports = objs.getImports(); |
| 388 | + if (recursiveImports == null) |
| 389 | + return objs; |
| 390 | + |
| 391 | + ListIterator<Map<String, String>> listIterator = imports.listIterator(); |
| 392 | + while (listIterator.hasNext()) { |
| 393 | + String _import = listIterator.next().get("import"); |
| 394 | + // if the import package happens to be found in the importMapping (key) |
| 395 | + // add the corresponding import package to the list |
| 396 | + if (importMapping.containsKey(_import)) { |
| 397 | + listIterator.add(createMapping("import", importMapping.get(_import))); |
353 | 398 | }
|
354 | 399 | }
|
355 | 400 |
|
|
0 commit comments