Skip to content

Commit 6283625

Browse files
fsonntagwing328
authored andcommitted
[Python][R] Escape single quotes in strings (#2808) (#2809)
1 parent 632364e commit 6283625

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ public String toDefaultValue(Schema p) {
634634
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
635635
return "'''" + p.getDefault() + "'''";
636636
else
637-
return "'" + p.getDefault() + "'";
637+
return "'" + ((String) p.getDefault()).replaceAll("'","\'") + "'";
638638
}
639639
} else if (ModelUtils.isArraySchema(p)) {
640640
if (p.getDefault() != null) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ public String toDefaultValue(Schema p) {
569569
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
570570
return "'''" + p.getDefault() + "'''";
571571
else
572-
return "'" + p.getDefault() + "'";
572+
return "'" + ((String) p.getDefault()).replaceAll("'","\'") + "'";
573573
}
574574
} else if (ModelUtils.isArraySchema(p)) {
575575
if (p.getDefault() != null) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,13 @@ public void testRegularExpressionOpenAPISchemaVersion3() {
9292
// pattern_with_modifiers '/^pattern\d{3}$/i
9393
Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i");
9494
}
95+
96+
@Test(description = "test single quotes escape")
97+
public void testSingleQuotes() {
98+
final PythonClientCodegen codegen = new PythonClientCodegen();
99+
StringSchema schema = new StringSchema();
100+
schema.setDefault("Text containing 'single' quote");
101+
String defaultValue = codegen.toDefaultValue(schema);
102+
Assert.assertEquals("'Text containing \'single\' quote'", defaultValue);
103+
}
95104
}

0 commit comments

Comments
 (0)