diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java index ef92f7ce0db4..e8df9d1b8ca5 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java @@ -684,7 +684,8 @@ public String toDefaultValue(Schema p) { if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find()) return "'''" + p.getDefault() + "'''"; else - return "'" + ((String) p.getDefault()).replaceAll("'", "\'") + "'"; + // wrap using double quotes to avoid the need to escape any embedded single quotes + return "\"" + p.getDefault() + "\""; } } else if (ModelUtils.isArraySchema(p)) { if (p.getDefault() != null) { @@ -728,7 +729,8 @@ private String toExampleValueRecursive(Schema schema, List included_sche if (StringUtils.isNotBlank(example) && !"null".equals(example)) { if (ModelUtils.isStringSchema(schema)) { - example = "'" + example + "'"; + // wrap using double quotes to avoid the need to escape any embedded single quotes + example = "\"" + example + "\""; } return example; } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java index 565e6848f64b..3c2544edf91e 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java @@ -93,12 +93,21 @@ public void testRegularExpressionOpenAPISchemaVersion3() { Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i"); } - @Test(description = "test single quotes escape") - public void testSingleQuotes() { + @Test(description = "test default value with single quotes") + public void testSingleQuotesDefaultValue() { 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); + Assert.assertEquals(defaultValue, "\"Text containing 'single' quote\""); + } + + @Test(description = "test example value with single quotes") + public void testSingleQuotesExampleValue() { + final PythonClientCodegen codegen = new PythonClientCodegen(); + StringSchema schema = new StringSchema(); + schema.setExample("Text containing 'single' quote"); + String exampleValue = codegen.toExampleValue(schema); + Assert.assertEquals(exampleValue, "\"Text containing 'single' quote\""); } }