@@ -1891,34 +1891,21 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
18911891 example = "2" ;
18921892 } else if (StringUtils .isNotBlank (schema .getPattern ())) {
18931893 String pattern = schema .getPattern ();
1894+ List <Object > results = getPatternAndModifiers (pattern );
1895+ String extractedPattern = (String ) results .get (0 );
1896+ List <String > regexFlags = (List <String >) results .get (1 );
18941897 /*
18951898 RxGen does not support our ECMA dialect https://github.com/curious-odd-man/RgxGen/issues/56
18961899 So strip off the leading / and trailing / and turn on ignore case if we have it
18971900 */
1898- Pattern valueExtractor = Pattern .compile ("^/?(.+?)/?([gim]?)$" );
1899- Matcher m = valueExtractor .matcher (pattern );
19001901 RgxGen rgxGen = null ;
1901- if (m .find ()) {
1902- int groupCount = m .groupCount ();
1903- if (groupCount == 1 ) {
1904- // only pattern found
1905- String isolatedPattern = m .group (1 );
1906- rgxGen = new RgxGen (isolatedPattern );
1907- } else if (groupCount == 2 ) {
1908- // patterns and flag found
1909- String isolatedPattern = m .group (1 );
1910- String flags = m .group (2 );
1911- if (flags .contains ("i" )) {
1912- rgxGen = new RgxGen (isolatedPattern );
1913- RgxGenProperties properties = new RgxGenProperties ();
1914- RgxGenOption .CASE_INSENSITIVE .setInProperties (properties , true );
1915- rgxGen .setProperties (properties );
1916- } else {
1917- rgxGen = new RgxGen (isolatedPattern );
1918- }
1919- }
1902+ if (regexFlags .size () > 0 && regexFlags .contains ("i" )) {
1903+ rgxGen = new RgxGen (extractedPattern );
1904+ RgxGenProperties properties = new RgxGenProperties ();
1905+ RgxGenOption .CASE_INSENSITIVE .setInProperties (properties , true );
1906+ rgxGen .setProperties (properties );
19201907 } else {
1921- rgxGen = new RgxGen (pattern );
1908+ rgxGen = new RgxGen (extractedPattern );
19221909 }
19231910
19241911 // this seed makes it so if we have [a-z] we pick a
@@ -2358,6 +2345,16 @@ protected void updatePropertyForString(CodegenProperty property, Schema p) {
23582345 property .pattern = toRegularExpression (p .getPattern ());
23592346 }
23602347
2348+ @ Override
2349+ public String toRegularExpression (String pattern ) {
2350+ if (pattern == null ) {
2351+ return null ;
2352+ }
2353+ List <Object > results = getPatternAndModifiers (pattern );
2354+ String extractedPattern = (String ) results .get (0 );
2355+ return extractedPattern ;
2356+ }
2357+
23612358 protected void updatePropertyForNumber (CodegenProperty property , Schema p ) {
23622359 property .setIsNumber (true );
23632360 // float and double differentiation is determined with format info
@@ -2482,35 +2479,61 @@ public ModelsMap postProcessModels(ModelsMap objs) {
24822479 return postProcessModelsEnum (objs );
24832480 }
24842481
2482+ /**
2483+ * @param pattern the regex pattern
2484+ * @return List<String pattern, List<String modifer>>
2485+ */
2486+ private List <Object > getPatternAndModifiers (String pattern ) {
2487+ /*
2488+ Notes:
2489+ RxGen does not support our ECMA dialect https://github.com/curious-odd-man/RgxGen/issues/56
2490+ So strip off the leading / and trailing / and turn on ignore case if we have it
2491+
2492+ json schema test cases omit the leading and trailing /s, so make sure that the regex allows that
2493+ */
2494+ Pattern valueExtractor = Pattern .compile ("^/?(.+?)/?([sim]?)$" );
2495+ Matcher m = valueExtractor .matcher (pattern );
2496+ if (m .find ()) {
2497+ int groupCount = m .groupCount ();
2498+ if (groupCount == 1 ) {
2499+ // only pattern found
2500+ String isolatedPattern = m .group (1 );
2501+ return Arrays .asList (isolatedPattern , null );
2502+ } else if (groupCount == 2 ) {
2503+ List <String > modifiers = new ArrayList <String >();
2504+ // patterns and flag found
2505+ String isolatedPattern = m .group (1 );
2506+ String flags = m .group (2 );
2507+ if (flags .contains ("s" )) {
2508+ modifiers .add ("DOTALL" );
2509+ }
2510+ if (flags .contains ("i" )) {
2511+ modifiers .add ("IGNORECASE" );
2512+ }
2513+ if (flags .contains ("m" )) {
2514+ modifiers .add ("MULTILINE" );
2515+ }
2516+ return Arrays .asList (isolatedPattern , modifiers );
2517+ }
2518+ }
2519+ return Arrays .asList (pattern , new ArrayList <String >());
2520+ }
2521+
24852522 /*
24862523 * The OpenAPI pattern spec follows the Perl convention and style of modifiers. Python
24872524 * does not support this in as natural a way so it needs to convert it. See
24882525 * https://docs.python.org/2/howto/regex.html#compilation-flags for details.
24892526 */
24902527 public void postProcessPattern (String pattern , Map <String , Object > vendorExtensions ) {
24912528 if (pattern != null ) {
2492- int regexLength = pattern .length ();
2493- String regex = pattern ;
2494- int i = pattern .lastIndexOf ('/' );
2495- if (regexLength >= 2 && pattern .charAt (0 ) == '/' && i != -1 ) {
2496- // json schema tests do not include the leading and trailing slashes
2497- // so I do not think that they are required
2498- regex = pattern .substring (1 , i );
2499- }
2500- regex = regex .replace ("'" , "\\ '" );
2501- List <String > modifiers = new ArrayList <String >();
2502-
2503- if (i != -1 ) {
2504- for (char c : pattern .substring (i ).toCharArray ()) {
2505- if (regexModifiers .containsKey (c )) {
2506- String modifier = regexModifiers .get (c );
2507- modifiers .add (modifier );
2508- }
2509- }
2510- }
2529+ List <Object > results = getPatternAndModifiers (pattern );
2530+ String extractedPattern = (String ) results .get (0 );
2531+ List <String > modifiers = (List <String >) results .get (1 );
25112532
2512- vendorExtensions .put ("x-regex" , regex );
2513- vendorExtensions .put ("x-modifiers" , modifiers );
2533+ vendorExtensions .put ("x-regex" , extractedPattern );
2534+ if (modifiers .size () > 0 ) {
2535+ vendorExtensions .put ("x-modifiers" , modifiers );
2536+ }
25142537 }
25152538 }
25162539
0 commit comments