Skip to content

Commit

Permalink
fix pattern in python flask (#5155)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Mar 22, 2017
1 parent e3d8dab commit a4d4b0f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
protected String packageVersion;
protected String controllerPackage;
protected String defaultController;
protected Map<Character, String> regexModifiers;

public FlaskConnexionCodegen() {
super();
Expand Down Expand Up @@ -110,6 +111,14 @@ public FlaskConnexionCodegen() {
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
supportingFiles.add(new SupportingFile("dockerignore.mustache", "", ".dockerignore"));

regexModifiers = new HashMap<Character, String>();
regexModifiers.put('i', "IGNORECASE");
regexModifiers.put('l', "LOCALE");
regexModifiers.put('m', "MULTILINE");
regexModifiers.put('s', "DOTALL");
regexModifiers.put('u', "UNICODE");
regexModifiers.put('x', "VERBOSE");

cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "python package name (convention: snake_case).")
.defaultValue("swagger_server"));
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "python package version.")
Expand Down Expand Up @@ -640,8 +649,42 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
if (StringUtils.isNotEmpty(property.pattern)) {
addImport(model, "import re");
}
postProcessPattern(property.pattern, property.vendorExtensions);
}

@Override
public void postProcessParameter(CodegenParameter parameter){
postProcessPattern(parameter.pattern, parameter.vendorExtensions);
}

/*
* The swagger pattern spec follows the Perl convention and style of modifiers. Python
* does not support this in as natural a way so it needs to convert it. See
* https://docs.python.org/2/howto/regex.html#compilation-flags for details.
*/
public void postProcessPattern(String pattern, Map<String, Object> vendorExtensions){
if(pattern != null) {
int i = pattern.lastIndexOf('/');

//Must follow Perl /pattern/modifiers convention
if(pattern.charAt(0) != '/' || i < 2) {
throw new IllegalArgumentException("Pattern must follow the Perl "
+ "/pattern/modifiers convention. "+pattern+" is not valid.");
}

String regex = pattern.substring(1, i).replace("'", "\\'");
List<String> modifiers = new ArrayList<String>();

for(char c : pattern.substring(i).toCharArray()) {
if(regexModifiers.containsKey(c)) {
String modifier = regexModifiers.get(c);
modifiers.add(modifier);
}
}

vendorExtensions.put("x-regex", regex);
vendorExtensions.put("x-modifiers", modifiers);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig

protected Map<Character, String> regexModifiers;

private String testFolder;
private String testFolder;

public PythonClientCodegen() {
super();
Expand Down Expand Up @@ -292,12 +292,12 @@ public String modelFileFolder() {

@Override
public String apiTestFileFolder() {
return outputFolder + File.separatorChar + testFolder;
return outputFolder + File.separatorChar + testFolder;
}

@Override
public String modelTestFileFolder() {
return outputFolder + File.separatorChar + testFolder;
return outputFolder + File.separatorChar + testFolder;
}

@Override
Expand Down Expand Up @@ -433,8 +433,8 @@ public String toModelFilename(String name) {

@Override
public String toModelTestFilename(String name) {
return "test_" + toModelFilename(name);
};
return "test_" + toModelFilename(name);
}

@Override
public String toApiFilename(String name) {
Expand All @@ -447,7 +447,7 @@ public String toApiFilename(String name) {

@Override
public String toApiTestFilename(String name) {
return "test_" + toApiFilename(name);
return "test_" + toApiFilename(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ paths:
type: "array"
items:
type: "string"
default: "available"
enum:
- "available"
- "pending"
- "sold"
default: "available"
collectionFormat: "csv"
responses:
200:
Expand Down

0 comments on commit a4d4b0f

Please sign in to comment.