Skip to content

Commit

Permalink
[Python][R] Escape single quotes in strings (#2808) (#2809)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsonntag authored and wing328 committed May 6, 2019
1 parent 632364e commit 6283625
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ public String toDefaultValue(Schema p) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ public String toDefaultValue(Schema p) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,13 @@ public void testRegularExpressionOpenAPISchemaVersion3() {
// 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);
}
}

0 comments on commit 6283625

Please sign in to comment.