@Override public Map<String, Object> postProcessOperations(Map<String, Object> objs) { if ("retrofit".equals(getLibrary()) || "retrofit2".equals(getLibrary())) { Map<String, Object> operations = (Map<String, Object>) objs.get("operations"); if (operations != null) { List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation"); for (CodegenOperation operation : ops) { if (operation.hasConsumes == Boolean.TRUE) { Map<String, String> firstType = operation.consumes.get(0); if (firstType != null) { if ("multipart/form-data".equals(firstType.get("mediaType"))) { operation.isMultipart = Boolean.TRUE; } } } if (operation.returnType == null) { operation.returnType = "Void"; } if ("retrofit2".equals(getLibrary()) && StringUtils.isNotEmpty(operation.path) && operation.path.startsWith("/")) operation.path = operation.path.substring(1); } } } return objs; }
@Override public Map<String, Object> postProcessModels(Map<String, Object> objs) { List<Object> models = (List<Object>) objs.get("models"); for (Object _mo : models) { Map<String, Object> mo = (Map<String, Object>) _mo; CodegenModel cm = (CodegenModel) mo.get("model"); for (CodegenProperty var : cm.vars) { Map<String, Object> allowableValues = var.allowableValues; // handle ArrayProperty if (var.items != null) { allowableValues = var.items.allowableValues; } if (allowableValues == null) { continue; } List<String> values = (List<String>) allowableValues.get("values"); if (values == null) { continue; } // put "enumVars" map into `allowableValues", including `name` and `value` List<Map<String, String>> enumVars = new ArrayList<Map<String, String>>(); String commonPrefix = findCommonPrefixOfVars(values); int truncateIdx = commonPrefix.length(); for (String value : values) { Map<String, String> enumVar = new HashMap<String, String>(); String enumName; if (truncateIdx == 0) { enumName = value; } else { enumName = value.substring(truncateIdx); if ("".equals(enumName)) { enumName = value; } } enumVar.put("name", toEnumVarName(enumName)); enumVar.put("value", value); enumVars.add(enumVar); } allowableValues.put("enumVars", enumVars); // handle default value for enum, e.g. available => StatusEnum.AVAILABLE if (var.defaultValue != null) { String enumName = null; for (Map<String, String> enumVar : enumVars) { if (var.defaultValue.equals(enumVar.get("value"))) { enumName = enumVar.get("name"); break; } } if (enumName != null) { var.defaultValue = var.datatypeWithEnum + "." + enumName; } } } } return objs; }
@Override public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) { objs = super.postProcessModelsEnum(objs); String lib = getLibrary(); if (StringUtils.isEmpty(lib) || "feign".equals(lib) || "jersey2".equals(lib)) { List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports"); List<Object> models = (List<Object>) objs.get("models"); for (Object _mo : models) { Map<String, Object> mo = (Map<String, Object>) _mo; CodegenModel cm = (CodegenModel) mo.get("model"); // for enum model if (Boolean.TRUE.equals(cm.isEnum) && cm.allowableValues != null) { cm.imports.add(importMapping.get("JsonValue")); Map<String, String> item = new HashMap<String, String>(); item.put("import", importMapping.get("JsonValue")); imports.add(item); } } } return objs; }
@Override public Map<String, Object> postProcessOperations(Map<String, Object> objs) { // Remove imports of List, ArrayList, Map and HashMap as they are // imported in the template already. List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports"); Pattern pattern = Pattern.compile("java\\.util\\.(List|ArrayList|Map|HashMap)"); for (Iterator<Map<String, String>> itr = imports.iterator(); itr.hasNext(); ) { String _import = itr.next().get("import"); if (pattern.matcher(_import).matches()) { itr.remove(); } } return objs; }
@Override public Map<String, Object> postProcessOperations(Map<String, Object> objs) { // Remove imports of List, ArrayList, Map and HashMap as they are // imported in the template already. List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports"); Pattern pattern = Pattern.compile("java\\.util\\.(List|ArrayList|Map|HashMap)"); for (Iterator<Map<String, String>> itr = imports.iterator(); itr.hasNext(); ) { String _import = itr.next().get("import"); if (pattern.matcher(_import).matches()) { itr.remove(); } } if (usesAnyRetrofitLibrary()) { Map<String, Object> operations = (Map<String, Object>) objs.get("operations"); if (operations != null) { List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation"); for (CodegenOperation operation : ops) { if (operation.hasConsumes == Boolean.TRUE) { Map<String, String> firstType = operation.consumes.get(0); if (firstType != null) { if ("multipart/form-data".equals(firstType.get("mediaType"))) { operation.isMultipart = Boolean.TRUE; } } } if (operation.returnType == null) { operation.returnType = "Void"; } if (usesRetrofit2Library() && StringUtils.isNotEmpty(operation.path) && operation.path.startsWith("/")) operation.path = operation.path.substring(1); } } } return objs; }
@Override public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) { CodegenModel codegenModel = super.fromModel(name, model, allDefinitions); if (allDefinitions != null && codegenModel != null && codegenModel.parentSchema != null && codegenModel.hasEnums) { final Model parentModel = allDefinitions.get(codegenModel.parentSchema); final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel); codegenModel = this.reconcileInlineEnums(codegenModel, parentCodegenModel); } return codegenModel; }
@Override public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) { CodegenModel codegenModel = super.fromModel(name, model, allDefinitions); if (codegenModel.description != null) { codegenModel.imports.add("ApiModel"); } if (allDefinitions != null && codegenModel != null && codegenModel.parentSchema != null && codegenModel.hasEnums) { final Model parentModel = allDefinitions.get(codegenModel.parentSchema); final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel); codegenModel = AbstractJavaCodegen.reconcileInlineEnums(codegenModel, parentCodegenModel); } return codegenModel; }