mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-04 22:50:53 +00:00
Merge pull request #2866 from jimschubert/cs/ignore_model_tests_option
[generator] Individual options to exclude tests and docs for apis and models
This commit is contained in:
commit
5af4156c02
20
README.md
20
README.md
@ -386,6 +386,26 @@ To control the specific files being generated, you can pass a CSV list of what y
|
|||||||
-Dmodels=User -DsupportingFiles=StringUtil.java
|
-Dmodels=User -DsupportingFiles=StringUtil.java
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To control generation of docs and tests for api and models, pass false to the option. For api, these options are `-DapiTest=false` and `-DapiDocs=false`. For models, `-DmodelTest=false` and `-DmodelDocs=false`.
|
||||||
|
These options default to true and don't limit the generation of the feature options listed above (like `-Dapi`):
|
||||||
|
|
||||||
|
```
|
||||||
|
# generate only models (with tests and documentation)
|
||||||
|
java -Dmodels {opts}
|
||||||
|
|
||||||
|
# generate only models (with tests but no documentation)
|
||||||
|
java -Dmodels -DmodelDocs=false {opts}
|
||||||
|
|
||||||
|
# generate only User and Pet models (no tests and no documentation)
|
||||||
|
java -Dmodels=User,Pet -DmodelTests=false {opts}
|
||||||
|
|
||||||
|
# generate only apis (without tests)
|
||||||
|
java -Dapis -DapiTests=false {opts}
|
||||||
|
|
||||||
|
# generate only apis (modelTests option is ignored)
|
||||||
|
java -Dapis -DmodelTests=false {opts}
|
||||||
|
```
|
||||||
|
|
||||||
When using selective generation, _only_ the templates needed for the specific generation will be used.
|
When using selective generation, _only_ the templates needed for the specific generation will be used.
|
||||||
|
|
||||||
### Customizing the generator
|
### Customizing the generator
|
||||||
|
@ -105,4 +105,14 @@ public class CodegenConstants {
|
|||||||
|
|
||||||
public static final String SUPPORTS_ES6 = "supportsES6";
|
public static final String SUPPORTS_ES6 = "supportsES6";
|
||||||
public static final String SUPPORTS_ES6_DESC = "Generate code that conforms to ES6.";
|
public static final String SUPPORTS_ES6_DESC = "Generate code that conforms to ES6.";
|
||||||
|
|
||||||
|
public static final String EXCLUDE_TESTS = "excludeTests";
|
||||||
|
public static final String EXCLUDE_TESTS_DESC = "Specifies that no tests are to be generated.";
|
||||||
|
|
||||||
|
public static final String GENERATE_API_TESTS = "generateApiTests";
|
||||||
|
public static final String GENERATE_API_TESTS_DESC = "Specifies that api tests are to be generated.";
|
||||||
|
|
||||||
|
public static final String GENERATE_MODEL_TESTS = "generateModelTests";
|
||||||
|
public static final String GENERATE_MODEL_TESTS_DESC = "Specifies that model tests are to be generated.";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,10 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
Boolean generateApis = null;
|
Boolean generateApis = null;
|
||||||
Boolean generateModels = null;
|
Boolean generateModels = null;
|
||||||
Boolean generateSupportingFiles = null;
|
Boolean generateSupportingFiles = null;
|
||||||
|
Boolean generateApiTests = null;
|
||||||
|
Boolean generateApiDocumentation = null;
|
||||||
|
Boolean generateModelTests = null;
|
||||||
|
Boolean generateModelDocumentation = null;
|
||||||
|
|
||||||
Set<String> modelsToGenerate = null;
|
Set<String> modelsToGenerate = null;
|
||||||
Set<String> apisToGenerate = null;
|
Set<String> apisToGenerate = null;
|
||||||
@ -68,6 +72,18 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
supportingFilesToGenerate = new HashSet<String>(Arrays.asList(supportingFiles.split(",")));
|
supportingFilesToGenerate = new HashSet<String>(Arrays.asList(supportingFiles.split(",")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(System.getProperty("modelTests") != null) {
|
||||||
|
generateModelTests = Boolean.valueOf(System.getProperty("modelTests"));
|
||||||
|
}
|
||||||
|
if(System.getProperty("modelDocs") != null) {
|
||||||
|
generateModelDocumentation = Boolean.valueOf(System.getProperty("modelDocs"));
|
||||||
|
}
|
||||||
|
if(System.getProperty("apiTests") != null) {
|
||||||
|
generateApiTests = Boolean.valueOf(System.getProperty("apiTests"));
|
||||||
|
}
|
||||||
|
if(System.getProperty("apiDocs") != null) {
|
||||||
|
generateApiDocumentation = Boolean.valueOf(System.getProperty("apiDocs"));
|
||||||
|
}
|
||||||
|
|
||||||
if(generateApis == null && generateModels == null && generateSupportingFiles == null) {
|
if(generateApis == null && generateModels == null && generateSupportingFiles == null) {
|
||||||
// no specifics are set, generate everything
|
// no specifics are set, generate everything
|
||||||
@ -85,6 +101,28 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// model/api tests and documentation options rely on parent generate options (api or model) and no other options.
|
||||||
|
// They default to true in all scenarios and can only be marked false explicitly
|
||||||
|
if (generateModelTests == null) {
|
||||||
|
generateModelTests = true;
|
||||||
|
}
|
||||||
|
if (generateModelDocumentation == null) {
|
||||||
|
generateModelDocumentation = true;
|
||||||
|
}
|
||||||
|
if (generateApiTests == null) {
|
||||||
|
generateApiTests = true;
|
||||||
|
}
|
||||||
|
if (generateApiDocumentation == null) {
|
||||||
|
generateApiDocumentation = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Additional properties added for tests to exclude references in project related files
|
||||||
|
config.additionalProperties().put(CodegenConstants.GENERATE_API_TESTS, generateApiTests);
|
||||||
|
config.additionalProperties().put(CodegenConstants.GENERATE_MODEL_TESTS, generateModelTests);
|
||||||
|
if(Boolean.FALSE.equals(generateApiTests) && Boolean.FALSE.equals(generateModelTests)) {
|
||||||
|
config.additionalProperties().put(CodegenConstants.EXCLUDE_TESTS, Boolean.TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
if (swagger == null || config == null) {
|
if (swagger == null || config == null) {
|
||||||
throw new RuntimeException("missing swagger input or config!");
|
throw new RuntimeException("missing swagger input or config!");
|
||||||
}
|
}
|
||||||
@ -282,51 +320,55 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
files.add(new File(filename));
|
files.add(new File(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
// to generate model test files
|
if(generateModelTests) {
|
||||||
for (String templateName : config.modelTestTemplateFiles().keySet()) {
|
// to generate model test files
|
||||||
String suffix = config.modelTestTemplateFiles().get(templateName);
|
for (String templateName : config.modelTestTemplateFiles().keySet()) {
|
||||||
String filename = config.modelTestFileFolder() + File.separator + config.toModelTestFilename(name) + suffix;
|
String suffix = config.modelTestTemplateFiles().get(templateName);
|
||||||
// do not overwrite test file that already exists
|
String filename = config.modelTestFileFolder() + File.separator + config.toModelTestFilename(name) + suffix;
|
||||||
if (new File(filename).exists()) {
|
// do not overwrite test file that already exists
|
||||||
LOGGER.info("File exists. Skipped overwriting " + filename);
|
if (new File(filename).exists()) {
|
||||||
continue;
|
LOGGER.info("File exists. Skipped overwriting " + 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));
|
||||||
}
|
}
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// to generate model documentation files
|
if(generateModelDocumentation) {
|
||||||
for (String templateName : config.modelDocTemplateFiles().keySet()) {
|
// to generate model documentation files
|
||||||
String suffix = config.modelDocTemplateFiles().get(templateName);
|
for (String templateName : config.modelDocTemplateFiles().keySet()) {
|
||||||
String filename = config.modelDocFileFolder() + File.separator + config.toModelDocFilename(name) + suffix;
|
String suffix = config.modelDocTemplateFiles().get(templateName);
|
||||||
if (!config.shouldOverwrite(filename)) {
|
String filename = config.modelDocFileFolder() + File.separator + config.toModelDocFilename(name) + suffix;
|
||||||
LOGGER.info("Skipped overwriting " + filename);
|
if (!config.shouldOverwrite(filename)) {
|
||||||
continue;
|
LOGGER.info("Skipped overwriting " + 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));
|
||||||
}
|
}
|
||||||
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);
|
||||||
@ -417,52 +459,56 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
files.add(new File(filename));
|
files.add(new File(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
// to generate api test files
|
if(generateApiTests) {
|
||||||
for (String templateName : config.apiTestTemplateFiles().keySet()) {
|
// to generate api test files
|
||||||
String filename = config.apiTestFilename(templateName, tag);
|
for (String templateName : config.apiTestTemplateFiles().keySet()) {
|
||||||
// do not overwrite test file that already exists
|
String filename = config.apiTestFilename(templateName, tag);
|
||||||
if (new File(filename).exists()) {
|
// do not overwrite test file that already exists
|
||||||
LOGGER.info("File exists. Skipped overwriting " + filename);
|
if (new File(filename).exists()) {
|
||||||
continue;
|
LOGGER.info("File exists. Skipped overwriting " + filename);
|
||||||
}
|
continue;
|
||||||
String templateFile = getFullTemplateFile(config, templateName);
|
}
|
||||||
String template = readTemplate(templateFile);
|
String templateFile = getFullTemplateFile(config, templateName);
|
||||||
Template tmpl = Mustache.compiler()
|
String template = readTemplate(templateFile);
|
||||||
.withLoader(new Mustache.TemplateLoader() {
|
Template tmpl = Mustache.compiler()
|
||||||
@Override
|
.withLoader(new Mustache.TemplateLoader() {
|
||||||
public Reader getTemplate(String name) {
|
@Override
|
||||||
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
|
public Reader getTemplate(String name) {
|
||||||
}
|
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
|
||||||
})
|
}
|
||||||
.defaultValue("")
|
})
|
||||||
.compile(template);
|
.defaultValue("")
|
||||||
|
.compile(template);
|
||||||
|
|
||||||
writeToFile(filename, tmpl.execute(operation));
|
writeToFile(filename, tmpl.execute(operation));
|
||||||
files.add(new File(filename));
|
files.add(new File(filename));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// to generate api documentation files
|
if(generateApiDocumentation) {
|
||||||
for (String templateName : config.apiDocTemplateFiles().keySet()) {
|
// to generate api documentation files
|
||||||
String filename = config.apiDocFilename(templateName, tag);
|
for (String templateName : config.apiDocTemplateFiles().keySet()) {
|
||||||
if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
|
String filename = config.apiDocFilename(templateName, tag);
|
||||||
LOGGER.info("Skipped overwriting " + filename);
|
if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
|
||||||
continue;
|
LOGGER.info("Skipped overwriting " + 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(operation));
|
||||||
|
files.add(new File(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
@ -137,6 +137,11 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
|||||||
@Override
|
@Override
|
||||||
public void processOpts() {
|
public void processOpts() {
|
||||||
super.processOpts();
|
super.processOpts();
|
||||||
|
Boolean excludeTests = false;
|
||||||
|
|
||||||
|
if(additionalProperties.containsKey(CodegenConstants.EXCLUDE_TESTS)) {
|
||||||
|
excludeTests = Boolean.valueOf(additionalProperties.get(CodegenConstants.EXCLUDE_TESTS).toString());
|
||||||
|
}
|
||||||
|
|
||||||
apiPackage = "Api";
|
apiPackage = "Api";
|
||||||
modelPackage = "Model";
|
modelPackage = "Model";
|
||||||
@ -234,7 +239,10 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
|||||||
|
|
||||||
// copy package.config to nuget's standard location for project-level installs
|
// copy package.config to nuget's standard location for project-level installs
|
||||||
supportingFiles.add(new SupportingFile("packages.config.mustache", packageFolder + File.separator, "packages.config"));
|
supportingFiles.add(new SupportingFile("packages.config.mustache", packageFolder + File.separator, "packages.config"));
|
||||||
supportingFiles.add(new SupportingFile("packages_test.config.mustache", testPackageFolder + File.separator, "packages.config"));
|
|
||||||
|
if(Boolean.FALSE.equals(excludeTests)) {
|
||||||
|
supportingFiles.add(new SupportingFile("packages_test.config.mustache", testPackageFolder + File.separator, "packages.config"));
|
||||||
|
}
|
||||||
|
|
||||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||||
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
||||||
@ -247,11 +255,9 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
|||||||
supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln"));
|
supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln"));
|
||||||
supportingFiles.add(new SupportingFile("Project.mustache", packageFolder, packageName + ".csproj"));
|
supportingFiles.add(new SupportingFile("Project.mustache", packageFolder, packageName + ".csproj"));
|
||||||
|
|
||||||
// TODO: Check if test project output is enabled, partially related to #2506. Should have options for:
|
if(Boolean.FALSE.equals(excludeTests)) {
|
||||||
// 1) No test project
|
supportingFiles.add(new SupportingFile("TestProject.mustache", testPackageFolder, testPackageName + ".csproj"));
|
||||||
// 2) No model tests
|
}
|
||||||
// 3) No api tests
|
|
||||||
supportingFiles.add(new SupportingFile("TestProject.mustache", testPackageFolder, testPackageName + ".csproj"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
additionalProperties.put("apiDocPath", apiDocPath);
|
additionalProperties.put("apiDocPath", apiDocPath);
|
||||||
|
@ -116,6 +116,11 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
@Override
|
@Override
|
||||||
public void processOpts() {
|
public void processOpts() {
|
||||||
super.processOpts();
|
super.processOpts();
|
||||||
|
Boolean excludeTests = false;
|
||||||
|
|
||||||
|
if(additionalProperties.containsKey(CodegenConstants.EXCLUDE_TESTS)) {
|
||||||
|
excludeTests = Boolean.valueOf(additionalProperties.get(CodegenConstants.EXCLUDE_TESTS).toString());
|
||||||
|
}
|
||||||
|
|
||||||
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
|
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
|
||||||
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
|
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
|
||||||
@ -151,7 +156,10 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
supportingFiles.add(new SupportingFile("__init__package.mustache", swaggerFolder, "__init__.py"));
|
supportingFiles.add(new SupportingFile("__init__package.mustache", swaggerFolder, "__init__.py"));
|
||||||
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py"));
|
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py"));
|
||||||
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py"));
|
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py"));
|
||||||
supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py"));
|
|
||||||
|
if(Boolean.FALSE.equals(excludeTests)) {
|
||||||
|
supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py"));
|
||||||
|
}
|
||||||
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
||||||
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
|
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,9 @@ VisualStudioVersion = 12.0.0.0
|
|||||||
MinimumVisualStudioVersion = 10.0.0.1
|
MinimumVisualStudioVersion = 10.0.0.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{packageName}}", "src\{{packageName}}\{{packageName}}.csproj", "{{packageGuid}}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{packageName}}", "src\{{packageName}}\{{packageName}}.csproj", "{{packageGuid}}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{testPackageName}}", "src\{{testPackageName}}\{{testPackageName}}.csproj", "{19F1DEBC-DE5E-4517-8062-F000CD499087}"
|
{{^excludeTests}}Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{testPackageName}}", "src\{{testPackageName}}\{{testPackageName}}.csproj", "{19F1DEBC-DE5E-4517-8062-F000CD499087}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
{{/excludeTests}}Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
Release|Any CPU = Release|Any CPU
|
Release|Any CPU = Release|Any CPU
|
||||||
|
Loading…
x
Reference in New Issue
Block a user