Fix single quote escaping for python code generator (and its test) (#21021)

Hard to spot, but in Java the strings "'" and "\'" are identical, so
the implementation and its test were broken identically.
This commit is contained in:
Curd Becker 2025-04-07 19:10:09 +02:00 committed by GitHub
parent 63afd455f7
commit 7a3ea2872a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 2 additions and 2 deletions

View File

@ -214,7 +214,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
String defaultValue = String.valueOf(p.getDefault());
if (defaultValue != null) {
defaultValue = defaultValue.replace("\\", "\\\\")
.replace("'", "\'");
.replace("'", "\\'");
if (Pattern.compile("\r\n|\r|\n").matcher(defaultValue).find()) {
return "'''" + defaultValue + "'''";
} else {

View File

@ -139,7 +139,7 @@ public class PythonClientCodegenTest {
StringSchema schema = new StringSchema();
schema.setDefault("Text containing 'single' quote");
String defaultValue = codegen.toDefaultValue(schema);
Assert.assertEquals("'Text containing \'single\' quote'", defaultValue);
Assert.assertEquals("'Text containing \\'single\\' quote'", defaultValue);
}
@Test(description = "test backslash default")