Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python][Client] Fix delimiter collision issue #5981 #6451

Merged
merged 5 commits into from
Jun 1, 2020
Merged
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 @@ -683,8 +683,12 @@ public String toDefaultValue(Schema p) {
if (p.getDefault() != null) {
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
return "'''" + p.getDefault() + "'''";
else if (p.getEnum() == null)
// wrap using double quotes to avoid the need to escape any embedded single quotes
return "\"" + p.getDefault() + "\"";
else
return "'" + ((String) p.getDefault()).replaceAll("'", "\'") + "'";
// convert to enum var name later in postProcessModels
return (String) p.getDefault();
}
} else if (ModelUtils.isArraySchema(p)) {
if (p.getDefault() != null) {
Expand Down Expand Up @@ -728,7 +732,8 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche

if (StringUtils.isNotBlank(example) && !"null".equals(example)) {
if (ModelUtils.isStringSchema(schema)) {
example = "'" + example + "'";
// wrap using double quotes to avoid the need to escape any embedded single quotes
example = "\"" + example + "\"";
}
return example;
}
Expand Down Expand Up @@ -902,43 +907,45 @@ public void setParameterExampleValue(CodegenParameter p) {
type = p.dataType;
}

if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
if (type != null) {
if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
if (example == null) {
example = p.paramName + "_example";
example = p.paramName + "_example";
}
example = "'" + escapeText(example) + "'";
} else if ("Integer".equals(type) || "int".equals(type)) {
} else if ("Integer".equals(type) || "int".equals(type)) {
if (example == null) {
example = "56";
example = "56";
}
} else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) {
} else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) {
if (example == null) {
example = "3.4";
example = "3.4";
}
} else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) {
} else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) {
if (example == null) {
example = "True";
example = "True";
}
} else if ("file".equalsIgnoreCase(type)) {
} else if ("file".equalsIgnoreCase(type)) {
if (example == null) {
example = "/path/to/file";
example = "/path/to/file";
}
example = "'" + escapeText(example) + "'";
} else if ("Date".equalsIgnoreCase(type)) {
} else if ("Date".equalsIgnoreCase(type)) {
if (example == null) {
example = "2013-10-20";
example = "2013-10-20";
}
example = "'" + escapeText(example) + "'";
} else if ("DateTime".equalsIgnoreCase(type)) {
} else if ("DateTime".equalsIgnoreCase(type)) {
if (example == null) {
example = "2013-10-20T19:20:30+01:00";
example = "2013-10-20T19:20:30+01:00";
}
example = "'" + escapeText(example) + "'";
} else if (!languageSpecificPrimitives.contains(type)) {
} else if (!languageSpecificPrimitives.contains(type)) {
// type is a model class, e.g. User
example = this.packageName + "." + type + "()";
} else {
} else {
LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI. Looks like these changes are not indented properly (2-space vs 4-space). I can fix it later after merging this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. Thank you!

}

if (example == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import java.util.HashSet;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModelFactory;
import org.openapitools.codegen.CodegenModelType;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.PythonClientCodegen;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -93,12 +99,99 @@ public void testRegularExpressionOpenAPISchemaVersion3() {
Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i");
}

@Test(description = "test single quotes escape")
public void testSingleQuotes() {
@Test(description = "test default value with single quotes")
public void testSingleQuotesDefaultValue() {
final PythonClientCodegen codegen = new PythonClientCodegen();
StringSchema schema = new StringSchema();
schema.setDefault("Text containing 'single' quote");
String defaultValue = codegen.toDefaultValue(schema);
Assert.assertEquals("'Text containing \'single\' quote'", defaultValue);
Assert.assertEquals(defaultValue, "\"Text containing 'single' quote\"");
}

@Test(description = "test example value with single quotes")
public void testSingleQuotesExampleValue() {
final PythonClientCodegen codegen = new PythonClientCodegen();
StringSchema schema = new StringSchema();
schema.setExample("Text containing 'single' quote");
String exampleValue = codegen.toExampleValue(schema);
Assert.assertEquals(exampleValue, "\"Text containing 'single' quote\"");
}

@Test
public void testFormParameterHasDefaultValue() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml");
final PythonClientCodegen codegen = new PythonClientCodegen();
codegen.setOpenAPI(openAPI);

Schema requestBodySchema = ModelUtils.getSchemaFromRequestBody(openAPI.getPaths().get("/fake").getGet().getRequestBody());
CodegenParameter codegenParameter = codegen.fromFormProperty("enum_form_string", (Schema) requestBodySchema.getProperties().get("enum_form_string"), new HashSet<String>());

Assert.assertEquals(codegenParameter.defaultValue, "-efg");
}

@Test
public void testExample1() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml");
final PythonClientCodegen codegen = new PythonClientCodegen();

Operation operation = openAPI.getPaths().get("/example1/singular").getGet();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0));

Assert.assertEquals(codegenParameter.example, "example1 value");

Operation operation2 = openAPI.getPaths().get("/example1/plural").getGet();
CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter2, operation2.getParameters().get(0));

Assert.assertEquals(codegenParameter2.example, "An example1 value");
}

@Test
public void testExample2() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml");
final PythonClientCodegen codegen = new PythonClientCodegen();

Operation operation = openAPI.getPaths().get("/example2/singular").getGet();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0));

Assert.assertEquals(codegenParameter.example, "example2 value");
}

@Test
public void testExample3() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml");
final PythonClientCodegen codegen = new PythonClientCodegen();

Operation operation = openAPI.getPaths().get("/example3/singular").getGet();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0));

Assert.assertEquals(codegenParameter.example, "example3: parameter value");

Operation operation2 = openAPI.getPaths().get("/example3/plural").getGet();
CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter2, operation2.getParameters().get(0));

Assert.assertEquals(codegenParameter2.example, "example3: parameter value");
}

@Test
public void testExample4() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml");
final PythonClientCodegen codegen = new PythonClientCodegen();

Operation operation = openAPI.getPaths().get("/example4/singular").getPost();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getRequestBody());

Assert.assertEquals(codegenParameter.example, "example4 value");

Operation operation2 = openAPI.getPaths().get("/example4/plural").getPost();
CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter2, operation2.getRequestBody());

Assert.assertEquals(codegenParameter2.example, "An example4 value");
}
}