Fix logic for removal of forward slash in RegGen pattern (#8219)

* rxgen version update

* rgxgen version 1.2

* update rgxgen version to 1.3

* remove start end slash in patterns

* Remove debug logs

* fix logic for removal of backslash

* fix logic for forward-slash removal

* fix logic for removing unwanted characters in regex

* add test for example value for string properties

* improvise regex validation logic for example generation

* complicate regex examples

* regex pattern correction

Co-authored-by: aani <aani>
This commit is contained in:
Aanisha Mishra 2020-12-21 07:45:32 +05:30 committed by GitHub
parent 87926d0d1c
commit 72dc0cfb07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 3 deletions

View File

@ -797,9 +797,7 @@ public class PythonLegacyClientCodegen extends DefaultCodegen implements Codegen
if ("Number".equalsIgnoreCase(schema.getFormat())) {return "1";}
if (StringUtils.isNotBlank(schema.getPattern())) {
String pattern = schema.getPattern();
while (pattern.startsWith("/")) pattern = pattern.substring(0, pattern.length()-1);
while (pattern.endsWith("/")) pattern = pattern.substring(1);
RgxGen rgxGen = new RgxGen(pattern);
RgxGen rgxGen = new RgxGen(patternCorrection(pattern));
// this seed makes it so if we have [a-z] we pick a
Random random = new Random(18);
String sample = rgxGen.generate(random);
@ -1052,4 +1050,14 @@ public class PythonLegacyClientCodegen extends DefaultCodegen implements Codegen
}
}
}
public String patternCorrection(String pattern){
// Java does not recognize starting and ending forward slashes and mode modifiers
// It considers them as characters with no special meaning and tries to find them in the match string
boolean checkEnding = pattern.endsWith("/i") || pattern.endsWith("/g") || pattern.endsWith("/m");
if (checkEnding) pattern = pattern.substring(0, pattern.length()-2);
if (pattern.endsWith("/")) pattern = pattern.substring(0, pattern.length()-1);
if (pattern.startsWith("/")) pattern = pattern.substring(1);
return pattern;
}
}

View File

@ -18,17 +18,20 @@
package org.openapitools.codegen.python;
import com.google.common.collect.Sets;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.PythonLegacyClientCodegen;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;
public class PythonLegacyClientCodegenTest {
@ -93,6 +96,25 @@ public class PythonLegacyClientCodegenTest {
Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i");
}
@Test(description = "test generated example values for string properties")
public void testGeneratedExampleValues() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml");
final PythonLegacyClientCodegen codegen = new PythonLegacyClientCodegen();
codegen.setOpenAPI(openAPI);
final Schema dummyUserSchema = openAPI.getComponents().getSchemas().get("DummyUser");
final Schema nameSchema = (Schema) dummyUserSchema.getProperties().get("name");
final Schema numberSchema = (Schema) dummyUserSchema.getProperties().get("number");
final Schema addressSchema = (Schema) dummyUserSchema.getProperties().get("address");
final String namePattern = codegen.patternCorrection(nameSchema.getPattern());
final String numberPattern = codegen.patternCorrection(numberSchema.getPattern());
final String addressPattern = codegen.patternCorrection(addressSchema.getPattern());
Assert.assertTrue(codegen.escapeQuotationMark(codegen.toExampleValue(nameSchema)).matches(namePattern));
Assert.assertTrue(codegen.escapeQuotationMark(codegen.toExampleValue(numberSchema)).matches(numberPattern));
Assert.assertTrue(codegen.escapeQuotationMark(codegen.toExampleValue(addressSchema)).matches(addressPattern));
}
@Test(description = "test single quotes escape")
public void testSingleQuotes() {
final PythonLegacyClientCodegen codegen = new PythonLegacyClientCodegen();

View File

@ -118,3 +118,15 @@ components:
properties:
city:
type: string
DummyUser:
type: object
properties:
name:
type: string
pattern: '/^[a-z\s]+$/'
number:
type: string
pattern: '/[0-9]{10}/g'
address:
type: string
pattern: '/^[a-z0-9\s]+$/i'