Skip to content

Commit 7a3ea28

Browse files
authored
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.
1 parent 63afd45 commit 7a3ea28

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

Diff for: modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public String toDefaultValue(Schema p) {
214214
String defaultValue = String.valueOf(p.getDefault());
215215
if (defaultValue != null) {
216216
defaultValue = defaultValue.replace("\\", "\\\\")
217-
.replace("'", "\'");
217+
.replace("'", "\\'");
218218
if (Pattern.compile("\r\n|\r|\n").matcher(defaultValue).find()) {
219219
return "'''" + defaultValue + "'''";
220220
} else {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void testSingleQuotes() {
139139
StringSchema schema = new StringSchema();
140140
schema.setDefault("Text containing 'single' quote");
141141
String defaultValue = codegen.toDefaultValue(schema);
142-
Assert.assertEquals("'Text containing \'single\' quote'", defaultValue);
142+
Assert.assertEquals("'Text containing \\'single\\' quote'", defaultValue);
143143
}
144144

145145
@Test(description = "test backslash default")

0 commit comments

Comments
 (0)