Skip to content

Commit

Permalink
Fix escaped regex values in Python client (OpenAPITools#1517).
Browse files Browse the repository at this point in the history
* Override PythonClientCodegen toRegularExpression() method to correct
  issue with backslashes being escaped. This issue was a result of
  calling escapeText() in the parent DefaultCodegen class.
  • Loading branch information
Raznic committed Nov 24, 2018
1 parent bf2932d commit 1d2ef46
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,23 @@ public String toDefaultValue(Schema p) {
return null;
}

@Override
public String toRegularExpression(String pattern) {
if (StringUtils.isEmpty(pattern)) {
return pattern;
}

String regexString = pattern;
if (!regexString.startsWith("/")) {
regexString = "/" + regexString;
}
if (StringUtils.countMatches(regexString, '/') == 1) {
// we only have forward slash inserted at start... adding one to end
regexString = regexString + "/";
}
return regexString;
}

@Override
public void setParameterExampleValue(CodegenParameter p) {
String example;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

package org.openapitools.codegen.python;

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.CodegenOperation;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -54,4 +59,26 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
}

@Test(description = "test regex patterns")
public void testRegularExpressionOpenAPISchemaVersion3() {
final OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/test_regex.yaml", null, new ParseOptions()).getOpenAPI();
final PythonClientCodegen codegen = new PythonClientCodegen();
final String path = "/ping";
final Operation p = openAPI.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, openAPI.getComponents().getSchemas());
// pattern_no_forward_slashes '^pattern$'
Assert.assertEquals(op.allParams.get(0).pattern, "/^pattern$/");
// pattern_two_slashes '/^pattern$/i'
Assert.assertEquals(op.allParams.get(1).pattern, "/^pattern$/i");
// pattern_one_slash_start '/^pattern$'
Assert.assertEquals(op.allParams.get(2).pattern, "/^pattern$/");
// pattern_one_slash_end '^pattern$/'
Assert.assertEquals(op.allParams.get(3).pattern, "/^pattern$/");
// pattern_one_slash_near_end '^pattern$/im'
Assert.assertEquals(op.allParams.get(4).pattern, "/^pattern$/im");
// pattern_dont_escape_backslash '/^pattern\d{3}$/i' NOTE: the double \ is to escape \ in string but is read as single \
Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i");

}
}

0 comments on commit 1d2ef46

Please sign in to comment.