Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import io.swagger.models.Swagger;
import io.swagger.models.auth.AuthorizationValue;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class ClientOptInput {
private CodegenConfig config;
private ClientOpts opts;
private Swagger swagger;
private List<AuthorizationValue> auths;
private Map<String,String> systemProperties;

public ClientOptInput swagger(Swagger swagger) {
this.setSwagger(swagger);
Expand All @@ -28,6 +31,11 @@ public ClientOptInput config(CodegenConfig codegenConfig) {
return this;
}

public ClientOptInput systemProperties(Map<String,String> properties) {
this.setSystemProperties(properties);
return this;
}

@Deprecated
public ClientOptInput auth(String urlEncodedAuthString) {
this.setAuth(urlEncodedAuthString);
Expand Down Expand Up @@ -65,6 +73,18 @@ public void setOpts(ClientOpts opts) {
this.opts = opts;
}

public Map<String, String> getSystemProperties() {
if(systemProperties == null) {
return Collections.emptyMap();
} else {
return systemProperties;
}
}

public void setSystemProperties(Map<String, String> systemProperties) {
this.systemProperties = systemProperties;
}

@ApiModelProperty(dataType = "Object")
public Swagger getSwagger() {
return swagger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
protected ClientOptInput opts;
protected Swagger swagger;
protected CodegenIgnoreProcessor ignoreProcessor;
private Boolean generateApis = null;
private Boolean generateModels = null;
private Boolean generateSupportingFiles = null;
private boolean generateApis;
private boolean generateModels;
private boolean generateSupportingFiles;
private Boolean generateApiTests = null;
private Boolean generateApiDocumentation = null;
private Boolean generateModelTests = null;
Expand Down Expand Up @@ -86,33 +86,56 @@ private String getHost(){
return hostBuilder.toString();
}

/**
* Checks whether a system property is set.
* "System property" here means a value in the systemProperty map of {@code opts}.
*
* @param name the name of the property.
* @return {@code true} if the property is set, otherwise false.
*/
private boolean isSystemPropertySet(String name) {
return opts.getSystemProperties().get(name) != null;
}

/**
* Returns the value of the "system property" as a boolean, with default {@code true}.
* "System property" here means a value in the systemProperty map of {@code opts}.
*
* @param name The name of the system property.
* @return {@code true} if the property is not set, or if the value is (case-insensitive) "true", otherwise {@code false}.
*/
private boolean getSystemPropertyAsBoolean(String name) {
String value = opts.getSystemProperties().get(name);
return value == null || Boolean.valueOf(value);
}

private Set<String> getSystemPropertyAsStringSet(String name) {
String value = opts.getSystemProperties().get(name);
if (value != null && !value.isEmpty()) {
return new HashSet<String>(Arrays.asList(value.split(",")));
} else {
return null;
}
}

private void configureGeneratorProperties() {

// allows generating only models by specifying a CSV of models to generate, or empty for all
generateApis = System.getProperty("apis") != null ? true:null;
generateModels = System.getProperty("models") != null ? true: null;
generateSupportingFiles = System.getProperty("supportingFiles") != null ? true:null;

if (generateApis == null && generateModels == null && generateSupportingFiles == null) {
generateApis = isSystemPropertySet("apis");
generateModels = isSystemPropertySet("models");
generateSupportingFiles = isSystemPropertySet("supportingFiles");

if (!generateApis && !generateModels && !generateSupportingFiles) {
// no specifics are set, generate everything
generateApis = generateModels = generateSupportingFiles = true;
} else {
if(generateApis == null) {
generateApis = false;
}
if(generateModels == null) {
generateModels = false;
}
if(generateSupportingFiles == null) {
generateSupportingFiles = false;
}
}
// 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
generateModelTests = System.getProperty("modelTests") != null ? Boolean.valueOf(System.getProperty("modelTests")): true;
generateModelDocumentation = System.getProperty("modelDocs") != null ? Boolean.valueOf(System.getProperty("modelDocs")):true;
generateApiTests = System.getProperty("apiTests") != null ? Boolean.valueOf(System.getProperty("apiTests")): true;
generateApiDocumentation = System.getProperty("apiDocs") != null ? Boolean.valueOf(System.getProperty("apiDocs")):true;
generateModelTests = getSystemPropertyAsBoolean("modelTests");
generateModelDocumentation = getSystemPropertyAsBoolean("modelDocs");
generateApiTests = getSystemPropertyAsBoolean("apiTests");
generateApiDocumentation = getSystemPropertyAsBoolean("apiDocs");


// Additional properties added for tests to exclude references in project related files
Expand All @@ -121,7 +144,7 @@ private void configureGeneratorProperties() {
if(!generateApiTests && !generateModelTests) {
config.additionalProperties().put(CodegenConstants.EXCLUDE_TESTS, true);
}
if (System.getProperty("debugSwagger") != null) {
if (isSystemPropertySet("debugSwagger")) {
Json.prettyPrint(swagger);
}
config.processOpts();
Expand Down Expand Up @@ -227,11 +250,7 @@ private void generateModels(List<File> files, List<Object> allModels) {
return;
}

String modelNames = System.getProperty("models");
Set<String> modelsToGenerate = null;
if(modelNames != null && !modelNames.isEmpty()) {
modelsToGenerate = new HashSet<String>(Arrays.asList(modelNames.split(",")));
}
Set<String> modelsToGenerate = getSystemPropertyAsStringSet("models");

Set<String> modelKeys = definitions.keySet();
if(modelsToGenerate != null && !modelsToGenerate.isEmpty()) {
Expand Down Expand Up @@ -342,7 +361,7 @@ private Model getParent(Model model) {
throw new RuntimeException("Could not generate model '" + modelName + "'", e);
}
}
if (System.getProperty("debugModels") != null) {
if (isSystemPropertySet("debugModels")) {
LOGGER.info("############ Model info ############");
Json.prettyPrint(allModels);
}
Expand All @@ -354,11 +373,8 @@ private void generateApis(List<File> files, List<Object> allOperations) {
return;
}
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
Set<String> apisToGenerate = null;
String apiNames = System.getProperty("apis");
if(apiNames != null && !apiNames.isEmpty()) {
apisToGenerate = new HashSet<String>(Arrays.asList(apiNames.split(",")));
}
Set<String> apisToGenerate = getSystemPropertyAsStringSet("apis");

if(apisToGenerate != null && !apisToGenerate.isEmpty()) {
Map<String, List<CodegenOperation>> updatedPaths = new TreeMap<String, List<CodegenOperation>>();
for(String m : paths.keySet()) {
Expand Down Expand Up @@ -463,7 +479,7 @@ public int compare(CodegenOperation one, CodegenOperation another) {
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
}
}
if (System.getProperty("debugOperations") != null) {
if (isSystemPropertySet("debugOperations")) {
LOGGER.info("############ Operation info ############");
Json.prettyPrint(allOperations);
}
Expand All @@ -474,11 +490,7 @@ private void generateSupportingFiles(List<File> files, Map<String, Object> bundl
if (!generateSupportingFiles) {
return;
}
Set<String> supportingFilesToGenerate = null;
String supportingFiles = System.getProperty("supportingFiles");
if(supportingFiles!= null && !supportingFiles.isEmpty()) {
supportingFilesToGenerate = new HashSet<String>(Arrays.asList(supportingFiles.split(",")));
}
Set<String> supportingFilesToGenerate = getSystemPropertyAsStringSet("supportingFiles");

for (SupportingFile support : config.supportingFiles()) {
try {
Expand Down Expand Up @@ -637,7 +649,7 @@ public int compare(CodegenSecurity one, CodegenSecurity another) {

config.postProcessSupportingFileData(bundle);

if (System.getProperty("debugSupportingFiles") != null) {
if(isSystemPropertySet("debugSupportingFiles")) {
LOGGER.info("############ Supporting file info ############");
Json.prettyPrint(bundle);
}
Expand Down Expand Up @@ -739,7 +751,7 @@ private void processOperation(String resourcePath, String httpMethod, Operation
if (operation == null) {
return;
}
if (System.getProperty("debugOperations") != null) {
if (isSystemPropertySet("debugOperations")) {
LOGGER.info("processOperation: resourcePath= " + resourcePath + "\t;" + httpMethod + " " + operation + "\n");
}
List<String> tags = operation.getTags();
Expand All @@ -751,7 +763,7 @@ private void processOperation(String resourcePath, String httpMethod, Operation
build up a set of parameter "ids" defined at the operation level
per the swagger 2.0 spec "A unique parameter is defined by a combination of a name and location"
i'm assuming "location" == "in"
*/
*/
Set<String> operationParameters = new HashSet<String>();
if (operation.getParameters() != null) {
for (Parameter parameter : operation.getParameters()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,14 @@ public ClientOptInput toClientOptInput() {

config.additionalProperties().putAll(additionalProperties);

ClientOptInput input = new ClientOptInput()
.config(config);

final List<AuthorizationValue> authorizationValues = AuthParser.parse(auth);

Swagger swagger = new SwaggerParser().read(inputSpec, authorizationValues, true);

input.opts(new ClientOpts())
ClientOptInput input = new ClientOptInput()
.config(config)
.opts(new ClientOpts())
.systemProperties(systemProperties)
.swagger(swagger);

return input;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.swagger.codegen.statichtml;

import static java.util.Collections.singletonMap;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -44,18 +45,18 @@ public void setUp() throws Exception {
public void tearDown() throws Exception {
folder.delete();
}

@Test
public void testApiTags() throws Exception {
final Swagger swagger = new SwaggerParser().read("src/test/resources/2_0/petstore.json");

final int maxTagsToTest = 2; // how to flip it randomly from 2 to 1, and shuffle ops?
// if an op has a few tags it will be duplicated here, but it's exactly what we expect in doc
final List<Operation> expectedOperations = new ArrayList<Operation>();

final String capitalCommatizedTags = pickupFewTagsAndOps(swagger,
maxTagsToTest, expectedOperations);

final Collection<Object> seenOperations = new ArrayList<Object>();
CodegenConfig codegenConfig = new StaticHtmlGenerator(){ // new StaticDocCodegen(){
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
Expand All @@ -65,29 +66,25 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> ob
assertEquals(actualOperations.size(), expectedOperations.size(),
"Expectig the same size of ops for -Dapis="+capitalCommatizedTags +
" in fact, actual "+actualOperations+" doesn't seem like expecting "
+ expectedOperations);
+ expectedOperations);
return objs;
}
};
codegenConfig.setOutputDir(folder.getRoot().getAbsolutePath());

ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger)
.config(codegenConfig);

final String apisBackup = System.setProperty("apis", capitalCommatizedTags);
try {
DefaultGenerator gen = new DefaultGenerator();
gen.opts(clientOptInput);
gen.generate();
assertEquals(seenOperations.isEmpty(), false,
"something has been changed in code and now code bypass the mock above...");
} finally {
if (apisBackup!=null) {
System.setProperty("apis", apisBackup);
}else{
System.clearProperty("apis");
}
}

final ClientOptInput clientOptInput =
new ClientOptInput()
.opts(new ClientOpts())
.swagger(swagger)
.config(codegenConfig)
.systemProperties(singletonMap("apis", capitalCommatizedTags));


new DefaultGenerator()
.opts(clientOptInput)
.generate();
assertFalse(seenOperations.isEmpty(),
"something has been changed in code and now code bypass the mock above...");
}

protected String pickupFewTagsAndOps(final Swagger swagger,
Expand All @@ -107,27 +104,27 @@ protected String pickupFewTagsAndOps(final Swagger swagger,
}
}
}

final String capitalCommatizedTags = StringUtils.join(
Lists.transform(Lists.newArrayList(expectedTags),
new Function<String, String>() {
@Nullable
@Override
public String apply(final String input) {
return StringUtils.capitalize(input);
}
}), ",");
@Nullable
@Override
public String apply(final String input) {
return StringUtils.capitalize(input);
}
}), ",");
return capitalCommatizedTags;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
protected static Collection getOperations(Map<String, Object> objs) {
final ArrayList rez = new ArrayList();
final Map apiInfo = (Map)objs.get("apiInfo");
for(Object apiElem : ((List)apiInfo.get("apis"))){
Map<String, Object> api = (Map<String, Object>) apiElem;
rez.addAll( (Collection) // what if the same op goes on two tags??
((Map)api.get("operations")).get("operation"));
((Map)api.get("operations")).get("operation"));
}
return rez;
}
Expand Down