[Python][R] Escape single quotes in strings (#2808) (#2809)

This commit is contained in:
Felix Sonntag
2019-05-06 16:04:05 +02:00
committed by William Cheng
parent 632364e290
commit 6283625478
3 changed files with 11 additions and 2 deletions

View File

@@ -634,7 +634,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
return "'''" + p.getDefault() + "'''";
else
return "'" + p.getDefault() + "'";
return "'" + ((String) p.getDefault()).replaceAll("'","\'") + "'";
}
} else if (ModelUtils.isArraySchema(p)) {
if (p.getDefault() != null) {

View File

@@ -569,7 +569,7 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
return "'''" + p.getDefault() + "'''";
else
return "'" + p.getDefault() + "'";
return "'" + ((String) p.getDefault()).replaceAll("'","\'") + "'";
}
} else if (ModelUtils.isArraySchema(p)) {
if (p.getDefault() != null) {

View File

@@ -92,4 +92,13 @@ public class PythonClientCodegenTest {
// pattern_with_modifiers '/^pattern\d{3}$/i
Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i");
}
@Test(description = "test single quotes escape")
public void testSingleQuotes() {
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);
}
}