Skip to content

Commit

Permalink
[python][client] Fix delimiter collision (OpenAPITools#5981)
Browse files Browse the repository at this point in the history
  • Loading branch information
fullcircle23 committed May 27, 2020
1 parent ddcda1e commit 3fa218f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -728,7 +729,8 @@ private String toExampleValueRecursive(Schema schema, List<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\"");
}
}

0 comments on commit 3fa218f

Please sign in to comment.