diff --git a/.gitignore b/.gitignore index d864b9076a35..5cd2860e36fe 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ packages/ /target /generated-files +test-output/ nbactions.xml # scalatra diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java index 34c5026f64ff..ac39f59be341 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java @@ -37,6 +37,14 @@ import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.config.GeneratorProperties; import org.openapitools.codegen.ignore.CodegenIgnoreProcessor; +//import org.openapitools.codegen.languages.AbstractJavaCodegen; +import org.openapitools.codegen.templating.ApiBundle; +import org.openapitools.codegen.templating.ApiInfoBundle; +import org.openapitools.codegen.templating.Bundle; +import org.openapitools.codegen.templating.ModelBundle; +import org.openapitools.codegen.templating.ModelsBundle; +import org.openapitools.codegen.templating.OperationsBundle; +import org.openapitools.codegen.templating.TemplatingInputBundle; import org.openapitools.codegen.utils.ImplementationVersion; import org.openapitools.codegen.utils.ModelUtils; import org.openapitools.codegen.utils.URLPathUtils; @@ -261,7 +269,7 @@ private void configureOpenAPIInfo() { } } - private void generateModelTests(List files, Map models, String modelName) throws IOException { + private void generateModelTests(List files, ModelsBundle models, String modelName) throws IOException { // to generate model test files for (String templateName : config.modelTestTemplateFiles().keySet()) { String suffix = config.modelTestTemplateFiles().get(templateName); @@ -281,7 +289,7 @@ private void generateModelTests(List files, Map models, St } } - private void generateModelDocumentation(List files, Map models, String modelName) throws IOException { + private void generateModelDocumentation(List files, ModelsBundle models, String modelName) throws IOException { for (String templateName : config.modelDocTemplateFiles().keySet()) { String docExtension = config.getDocExtension(); String suffix = docExtension != null ? docExtension : config.modelDocTemplateFiles().get(templateName); @@ -300,7 +308,7 @@ private void generateModelDocumentation(List files, Map mo } } - private void generateModel(List files, Map models, String modelName) throws IOException { + private void generateModel(List files, ModelsBundle models, String modelName) throws IOException { for (String templateName : config.modelTemplateFiles().keySet()) { String suffix = config.modelTemplateFiles().get(templateName); String filename = config.modelFileFolder() + File.separator + config.toModelFilename(modelName) + suffix; @@ -318,7 +326,7 @@ private void generateModel(List files, Map models, String } } - private void generateModels(List files, List allModels, List unusedModels) { + private void generateModels(List files, List allModels, List unusedModels) { if (!generateModels) { return; } @@ -347,7 +355,7 @@ private void generateModels(List files, List allModels, List allProcessedModels = new TreeMap(new Comparator() { + Map allProcessedModels = new TreeMap(new Comparator() { @Override public int compare(String o1, String o2) { return ObjectUtils.compare(config.toModelName(o1), config.toModelName(o2)); @@ -446,8 +454,8 @@ private Model getParent(Model model) { Map schemaMap = new HashMap<>(); schemaMap.put(name, schema); - Map models = processModels(config, schemaMap); - models.put("classname", config.toModelName(name)); + ModelsBundle models = processModels(config, schemaMap); + models.setClassname(config.toModelName(name)); models.putAll(config.additionalProperties()); allProcessedModels.put(name, models); } catch (Exception e) { @@ -456,15 +464,22 @@ private Model getParent(Model model) { } // loop through all models to update children models, isSelfReference, isCircularReference, etc - allProcessedModels = config.updateAllModels(allProcessedModels); + Map processed = new LinkedHashMap(allProcessedModels); + processed = config.updateAllModels(processed); // post process all processed models - allProcessedModels = config.postProcessAllModels(allProcessedModels); + processed = config.postProcessAllModels(processed); + + allProcessedModels = new LinkedHashMap(); + for (Map.Entry entry: processed.entrySet()) { + allProcessedModels.put(entry.getKey(), new ModelsBundle((Map) entry.getValue())); + } // generate files based on processed models for (String modelName : allProcessedModels.keySet()) { - Map models = (Map) allProcessedModels.get(modelName); - models.put("modelPackage", config.modelPackage()); + ModelsBundle models = allProcessedModels.get(modelName); + models.setModelPackage(config.modelPackage()); + models.putAll(config.additionalProperties()); try { //don't generate models that have an import mapping if (config.importMapping().containsKey(modelName)) { @@ -472,10 +487,10 @@ private Model getParent(Model model) { } // TODO revise below as we've already performed unaliasing so that the isAlias check may be removed - Map modelTemplate = (Map) ((List) models.get("models")).get(0); + ModelBundle modelTemplate = models.getModels().get(0); // Special handling of aliases only applies to Java - if (modelTemplate != null && modelTemplate.containsKey("model")) { - CodegenModel m = (CodegenModel) modelTemplate.get("model"); + if (modelTemplate != null) { + CodegenModel m = modelTemplate.getModel(); if (m.isAlias) { continue; // Don't create user-defined classes for aliases } @@ -505,7 +520,7 @@ private Model getParent(Model model) { } - private void generateApis(List files, List allOperations, List allModels) { + private void generateApis(List files, List allOperations, List allModels) { if (!generateApis) { return; } @@ -533,19 +548,19 @@ public int compare(CodegenOperation one, CodegenOperation another) { return ObjectUtils.compare(one.operationId, another.operationId); } }); - Map operation = processOperations(config, tag, ops, allModels); + ApiBundle operation = processOperations(config, tag, ops, allModels); URL url = URLPathUtils.getServerURL(openAPI); - operation.put("basePath", basePath); - operation.put("basePathWithoutHost", config.encodePath(url.getPath()).replaceAll("/$", "")); - operation.put("contextPath", contextPath); - operation.put("baseName", tag); - operation.put("apiPackage", config.apiPackage()); - operation.put("modelPackage", config.modelPackage()); + operation.setBasePath(basePath); + operation.setBasePathWithoutHost(config.encodePath(url.getPath()).replaceAll("/$", "")); + operation.setContextPath(contextPath); + operation.setBaseName(tag); + operation.setApiPackage(config.apiPackage()); + operation.setModelPackage(config.modelPackage()); operation.putAll(config.additionalProperties()); - operation.put("classname", config.toApiName(tag)); - operation.put("classVarName", config.toApiVarName(tag)); - operation.put("importPath", config.toApiImport(tag)); - operation.put("classFilename", config.toApiFilename(tag)); + operation.setClassname(config.toApiName(tag)); + operation.setClassVarName(config.toApiVarName(tag)); + operation.setImportPath(config.toApiImport(tag)); + operation.setClassFilename(config.toApiFilename(tag)); if (allModels == null || allModels.isEmpty()) { operation.put("hasModel", false); @@ -554,7 +569,7 @@ public int compare(CodegenOperation one, CodegenOperation another) { } if (!config.vendorExtensions().isEmpty()) { - operation.put("vendorExtensions", config.vendorExtensions()); + operation.setVendorExtensions(config.vendorExtensions()); } // process top-level x-group-parameters @@ -576,18 +591,18 @@ public int compare(CodegenOperation one, CodegenOperation another) { if (this.config.additionalProperties().containsKey(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG)) { sortParamsByRequiredFlag = Boolean.valueOf(this.config.additionalProperties().get(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG).toString()); } - operation.put("sortParamsByRequiredFlag", sortParamsByRequiredFlag); + operation.setSortParamsByRequiredFlag(sortParamsByRequiredFlag); /* consumes, produces are no longer defined in OAS3.0 processMimeTypes(swagger.getConsumes(), operation, "consumes"); processMimeTypes(swagger.getProduces(), operation, "produces"); */ - allOperations.add(new HashMap(operation)); + allOperations.add(operation); for (int i = 0; i < allOperations.size(); i++) { - Map oo = (Map) allOperations.get(i); + ApiBundle oo = allOperations.get(i); if (i < (allOperations.size() - 1)) { - oo.put("hasMore", "true"); + oo.setHasMore(true); } } @@ -658,7 +673,7 @@ public int compare(CodegenOperation one, CodegenOperation another) { } - private void generateSupportingFiles(List files, Map bundle) { + private void generateSupportingFiles(List files, TemplatingInputBundle bundle) { if (!generateSupportingFiles) { return; } @@ -809,41 +824,43 @@ protected File writeInputStreamToFile(String filename, InputStream in, String te return outputFile; } - private Map buildSupportFileBundle(List allOperations, List allModels) { + private TemplatingInputBundle buildSupportFileBundle(List allOperations, List allModels) { - Map bundle = new HashMap(); + TemplatingInputBundle bundle = new TemplatingInputBundle(); bundle.putAll(config.additionalProperties()); - bundle.put("apiPackage", config.apiPackage()); + bundle.setApiPackage(config.apiPackage()); - Map apis = new HashMap(); - apis.put("apis", allOperations); + ApiInfoBundle apis = new ApiInfoBundle(); + apis.setApis(allOperations); URL url = URLPathUtils.getServerURL(openAPI); - bundle.put("openAPI", openAPI); - bundle.put("basePath", basePath); - bundle.put("basePathWithoutHost", basePathWithoutHost); - bundle.put("scheme", URLPathUtils.getScheme(url, config)); - bundle.put("host", url.getHost()); - bundle.put("contextPath", contextPath); - bundle.put("apiInfo", apis); - bundle.put("models", allModels); - bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar)); - bundle.put("modelPackage", config.modelPackage()); - - Map securitySchemeMap = openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null; + bundle.setOpenAPI(openAPI); + bundle.setBasePath(basePath); + bundle.setBasePathWithoutHost(basePathWithoutHost); + bundle.setScheme(URLPathUtils.getScheme(url, config)); + bundle.setHost(url.getHost()); + bundle.setContextPath(contextPath); + bundle.setApiInfo(apis); + bundle.setModels(allModels); + bundle.setApiFolder(config.apiPackage().replace('.', File.separatorChar)); + bundle.setModelPackage(config.modelPackage()); + + Map securitySchemeMap = + openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null; List authMethods = config.fromSecurity(securitySchemeMap); if (authMethods != null && !authMethods.isEmpty()) { - bundle.put("authMethods", authMethods); - bundle.put("hasAuthMethods", true); + bundle.setAuthMethods(authMethods); + bundle.setHasAuthMethods(true); if (hasOAuthMethods(authMethods)) { - bundle.put("hasOAuthMethods", true); - bundle.put("oauthMethods", getOAuthMethods(authMethods)); + bundle.setHasOAuthMethods(true); + bundle.setOauthMethods(getOAuthMethods(authMethods)); } if (hasBearerMethods(authMethods)) { bundle.put("hasBearerMethods", true); + bundle.setHasBearerMethods(true); } } @@ -854,12 +871,12 @@ private Map buildSupportFileBundle(List allOperations, L } if (openAPI.getExternalDocs() != null) { - bundle.put("externalDocs", openAPI.getExternalDocs()); + bundle.setExternalDocs(openAPI.getExternalDocs()); } for (int i = 0; i < allModels.size() - 1; i++) { - HashMap cm = (HashMap) allModels.get(i); - CodegenModel m = cm.get("model"); + ModelBundle cm = allModels.get(i); + CodegenModel m = cm.getModel(); m.hasMoreModels = true; } @@ -893,14 +910,14 @@ public List generate() { List files = new ArrayList(); // models List filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI); - List allModels = new ArrayList(); + List allModels = new ArrayList(); generateModels(files, allModels, filteredSchemas); // apis - List allOperations = new ArrayList(); + List allOperations = new ArrayList<>(); generateApis(files, allOperations, allModels); // supporting files - Map bundle = buildSupportFileBundle(allOperations, allModels); + TemplatingInputBundle bundle = buildSupportFileBundle(allOperations, allModels); generateSupportingFiles(files, bundle); config.processOpenAPI(openAPI); @@ -911,7 +928,7 @@ public List generate() { } - protected File processTemplateToFile(Map templateData, String templateName, String outputFilename) throws IOException { + protected File processTemplateToFile(Bundle templateData, String templateName, String outputFilename) throws IOException { String adjustedOutputFilename = outputFilename.replaceAll("//", "/").replace('/', File.separatorChar); if (ignoreProcessor.allowsFile(new File(adjustedOutputFilename))) { String templateFile = getFullTemplateFile(config, templateName); @@ -1060,11 +1077,11 @@ private static String generateParameterId(Parameter parameter) { } - private Map processOperations(CodegenConfig config, String tag, List ops, List allModels) { - Map operations = new HashMap(); - Map objs = new HashMap(); - objs.put("classname", config.toApiName(tag)); - objs.put("pathPrefix", config.toApiVarName(tag)); + private ApiBundle processOperations(CodegenConfig config, String tag, List ops, List allModels) { + ApiBundle operations = new ApiBundle(); + OperationsBundle objs = new OperationsBundle(); + objs.setClassname(config.toApiName(tag)); + objs.setPathPrefix(config.toApiVarName(tag)); // check for operationId uniqueness Set opIds = new HashSet(); @@ -1077,10 +1094,10 @@ private Map processOperations(CodegenConfig config, String tag, } opIds.add(opId); } - objs.put("operation", ops); + objs.setOperation(ops); - operations.put("operations", objs); - operations.put("package", config.apiPackage()); + operations.setOperations(objs); + operations.setPackage(config.apiPackage()); Set allImports = new TreeSet(); for (CodegenOperation op : ops) { @@ -1106,16 +1123,18 @@ private Map processOperations(CodegenConfig config, String tag, } } - operations.put("imports", imports); + operations.setImports(imports); // add a flag to indicate whether there's any {{import}} if (imports.size() > 0) { - operations.put("hasImport", true); + operations.setHasImport(true); } - config.postProcessOperationsWithModels(operations, allModels); + List listAllModels = new ArrayList(allModels); + + config.postProcessOperationsWithModels(operations, listAllModels); if (objs.size() > 0) { - List os = (List) objs.get("operation"); + List os = objs.getOperation(); if (os != null && os.size() > 0) { CodegenOperation op = os.get(os.size() - 1); @@ -1126,26 +1145,26 @@ private Map processOperations(CodegenConfig config, String tag, } - private Map processModels(CodegenConfig config, Map definitions) { - Map objs = new HashMap(); - objs.put("package", config.modelPackage()); - List models = new ArrayList(); + private ModelsBundle processModels(CodegenConfig config, Map definitions) { + ModelsBundle objs = new ModelsBundle(); + objs.setPackage(config.modelPackage()); + List models = new ArrayList(); Set allImports = new LinkedHashSet(); for (String key : definitions.keySet()) { Schema schema = definitions.get(key); if (schema == null) throw new RuntimeException("schema cannot be null in processMoels"); CodegenModel cm = config.fromModel(key, schema); - Map mo = new HashMap(); - mo.put("model", cm); - mo.put("importPath", config.toModelImport(cm.classname)); + ModelBundle mo = new ModelBundle(); + mo.setModel(cm); + mo.setImportPath(config.toModelImport(cm.classname)); models.add(mo); cm.removeSelfReferenceImport(); allImports.addAll(cm.imports); } - objs.put("models", models); + objs.setModels(models); Set importSet = new TreeSet(); for (String nextImport : allImports) { String mapping = config.importMapping().get(nextImport); @@ -1167,7 +1186,7 @@ private Map processModels(CodegenConfig config, Map> imports = new ArrayList<>(); + private String basePath; + private String basePathWithoutHost; + private String contextPath; + private String baseName; + private String apiPackage; + private String modelPackage; + private String classname; + private String classVarName; + private String importPath; + private String classFilename; + private Map vendorExtensions = new HashMap<>(); + private boolean sortParamsByRequiredFlag; + private Collection authMethods = new ArrayList<>(); + private boolean hasMore; + private boolean hasImport; + + + // getters and setters. Each setter puts the value in the underlying Map + + public String getPackage() { + return _package; + } + + public void setPackage(String _package) { + this._package = _package; + put("package", _package); + } + + + public List> getImports() { + return imports; + } + + public void setImports(List> imports) { + this.imports = imports; + put("imports", imports); + } + + + public String getBasePath() { + return basePath; + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + put("basePath", basePath); + } + + + public String getBasePathWithoutHost() { + return basePathWithoutHost; + } + + public void setBasePathWithoutHost(String basePathWithoutHost) { + this.basePathWithoutHost = basePathWithoutHost; + put("basePathWithoutHost", basePathWithoutHost); + } + + + public String getContextPath() { + return contextPath; + } + + public void setContextPath(String contextPath) { + this.contextPath = contextPath; + put("contextPath", contextPath); + } + + + public String getBaseName() { + return baseName; + } + + public void setBaseName(String baseName) { + this.baseName = baseName; + put("baseName", baseName); + } + + + public String getApiPackage() { + return apiPackage; + } + + public void setApiPackage(String apiPackage) { + this.apiPackage = apiPackage; + put("apiPackage", apiPackage); + } + + + public String getModelPackage() { + return modelPackage; + } + + public void setModelPackage(String modelPackage) { + this.modelPackage = modelPackage; + put("modelPackage", modelPackage); + } + + + public String getClassname() { + return classname; + } + + public void setClassname(String classname) { + this.classname = classname; + put("classname", classname); + } + + + public String getClassVarName() { + return classVarName; + } + + public void setClassVarName(String classVarName) { + this.classVarName = classVarName; + put("classVarName", classVarName); + } + + + public String getImportPath() { + return importPath; + } + + public void setImportPath(String importPath) { + this.importPath = importPath; + put("importPath", importPath); + } + + + public String getClassFilename() { + return classFilename; + } + + public void setClassFilename(String classFilename) { + this.classFilename = classFilename; + put("classFilename", classFilename); + } + + + public Map getVendorExtensions() { + return vendorExtensions; + } + + public void setVendorExtensions(Map vendorExtensions) { + this.vendorExtensions = vendorExtensions; + put("vendorExtensions", vendorExtensions); + } + + + public boolean isSortParamsByRequiredFlag() { + return sortParamsByRequiredFlag; + } + + public void setSortParamsByRequiredFlag(boolean sortParamsByRequiredFlag) { + this.sortParamsByRequiredFlag = sortParamsByRequiredFlag; + put("sortParamsByRequiredFlag", sortParamsByRequiredFlag); + } + + + public OperationsBundle getOperations() { + return this.operations; + } + + public void setOperations(OperationsBundle operations) { + this.operations = operations; + put("operations", operations); + } + + + public boolean getHasMore() { + return hasMore; + } + + public void setHasMore(boolean hasMore) { + this.hasMore = hasMore; + put("hasMore", hasMore); + } + + public void setHasImport(boolean hasImport) { + this.hasImport = hasImport; + put("hasImport", hasImport); + } + + public boolean getHasImport() { + return hasImport; + } +} diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/ApiInfoBundle.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/ApiInfoBundle.java new file mode 100644 index 000000000000..b9583b5da9dd --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/ApiInfoBundle.java @@ -0,0 +1,21 @@ +package org.openapitools.codegen.templating; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class ApiInfoBundle extends BaseBundle { + + private List apis = new ArrayList<>(); + + // getters and setters. Each setter puts the value in the underlying Map + + public List getApis() { + return this.apis; + } + + public void setApis(List apis) { + this.apis = apis; + put("apis", apis); + } +} diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/BaseBundle.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/BaseBundle.java new file mode 100644 index 000000000000..5651cb17d30f --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/BaseBundle.java @@ -0,0 +1,17 @@ +package org.openapitools.codegen.templating; + +import java.util.HashMap; +import java.util.Map; + +/** + * simply extend HashMap + */ +class BaseBundle extends HashMap implements Bundle { + + BaseBundle() { + } + + public BaseBundle(Map value) { + super(value); + } +} diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/Bundle.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/Bundle.java new file mode 100644 index 000000000000..fcdd43810c33 --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/Bundle.java @@ -0,0 +1,12 @@ +package org.openapitools.codegen.templating; + +import java.util.Map; + +/** + * a Bundle is a simple {@literal Map} + * but we add strongly typed fields to avoid + * - get("name") | put("name", value) pseudo-properties + * - the back and forth casting + */ +public interface Bundle extends Map{ +} diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/CodegenOperationBundle.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/CodegenOperationBundle.java new file mode 100644 index 000000000000..641aebf5d4bd --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/CodegenOperationBundle.java @@ -0,0 +1,44 @@ +package org.openapitools.codegen.templating; + +import org.openapitools.codegen.CodegenOperation; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; + +public class CodegenOperationBundle extends BaseBundle{ + private String path; + private Collection operation = new ArrayList<>(); + private boolean hasMore; + + // getters and setters. Each setter puts the value in the underlying Map + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + put("path", path); + } + + + public Collection getOperation() { + return operation; + } + + public void setOperation(Collection operation) { + this.operation = operation; + put("operation", operation); + } + + + public boolean getHasMore() { + return hasMore; + } + + public void setHasMore(boolean hasMore) { + this.hasMore = hasMore; + put("hasMore", hasMore); + } +} diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/ModelBundle.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/ModelBundle.java new file mode 100644 index 000000000000..b7720f79198e --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/ModelBundle.java @@ -0,0 +1,45 @@ +package org.openapitools.codegen.templating; + +import org.openapitools.codegen.CodegenModel; + +import java.util.Map; + +public class ModelBundle extends BaseBundle{ + CodegenModel model; + String importPath; + + public ModelBundle(Map model) { + super(model); + if (model.containsKey("model")) { + setModel((CodegenModel) model.get("model")); + } + if (model.containsKey("importPath")) { + setImportPath((String) model.get("importPath")); + } + } + + public ModelBundle() { + super(); + } + + // getters and setters. Each setter puts the value in the underlying Map + + public CodegenModel getModel() { + return model; + } + + public void setModel(CodegenModel model) { + this.model = model; + put("model", model); + } + + + public String getImportPath() { + return importPath; + } + + public void setImportPath(String importPath) { + this.importPath = importPath; + put("importPath", importPath); + } +} diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/ModelsBundle.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/ModelsBundle.java new file mode 100644 index 000000000000..b685863c1678 --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/ModelsBundle.java @@ -0,0 +1,93 @@ +package org.openapitools.codegen.templating; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class ModelsBundle extends BaseBundle { + private String _package; + private List models = new ArrayList<>(); + private List> imports = new ArrayList<>(); + private String classname; + private String modelPackage; + + @SuppressWarnings("unchecked") + public ModelsBundle (Map value) { + super(value); + if (value.containsKey("packages")) { + setPackage((String) value.get("package")); + } + if (value.containsKey("models")) { + List> models = (List>) value.get("models"); + List modelBundles = new ArrayList<>(); + for (Map model : models) { + modelBundles.add(new ModelBundle(model)); + } + setModels(modelBundles); + } + if (value.containsKey("imports")) { + setImports((List>) value.get("imports")); + } + if (value.containsKey("classname")) { + setClassname((String) value.get("classname")); + } + if (value.containsKey("modelPackage")) { + setModelPackage((String) value.get("modelPackage")); + } + } + + public ModelsBundle() { + super(); + } + + // getters and setters. Each setter puts the value in the underlying Map + + public List getModels() { + return models; + } + + public void setModels(List models) { + this.models = models; + put("models", models); + } + + + public List> getImports() { + return imports; + } + + public void setImports(List> imports) { + this.imports = imports; + put("imports", imports); + } + + + public String getPackage() { + return _package; + } + + public void setPackage(String _package) { + this._package = _package; + put("package", _package); + } + + + public String getClassname() { + return classname; + } + + public void setClassname(String classname) { + this.classname = classname; + put("classname", classname); + } + + + public String getModelPackage() { + return modelPackage; + } + + public void setModelPackage(String modelPackage) { + this.modelPackage = modelPackage; + put("modelPackage", modelPackage); + } +} diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/OperationsBundle.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/OperationsBundle.java new file mode 100644 index 000000000000..588139df6769 --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/OperationsBundle.java @@ -0,0 +1,44 @@ +package org.openapitools.codegen.templating; + +import java.util.*; + +import org.openapitools.codegen.CodegenOperation; +import org.openapitools.codegen.CodegenSecurity; + +public class OperationsBundle extends BaseBundle{ + + private List operation = new ArrayList<>(); + private String classname; + private String pathPrefix; + + // getters and setters. Each setter puts the value in the underlying Map + + public String getPathPrefix() { + return pathPrefix; + } + + public void setPathPrefix(String pathPrefix) { + this.pathPrefix = pathPrefix; + put("pathPrefix", pathPrefix); + } + + public List getOperation() { + return this.operation; + } + + public void setOperation(List operation) { + this.operation = operation; + put("operation", operation); + } + + + public String getClassname() { + return classname; + } + + public void setClassname(String className) { + this.classname = className; + put("classname", className); + } + +} diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/TemplatingInputBundle.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/TemplatingInputBundle.java new file mode 100644 index 000000000000..c8d1a4386bef --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/TemplatingInputBundle.java @@ -0,0 +1,202 @@ +package org.openapitools.codegen.templating; + +import io.swagger.v3.oas.models.ExternalDocumentation; +import io.swagger.v3.oas.models.OpenAPI; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.openapitools.codegen.CodegenSecurity; + +public class TemplatingInputBundle extends BaseBundle { + + private String apiPackage; + private OpenAPI openAPI; + private String modelPackage; + private String basePath; + private String basePathWithoutHost; + private String scheme; + private String host; + private String contextPath; + private ApiInfoBundle apiInfo; + private List models = new ArrayList<>(); + private String apiFolder; + private List authMethods = new ArrayList<>(); + private ExternalDocumentation externalDocs; + private List> pathSet = new ArrayList<>(); + private boolean hasAuthMethods; + private boolean hasOAuthMethods; + private List oauthMethods; + private boolean hasBearerMethods; + + // getters and setters. Each setter puts the value in the underlying Map + + public String getApiPackage() { + return apiPackage; + } + + public void setApiPackage(String apiPackage) { + this.apiPackage = apiPackage; + put("apiPackage", apiPackage); + } + + + public OpenAPI getOpenAPI() { + return openAPI; + } + + public void setOpenAPI(OpenAPI openAPI) { + this.openAPI = openAPI; + put("openAPI", openAPI); + } + + + public String getModelPackage() { + return modelPackage; + } + + public void setModelPackage(String modelPackage) { + this.modelPackage = modelPackage; + put("modelPackage", modelPackage); + } + + + public String getBasePath() { + return basePath; + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + put("basePath", basePath); + } + + + public String getBasePathWithoutHost() { + return basePathWithoutHost; + } + + public void setBasePathWithoutHost(String basePathWithoutHost) { + this.basePathWithoutHost = basePathWithoutHost; + put("basePathWithoutHost", basePathWithoutHost); + } + + + public String getScheme() { + return scheme; + } + + public void setScheme(String scheme) { + this.scheme = scheme; + put("scheme", scheme); + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + put("host", host); + } + + + public String getContextPath() { + return contextPath; + } + + public void setContextPath(String contextPath) { + this.contextPath = contextPath; + put("contextPath", contextPath); + } + + + public ApiInfoBundle getApiInfo() { + return apiInfo; + } + + public void setApiInfo(ApiInfoBundle apiInfo) { + this.apiInfo = apiInfo; + put("apiInfo", apiInfo); + } + + + public List getModels() { + return models; + } + + public void setModels(List models) { + this.models = models; + put("models", models); + } + + + public String getApiFolder() { + return apiFolder; + } + + public void setApiFolder(String apiFolder) { + this.apiFolder = apiFolder; + put("apiFolder", apiFolder); + } + + + public List getAuthMethods() { + return authMethods; + } + + public void setAuthMethods(List authMethods) { + this.authMethods = authMethods; + put("authMethods", authMethods); + } + + + public ExternalDocumentation getExternalDocs() { + return externalDocs; + } + + public void setExternalDocs(ExternalDocumentation externalDocs) { + this.externalDocs = externalDocs; + put("externalDocs", externalDocs); + } + + + public boolean getHasAuthMethods() { + return hasAuthMethods; + } + + public void setHasAuthMethods(boolean hasAuthMethods) { + this.hasAuthMethods = hasAuthMethods; + put("hasAuthMethods", hasAuthMethods); + } + + + public boolean getHasOAuthMethods() { + return hasOAuthMethods; + } + + public void setHasOAuthMethods(boolean hasOAuthMethods) { + this.hasOAuthMethods = hasOAuthMethods; + put("hasOAuthMethods", hasOAuthMethods); + } + + + public List getOauthMethods() { + return oauthMethods; + } + + public void setOauthMethods(List oauthMethods) { + this.oauthMethods = oauthMethods; + put("oauthMethods", oauthMethods); + } + + + public boolean getHasBearerMethods() { + return this.hasBearerMethods; + } + + public void setHasBearerMethods(boolean hasBearerMethods) { + this.hasBearerMethods = hasBearerMethods; + put("hasBearerMethods", hasBearerMethods); + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/MockDefaultGenerator.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/MockDefaultGenerator.java index 8ee454a700d7..3f43cee19f07 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/MockDefaultGenerator.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/MockDefaultGenerator.java @@ -16,6 +16,8 @@ package org.openapitools.codegen; +import org.openapitools.codegen.templating.Bundle; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -31,7 +33,7 @@ public class MockDefaultGenerator extends DefaultGenerator { private Map files = new HashMap<>(); @Override - protected File processTemplateToFile(Map templateData, String templateName, String outputFilename) throws IOException { + protected File processTemplateToFile(Bundle templateData, String templateName, String outputFilename) throws IOException { templateBasedFiles.add(new WrittenTemplateBasedFile(templateData, templateName, normalizePath(outputFilename))); return super.processTemplateToFile(templateData, templateName, outputFilename); } @@ -61,17 +63,17 @@ public Map getFiles() { } public static class WrittenTemplateBasedFile { - private Map templateData; + private Bundle templateData; private String templateName; private String outputFilename; - public WrittenTemplateBasedFile(Map templateData, String templateName, String outputFilename) { + public WrittenTemplateBasedFile(Bundle templateData, String templateName, String outputFilename) { this.templateData = templateData; this.templateName = templateName; this.outputFilename = outputFilename; } - public Map getTemplateData() { + public Bundle getTemplateData() { return templateData; }