forked from loafle/openapi-generator-original
[Bash] Bash client script generator (#4541)
* Initial commit * Remormatted petstore tests * Added Bash codegen to main README.md * Added bash to integration tests * Fixed stdin detection in generated script * Added back ruby module
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package io.swagger.codegen.bash;
|
||||
|
||||
import io.swagger.codegen.AbstractOptionsTest;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.languages.BashClientCodegen;
|
||||
import io.swagger.codegen.options.BashClientOptionsProvider;
|
||||
|
||||
import mockit.Expectations;
|
||||
import mockit.Tested;
|
||||
|
||||
public class BashClientOptionsTest extends AbstractOptionsTest {
|
||||
|
||||
@Tested
|
||||
private BashClientCodegen clientCodegen;
|
||||
|
||||
public BashClientOptionsTest() {
|
||||
super(new BashClientOptionsProvider());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CodegenConfig getCodegenConfig() {
|
||||
return clientCodegen;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Override
|
||||
protected void setExpectations() {
|
||||
new Expectations(clientCodegen) {{
|
||||
clientCodegen.setCurlOptions(
|
||||
BashClientOptionsProvider.CURL_OPTIONS);
|
||||
times = 1;
|
||||
clientCodegen.setProcessMarkdown(
|
||||
Boolean.parseBoolean(
|
||||
BashClientOptionsProvider.PROCESS_MARKDOWN));
|
||||
times = 1;
|
||||
clientCodegen.setScriptName(
|
||||
BashClientOptionsProvider.SCRIPT_NAME);
|
||||
times = 1;
|
||||
clientCodegen.setGenerateBashCompletion(
|
||||
Boolean.parseBoolean(
|
||||
BashClientOptionsProvider.GENERATE_BASH_COMPLETION));
|
||||
times = 1;
|
||||
clientCodegen.setGenerateZshCompletion(
|
||||
Boolean.parseBoolean(
|
||||
BashClientOptionsProvider.GENERATE_ZSH_COMPLETION));
|
||||
times = 1;
|
||||
clientCodegen.setHostEnvironmentVariable(
|
||||
BashClientOptionsProvider.HOST_ENVIRONMENT_VARIABLE_NAME);
|
||||
times = 1;
|
||||
clientCodegen.setApiKeyAuthEnvironmentVariable(
|
||||
BashClientOptionsProvider.APIKEY_AUTH_ENVIRONMENT_VARIABLE_NAME);
|
||||
times = 1;
|
||||
}};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
package io.swagger.codegen.python;
|
||||
|
||||
import io.swagger.codegen.CodegenModel;
|
||||
import io.swagger.codegen.CodegenOperation;
|
||||
import io.swagger.codegen.CodegenProperty;
|
||||
import io.swagger.codegen.CodegenParameter;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
import io.swagger.codegen.languages.BashClientCodegen;
|
||||
import io.swagger.models.ArrayModel;
|
||||
import io.swagger.models.Model;
|
||||
import io.swagger.models.ModelImpl;
|
||||
import io.swagger.models.Operation;
|
||||
import io.swagger.models.Swagger;
|
||||
import io.swagger.models.properties.ArrayProperty;
|
||||
import io.swagger.models.properties.DateTimeProperty;
|
||||
import io.swagger.models.properties.LongProperty;
|
||||
import io.swagger.models.properties.MapProperty;
|
||||
import io.swagger.models.properties.RefProperty;
|
||||
import io.swagger.models.properties.StringProperty;
|
||||
import io.swagger.parser.SwaggerParser;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
import org.testng.annotations.ITestAnnotation;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings("static-method")
|
||||
public class BashTest {
|
||||
|
||||
@Test(description = "test basic petstore operation with Bash extensions")
|
||||
public void petstoreOperationTest() {
|
||||
|
||||
final Swagger swagger
|
||||
= new SwaggerParser()
|
||||
.read("src/test/resources/2_0/petstore-bash.json");
|
||||
final DefaultCodegen codegen = new BashClientCodegen();
|
||||
final Operation findPetsByStatusOperation
|
||||
= swagger.getPath("/pet/findByStatus").getGet();
|
||||
|
||||
final CodegenOperation op
|
||||
= codegen.fromOperation(
|
||||
"/pet/findByStatus",
|
||||
"GET",
|
||||
findPetsByStatusOperation,
|
||||
swagger.getDefinitions(),
|
||||
swagger);
|
||||
|
||||
Assert.assertTrue(
|
||||
op.vendorExtensions.containsKey("x-bash-codegen-sample"));
|
||||
|
||||
Assert.assertEquals(
|
||||
op.vendorExtensions.get("x-bash-codegen-description"),
|
||||
"Multiple status 'values' can be provided with comma separated strings");
|
||||
|
||||
}
|
||||
|
||||
@Test(description = "test basic petstore operation with example body")
|
||||
public void petstoreParameterExampleTest() {
|
||||
|
||||
final Swagger swagger
|
||||
= new SwaggerParser()
|
||||
.read("src/test/resources/2_0/petstore-bash.json");
|
||||
final DefaultCodegen codegen = new BashClientCodegen();
|
||||
final Operation addPetOperation
|
||||
= swagger.getPath("/pet").getPost();
|
||||
|
||||
final CodegenOperation op
|
||||
= codegen.fromOperation(
|
||||
"/pet",
|
||||
"POST",
|
||||
addPetOperation,
|
||||
swagger.getDefinitions(),
|
||||
swagger);
|
||||
|
||||
Assert.assertEquals(op.bodyParams.size(), 1);
|
||||
|
||||
CodegenParameter pet = op.bodyParams.get(0);
|
||||
|
||||
Assert.assertTrue(pet.vendorExtensions
|
||||
.containsKey("x-codegen-body-example"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test(description = "test Bash client codegen escapeText method")
|
||||
public void escapeTextTest() {
|
||||
final DefaultCodegen codegen = new BashClientCodegen();
|
||||
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("\\/"), "/");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("\\"), "\\\\");
|
||||
|
||||
|
||||
((BashClientCodegen)codegen).setProcessMarkdown(false);
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("__Bold text__"),
|
||||
"__Bold text__");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("**Bold text**"),
|
||||
"**Bold text**");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("*Italic text*"),
|
||||
"*Italic text*");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("_Italic text_"),
|
||||
"_Italic text_");
|
||||
|
||||
|
||||
((BashClientCodegen)codegen).setProcessMarkdown(true);
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("__Bold text__"),
|
||||
"$(tput bold) Bold text $(tput sgr0)");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("**Bold text**"),
|
||||
"$(tput bold) Bold text $(tput sgr0)");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("*Italic text*"),
|
||||
"$(tput dim) Italic text $(tput sgr0)");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("_Italic text_"),
|
||||
"$(tput dim) Italic text $(tput sgr0)");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("# SECTION NAME"),
|
||||
"\n$(tput bold)$(tput setaf 7)SECTION NAME$(tput sgr0)");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("## SECTION NAME"),
|
||||
"\n$(tput bold)$(tput setaf 7)SECTION NAME$(tput sgr0)");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText("### SECTION NAME"),
|
||||
"\n$(tput bold)$(tput setaf 7)SECTION NAME$(tput sgr0)");
|
||||
|
||||
Assert.assertEquals(codegen.escapeText(
|
||||
"```\nnice -n 100 mvn test\n```"),
|
||||
"\n---\nnice -n 100 mvn test\n---");
|
||||
}
|
||||
|
||||
@Test(description = "test Bash client codegen escapeUnsafeCharacters method")
|
||||
public void escapeUnsafeCharactersTest() {
|
||||
final DefaultCodegen codegen = new BashClientCodegen();
|
||||
|
||||
Assert.assertEquals(codegen.escapeUnsafeCharacters("`no backticks`"),
|
||||
"'no backticks'");
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test(description = "test Bash client codegen escapeReservedWord method")
|
||||
public void escapeReservedWordTest() {
|
||||
final DefaultCodegen codegen = new BashClientCodegen();
|
||||
|
||||
Assert.assertEquals(codegen.escapeReservedWord("case"), "_case");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package io.swagger.codegen.options;
|
||||
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.languages.BashClientCodegen;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class BashClientOptionsProvider implements OptionsProvider {
|
||||
|
||||
public static final String CURL_OPTIONS = "-k --tlsv1.2";
|
||||
public static final String PROCESS_MARKDOWN = "true";
|
||||
public static final String SCRIPT_NAME = "petstore-cli";
|
||||
public static final String GENERATE_BASH_COMPLETION = "true";
|
||||
public static final String GENERATE_ZSH_COMPLETION = "false";
|
||||
public static final String HOST_ENVIRONMENT_VARIABLE_NAME
|
||||
= "PETSTORE_HOSTNAME";
|
||||
public static final String BASIC_AUTH_ENVIRONMENT_VARIABLE_NAME
|
||||
= "PETSTORE_BASIC_AUTH";
|
||||
public static final String APIKEY_AUTH_ENVIRONMENT_VARIABLE_NAME
|
||||
= "PETSTORE_APIKEY";
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
return "bash";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> createOptions() {
|
||||
|
||||
ImmutableMap.Builder<String, String> builder
|
||||
= new ImmutableMap.Builder<String, String>();
|
||||
|
||||
return builder
|
||||
.put(BashClientCodegen.CURL_OPTIONS, CURL_OPTIONS)
|
||||
.put(BashClientCodegen.SCRIPT_NAME, SCRIPT_NAME)
|
||||
.put(BashClientCodegen.PROCESS_MARKDOWN, PROCESS_MARKDOWN)
|
||||
.put(BashClientCodegen.GENERATE_BASH_COMPLETION,
|
||||
GENERATE_BASH_COMPLETION)
|
||||
.put(BashClientCodegen.GENERATE_ZSH_COMPLETION,
|
||||
GENERATE_ZSH_COMPLETION)
|
||||
.put(BashClientCodegen.HOST_ENVIRONMENT_VARIABLE_NAME,
|
||||
HOST_ENVIRONMENT_VARIABLE_NAME)
|
||||
.put(BashClientCodegen.BASIC_AUTH_ENVIRONMENT_VARIABLE_NAME,
|
||||
BASIC_AUTH_ENVIRONMENT_VARIABLE_NAME)
|
||||
.put(BashClientCodegen.APIKEY_AUTH_ENVIRONMENT_VARIABLE_NAME,
|
||||
APIKEY_AUTH_ENVIRONMENT_VARIABLE_NAME)
|
||||
.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, "false")
|
||||
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, "false")
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isServer() {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user