forked from loafle/openapi-generator-original
add api test and model test files
This commit is contained in:
parent
ccd0db4434
commit
fb1cc254e4
@ -19,10 +19,14 @@ public interface CodegenConfig {
|
|||||||
|
|
||||||
Map<String, Object> additionalProperties();
|
Map<String, Object> additionalProperties();
|
||||||
|
|
||||||
|
String testPackage();
|
||||||
|
|
||||||
String apiPackage();
|
String apiPackage();
|
||||||
|
|
||||||
String apiFileFolder();
|
String apiFileFolder();
|
||||||
|
|
||||||
|
String apiTestFileFolder();
|
||||||
|
|
||||||
String fileSuffix();
|
String fileSuffix();
|
||||||
|
|
||||||
String outputFolder();
|
String outputFolder();
|
||||||
@ -33,6 +37,8 @@ public interface CodegenConfig {
|
|||||||
|
|
||||||
String modelFileFolder();
|
String modelFileFolder();
|
||||||
|
|
||||||
|
String modelTestFileFolder();
|
||||||
|
|
||||||
String modelPackage();
|
String modelPackage();
|
||||||
|
|
||||||
String toApiName(String name);
|
String toApiName(String name);
|
||||||
@ -87,6 +93,10 @@ public interface CodegenConfig {
|
|||||||
|
|
||||||
Map<String, String> modelTemplateFiles();
|
Map<String, String> modelTemplateFiles();
|
||||||
|
|
||||||
|
Map<String, String> apiTestTemplateFiles();
|
||||||
|
|
||||||
|
Map<String, String> modelTestTemplateFiles();
|
||||||
|
|
||||||
Set<String> languageSpecificPrimitives();
|
Set<String> languageSpecificPrimitives();
|
||||||
|
|
||||||
void preprocessSwagger(Swagger swagger);
|
void preprocessSwagger(Swagger swagger);
|
||||||
@ -97,6 +107,10 @@ public interface CodegenConfig {
|
|||||||
|
|
||||||
String toModelFilename(String name);
|
String toModelFilename(String name);
|
||||||
|
|
||||||
|
String toApiTestFilename(String name);
|
||||||
|
|
||||||
|
String toModelTestFilename(String name);
|
||||||
|
|
||||||
String toModelImport(String name);
|
String toModelImport(String name);
|
||||||
|
|
||||||
String toApiImport(String name);
|
String toApiImport(String name);
|
||||||
@ -115,6 +129,8 @@ public interface CodegenConfig {
|
|||||||
|
|
||||||
String apiFilename(String templateName, String tag);
|
String apiFilename(String templateName, String tag);
|
||||||
|
|
||||||
|
String apiTestFilename(String templateName, String tag);
|
||||||
|
|
||||||
boolean shouldOverwrite(String filename);
|
boolean shouldOverwrite(String filename);
|
||||||
|
|
||||||
boolean isSkipOverwrite();
|
boolean isSkipOverwrite();
|
||||||
|
@ -63,8 +63,11 @@ public class DefaultCodegen {
|
|||||||
protected Set<String> languageSpecificPrimitives = new HashSet<String>();
|
protected Set<String> languageSpecificPrimitives = new HashSet<String>();
|
||||||
protected Map<String, String> importMapping = new HashMap<String, String>();
|
protected Map<String, String> importMapping = new HashMap<String, String>();
|
||||||
protected String modelPackage = "", apiPackage = "", fileSuffix;
|
protected String modelPackage = "", apiPackage = "", fileSuffix;
|
||||||
|
protected String testPackage = "";
|
||||||
protected Map<String, String> apiTemplateFiles = new HashMap<String, String>();
|
protected Map<String, String> apiTemplateFiles = new HashMap<String, String>();
|
||||||
protected Map<String, String> modelTemplateFiles = new HashMap<String, String>();
|
protected Map<String, String> modelTemplateFiles = new HashMap<String, String>();
|
||||||
|
protected Map<String, String> apiTestTemplateFiles = new HashMap<String, String>();
|
||||||
|
protected Map<String, String> modelTestTemplateFiles = new HashMap<String, String>();
|
||||||
protected String templateDir;
|
protected String templateDir;
|
||||||
protected String embeddedTemplateDir;
|
protected String embeddedTemplateDir;
|
||||||
protected Map<String, Object> additionalProperties = new HashMap<String, Object>();
|
protected Map<String, Object> additionalProperties = new HashMap<String, Object>();
|
||||||
@ -180,6 +183,10 @@ public class DefaultCodegen {
|
|||||||
return importMapping;
|
return importMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String testPackage() {
|
||||||
|
return testPackage;
|
||||||
|
}
|
||||||
|
|
||||||
public String modelPackage() {
|
public String modelPackage() {
|
||||||
return modelPackage;
|
return modelPackage;
|
||||||
}
|
}
|
||||||
@ -204,6 +211,14 @@ public class DefaultCodegen {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<String, String> apiTestTemplateFiles() {
|
||||||
|
return apiTestTemplateFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> modelTestTemplateFiles() {
|
||||||
|
return modelTestTemplateFiles;
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, String> apiTemplateFiles() {
|
public Map<String, String> apiTemplateFiles() {
|
||||||
return apiTemplateFiles;
|
return apiTemplateFiles;
|
||||||
}
|
}
|
||||||
@ -220,6 +235,14 @@ public class DefaultCodegen {
|
|||||||
return outputFolder + "/" + modelPackage().replace('.', '/');
|
return outputFolder + "/" + modelPackage().replace('.', '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String apiTestFileFolder() {
|
||||||
|
return outputFolder + "/" + testPackage().replace('.', '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
public String modelTestFileFolder() {
|
||||||
|
return outputFolder + "/" + testPackage().replace('.', '/');
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, Object> additionalProperties() {
|
public Map<String, Object> additionalProperties() {
|
||||||
return additionalProperties;
|
return additionalProperties;
|
||||||
}
|
}
|
||||||
@ -270,6 +293,16 @@ public class DefaultCodegen {
|
|||||||
return toApiName(name);
|
return toApiName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the file name of the Api
|
||||||
|
*
|
||||||
|
* @param name the file name of the Api
|
||||||
|
* @return the file name of the Api
|
||||||
|
*/
|
||||||
|
public String toApiTestFilename(String name) {
|
||||||
|
return toApiName(name) + "Test";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the variable name in the Api
|
* Return the variable name in the Api
|
||||||
*
|
*
|
||||||
@ -290,6 +323,16 @@ public class DefaultCodegen {
|
|||||||
return initialCaps(name);
|
return initialCaps(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the capitalized file name of the model
|
||||||
|
*
|
||||||
|
* @param name the model name
|
||||||
|
* @return the file name of the model
|
||||||
|
*/
|
||||||
|
public String toModelTestFilename(String name) {
|
||||||
|
return initialCaps(name) + "Test";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the operation ID (method name)
|
* Return the operation ID (method name)
|
||||||
*
|
*
|
||||||
@ -2050,6 +2093,11 @@ public class DefaultCodegen {
|
|||||||
return apiFileFolder() + '/' + toApiFilename(tag) + suffix;
|
return apiFileFolder() + '/' + toApiFilename(tag) + suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String apiTestFilename(String templateName, String tag) {
|
||||||
|
String suffix = apiTestTemplateFiles().get(templateName);
|
||||||
|
return apiTestFileFolder() + '/' + toApiTestFilename(tag) + suffix;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean shouldOverwrite(String filename) {
|
public boolean shouldOverwrite(String filename) {
|
||||||
return !(skipOverwrite && new File(filename).exists());
|
return !(skipOverwrite && new File(filename).exists());
|
||||||
}
|
}
|
||||||
|
@ -211,6 +211,28 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
writeToFile(filename, tmpl.execute(models));
|
writeToFile(filename, tmpl.execute(models));
|
||||||
files.add(new File(filename));
|
files.add(new File(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// to generate model test files
|
||||||
|
for (String templateName : config.modelTestTemplateFiles().keySet()) {
|
||||||
|
String suffix = config.modelTestTemplateFiles().get(templateName);
|
||||||
|
String filename = config.modelTestFileFolder() + File.separator + config.toModelTestFilename(name) + suffix;
|
||||||
|
if (!config.shouldOverwrite(filename)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String templateFile = getFullTemplateFile(config, templateName);
|
||||||
|
String template = readTemplate(templateFile);
|
||||||
|
Template tmpl = Mustache.compiler()
|
||||||
|
.withLoader(new Mustache.TemplateLoader() {
|
||||||
|
@Override
|
||||||
|
public Reader getTemplate(String name) {
|
||||||
|
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.defaultValue("")
|
||||||
|
.compile(template);
|
||||||
|
writeToFile(filename, tmpl.execute(models));
|
||||||
|
files.add(new File(filename));
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Could not generate model '" + name + "'", e);
|
throw new RuntimeException("Could not generate model '" + name + "'", e);
|
||||||
}
|
}
|
||||||
@ -288,6 +310,29 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
writeToFile(filename, tmpl.execute(operation));
|
writeToFile(filename, tmpl.execute(operation));
|
||||||
files.add(new File(filename));
|
files.add(new File(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (String templateName : config.apiTestTemplateFiles().keySet()) {
|
||||||
|
String filename = config.apiTestFilename(templateName, tag);
|
||||||
|
if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String templateFile = getFullTemplateFile(config, templateName);
|
||||||
|
String template = readTemplate(templateFile);
|
||||||
|
Template tmpl = Mustache.compiler()
|
||||||
|
.withLoader(new Mustache.TemplateLoader() {
|
||||||
|
@Override
|
||||||
|
public Reader getTemplate(String name) {
|
||||||
|
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.defaultValue("")
|
||||||
|
.compile(template);
|
||||||
|
|
||||||
|
writeToFile(filename, tmpl.execute(operation));
|
||||||
|
files.add(new File(filename));
|
||||||
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
|
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
|
||||||
}
|
}
|
||||||
|
@ -42,9 +42,12 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
outputFolder = "generated-code" + File.separator + "php";
|
outputFolder = "generated-code" + File.separator + "php";
|
||||||
modelTemplateFiles.put("model.mustache", ".php");
|
modelTemplateFiles.put("model.mustache", ".php");
|
||||||
apiTemplateFiles.put("api.mustache", ".php");
|
apiTemplateFiles.put("api.mustache", ".php");
|
||||||
|
modelTestTemplateFiles.put("model_test.mustache", ".php");
|
||||||
|
apiTestTemplateFiles.put("api_test.mustache", ".php");
|
||||||
embeddedTemplateDir = templateDir = "php";
|
embeddedTemplateDir = templateDir = "php";
|
||||||
apiPackage = invokerPackage + "\\Api";
|
apiPackage = invokerPackage + "\\Api";
|
||||||
modelPackage = invokerPackage + "\\Model";
|
modelPackage = invokerPackage + "\\Model";
|
||||||
|
testPackage = invokerPackage + "\\Tests";
|
||||||
|
|
||||||
reservedWords = new HashSet<String>(
|
reservedWords = new HashSet<String>(
|
||||||
Arrays.asList(
|
Arrays.asList(
|
||||||
@ -237,6 +240,16 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
return (outputFolder + "/" + toPackagePath(modelPackage, srcBasePath));
|
return (outputFolder + "/" + toPackagePath(modelPackage, srcBasePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String apiTestFileFolder() {
|
||||||
|
return (outputFolder + "/" + toPackagePath(testPackage, srcBasePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String modelTestFileFolder() {
|
||||||
|
return (outputFolder + "/" + toPackagePath(testPackage, srcBasePath));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getTypeDeclaration(Property p) {
|
public String getTypeDeclaration(Property p) {
|
||||||
if (p instanceof ArrayProperty) {
|
if (p instanceof ArrayProperty) {
|
||||||
@ -365,6 +378,12 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
return toModelName(name);
|
return toModelName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toModelTestFilename(String name) {
|
||||||
|
// should be the same as the model name
|
||||||
|
return toModelName(name) + "Test";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toOperationId(String operationId) {
|
public String toOperationId(String operationId) {
|
||||||
// throw exception if method name is empty
|
// throw exception if method name is empty
|
||||||
|
Loading…
x
Reference in New Issue
Block a user