Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit f427033

Browse files
committed
Pattern code fixed
1 parent b0f69bc commit f427033

File tree

8 files changed

+49
-35
lines changed

8 files changed

+49
-35
lines changed

samples/client/petstore/java/docs/components/schemas/Apple.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ Apple.AppleMap validatedPayload =
4545
MapMaker.makeMap(
4646
new AbstractMap.SimpleEntry<>(
4747
"cultivar",
48-
"\ziEEpmVvrKlTttzGFqCEG"
48+
"IaQnEaqioxT oASzjxaSH"
4949
),
5050
new AbstractMap.SimpleEntry<>(
5151
"origin",
52-
"\ziEEpmVvrKlTttzGFqCEG"
52+
"IaQnEaqioxT oASzjxaSH"
5353
)
5454
),
5555
configuration
@@ -114,7 +114,7 @@ static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSch
114114
115115
// String validation
116116
String validatedPayload = Apple.Origin.validate(
117-
"\ziEEpmVvrKlTttzGFqCEG",
117+
"IaQnEaqioxT oASzjxaSH",
118118
configuration
119119
);
120120
```
@@ -152,7 +152,7 @@ static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSch
152152
153153
// String validation
154154
String validatedPayload = Apple.Cultivar.validate(
155-
"\ziEEpmVvrKlTttzGFqCEG",
155+
"IaQnEaqioxT oASzjxaSH",
156156
configuration
157157
);
158158
```

samples/client/petstore/java/docs/components/schemas/FormatTest.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ FormatTest.FormatTestMap validatedPayload =
134134
),
135135
new AbstractMap.SimpleEntry<>(
136136
"pattern_with_digits",
137-
"\dddddddddd"
137+
"0480728880"
138138
),
139139
new AbstractMap.SimpleEntry<>(
140140
"pattern_with_digits_and_delimiter",
141-
"IMage_\dd"
141+
"IMage_88"
142142
),
143143
new AbstractMap.SimpleEntry<>(
144144
"noneProp",
@@ -251,7 +251,7 @@ static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSch
251251
252252
// String validation
253253
String validatedPayload = FormatTest.PatternWithDigitsAndDelimiter.validate(
254-
"IMage_\dd",
254+
"IMage_88",
255255
configuration
256256
);
257257
```
@@ -292,7 +292,7 @@ static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSch
292292
293293
// String validation
294294
String validatedPayload = FormatTest.PatternWithDigits.validate(
295-
"\dddddddddd",
295+
"0480728880",
296296
configuration
297297
);
298298
```

src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,7 +2316,7 @@ private Object getNumberFromSchema(CodegenSchema schema) {
23162316
}
23172317

23182318
private String getPatternExample(CodegenPatternInfo patternInfo) {
2319-
String extractedPattern = patternInfo.pattern;
2319+
String extractedPattern = patternInfo.pattern.original;
23202320
LinkedHashSet<String> regexFlags = patternInfo.flags;
23212321
/*
23222322
RxGen does not support our ECMA dialect https://github.com/curious-odd-man/RgxGen/issues/56
@@ -2334,7 +2334,8 @@ private String getPatternExample(CodegenPatternInfo patternInfo) {
23342334

23352335
// this seed makes it so if we have [a-z] we pick a
23362336
Random random = new Random(18);
2337-
return rgxGen.generate(random);
2337+
String result = rgxGen.generate(random);
2338+
return result;
23382339
}
23392340

23402341
private Object getStringFromSchema(CodegenSchema schema) {
@@ -4704,15 +4705,21 @@ protected EnumInfo getEnumInfo(ArrayList<Object> values, Schema schema, String c
47044705
*/
47054706
public CodegenPatternInfo addRegularExpressionDelimiter(String pattern) {
47064707
if (StringUtils.isEmpty(pattern)) {
4707-
return new CodegenPatternInfo(pattern, null);
4708+
return new CodegenPatternInfo(
4709+
new CodegenText(pattern, escapeUnsafeCharacters(pattern)),
4710+
null
4711+
);
47084712
}
47094713

47104714
String usedPattern = pattern;
47114715
if (!pattern.matches("^/.*")) {
47124716
usedPattern = "/" + pattern.replaceAll("/", "\\\\/") + "/";
47134717
}
47144718

4715-
return new CodegenPatternInfo(usedPattern, null);
4719+
return new CodegenPatternInfo(
4720+
new CodegenText(usedPattern, escapeUnsafeCharacters(usedPattern)),
4721+
null
4722+
);
47164723
}
47174724

47184725
/**

src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.openapijsonschematools.codegen.generators.openapimodels.CodegenResponse;
3636
import org.openapijsonschematools.codegen.generators.openapimodels.CodegenSchema;
3737
import org.openapijsonschematools.codegen.generators.openapimodels.CodegenSecurityScheme;
38+
import org.openapijsonschematools.codegen.generators.openapimodels.CodegenText;
3839
import org.openapijsonschematools.codegen.generators.openapimodels.EnumInfo;
3940
import org.openapijsonschematools.codegen.templating.HandlebarsEngineAdapter;
4041
import org.openapijsonschematools.codegen.templating.SupportingFile;
@@ -1846,28 +1847,30 @@ public CodegenPatternInfo getPatternInfo(String pattern) {
18461847
if (pattern == null) {
18471848
return null;
18481849
}
1849-
String usedPattern = escapeUnsafeCharacters(pattern);
1850-
Matcher m = patternRegex.matcher(usedPattern);
1850+
Matcher m = patternRegex.matcher(pattern);
18511851
if (m.find()) {
18521852
int groupCount = m.groupCount();
1853-
if (groupCount == 1) {
1854-
// only pattern found
1853+
boolean patternWithNoFlags = groupCount == 1;
1854+
boolean patternWithFlags = groupCount == 2;
1855+
if (patternWithNoFlags) {
18551856
String isolatedPattern = m.group(1);
1856-
return new CodegenPatternInfo(isolatedPattern, null);
1857-
} else if (groupCount == 2) {
1858-
// patterns and flag found
1857+
CodegenText usedPattern = new CodegenText(isolatedPattern, escapeUnsafeCharacters(isolatedPattern));
1858+
return new CodegenPatternInfo(usedPattern, null);
1859+
} else if (patternWithFlags) {
18591860
String isolatedPattern = m.group(1);
1861+
CodegenText usedPattern = new CodegenText(isolatedPattern, escapeUnsafeCharacters(isolatedPattern));
18601862
String foundFlags = m.group(2);
18611863
if (foundFlags.isEmpty()) {
1862-
return new CodegenPatternInfo(isolatedPattern, null);
1864+
return new CodegenPatternInfo(usedPattern, null);
18631865
}
18641866
LinkedHashSet<String> flags = new LinkedHashSet<>();
18651867
for (Character c: foundFlags.toCharArray()) {
18661868
flags.add(c.toString());
18671869
}
1868-
return new CodegenPatternInfo(isolatedPattern, flags);
1870+
return new CodegenPatternInfo(usedPattern, flags);
18691871
}
18701872
}
1873+
CodegenText usedPattern = new CodegenText(pattern, escapeUnsafeCharacters(pattern));
18711874
return new CodegenPatternInfo(usedPattern, null);
18721875
}
18731876
}

src/main/java/org/openapijsonschematools/codegen/generators/PythonClientGenerator.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.openapijsonschematools.codegen.generators.openapimodels.CodegenDiscriminator;
3434
import org.openapijsonschematools.codegen.generators.openapimodels.CodegenPatternInfo;
3535
import org.openapijsonschematools.codegen.generators.openapimodels.CodegenSchema;
36+
import org.openapijsonschematools.codegen.generators.openapimodels.CodegenText;
3637
import org.openapijsonschematools.codegen.templating.MustacheEngineAdapter;
3738
import org.openapijsonschematools.codegen.templating.SupportingFile;
3839
import org.openapijsonschematools.codegen.generators.generatormetadata.features.DataTypeFeature;
@@ -1476,7 +1477,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
14761477
} else if (StringUtils.isNotBlank(schema.getPattern())) {
14771478
String pattern = schema.getPattern();
14781479
CodegenPatternInfo results = getPatternInfo(pattern);
1479-
String extractedPattern = results.pattern;
1480+
String extractedPattern = results.pattern.original;
14801481
LinkedHashSet<String> regexFlags = results.flags;
14811482
/*
14821483
RxGen does not support our ECMA dialect https://github.com/curious-odd-man/RgxGen/issues/56
@@ -1789,25 +1790,28 @@ public CodegenPatternInfo getPatternInfo(String pattern) {
17891790
Matcher m = patternRegex.matcher(pattern);
17901791
if (m.find()) {
17911792
int groupCount = m.groupCount();
1792-
if (groupCount == 1) {
1793-
// only pattern found
1793+
boolean patternWithNoFlags = groupCount == 1;
1794+
boolean patternWithFlags = groupCount == 2;
1795+
if (patternWithNoFlags) {
17941796
String isolatedPattern = m.group(1);
1795-
return new CodegenPatternInfo(isolatedPattern, null);
1796-
} else if (groupCount == 2) {
1797-
// patterns and flag found
1797+
CodegenText usedPattern = new CodegenText(isolatedPattern, escapeUnsafeCharacters(isolatedPattern));
1798+
return new CodegenPatternInfo(usedPattern, null);
1799+
} else if (patternWithFlags) {
17981800
String isolatedPattern = m.group(1);
1801+
CodegenText usedPattern = new CodegenText(isolatedPattern, escapeUnsafeCharacters(isolatedPattern));
17991802
String foundFlags = m.group(2);
18001803
if (foundFlags.isEmpty()) {
1801-
return new CodegenPatternInfo(isolatedPattern, null);
1804+
return new CodegenPatternInfo(usedPattern, null);
18021805
}
18031806
LinkedHashSet<String> flags = new LinkedHashSet<>();
18041807
for (Character c: foundFlags.toCharArray()) {
18051808
flags.add(c.toString());
18061809
}
1807-
return new CodegenPatternInfo(isolatedPattern, flags);
1810+
return new CodegenPatternInfo(usedPattern, flags);
18081811
}
18091812
}
1810-
return new CodegenPatternInfo(pattern, null);
1813+
CodegenText usedPattern = new CodegenText(pattern, escapeUnsafeCharacters(pattern));
1814+
return new CodegenPatternInfo(usedPattern, null);
18111815
}
18121816

18131817
@Override

src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenPatternInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import java.util.LinkedHashSet;
44

55
public class CodegenPatternInfo {
6-
public final String pattern;
6+
public final CodegenText pattern;
77
public final LinkedHashSet<String> flags;
88

9-
public CodegenPatternInfo(String pattern, LinkedHashSet<String> flags) {
9+
public CodegenPatternInfo(CodegenText pattern, LinkedHashSet<String> flags) {
1010
this.pattern = pattern;
1111
this.flags = flags;
1212
}

src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_pattern.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{#if forDocs}}
22
&nbsp;&nbsp;&nbsp;&nbsp;new KeywordEntry("pattern", new PatternValidator(<br>
33
{{~#with patternInfo}}
4-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"{{{pattern}}}"{{#if flags}},{{/if}}<br>
4+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"{{{pattern.codeEscaped}}}"{{#if flags}},{{/if}}<br>
55
{{~#if flags}}
66
{{#eq flags.size 1}}
77
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{#each flags}}Pattern.{{#eq this "i"}}CASE_INSENSITIVE{{/eq}}{{#eq this "m"}}MULTILINE{{/eq}}{{#eq this "s"}}DOTALL{{/eq}}{{#eq this "u"}}UNICODE_CHARACTER_CLASS{{/eq}}{{/each}}<br>
@@ -29,7 +29,7 @@
2929
{{~else}}
3030
new KeywordEntry("pattern", new PatternValidator(Pattern.compile(
3131
{{#with patternInfo}}
32-
"{{{pattern}}}"{{#if flags}},{{/if}}
32+
"{{{pattern.codeEscaped}}}"{{#if flags}},{{/if}}
3333
{{#if flags}}
3434
{{#eq flags.size 1}}
3535
{{#each flags}}Pattern.{{#eq this "i"}}CASE_INSENSITIVE{{/eq}}{{#eq this "m"}}MULTILINE{{/eq}}{{#eq this "s"}}DOTALL{{/eq}}{{#eq this "u"}}UNICODE_CHARACTER_CLASS{{/eq}}{{/each}}

src/main/resources/python/components/schemas/schema_cls/__pattern_info.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{#if propertyName}}{{propertyName}}: schemas.PatternInfo = {{/if}}schemas.PatternInfo(
22
{{#with patternInfo}}
3-
pattern=r'{{{pattern}}}'{{#if flags}},{{/if}} # noqa: E501
3+
pattern=r'{{{pattern.original}}}'{{#if flags}},{{/if}} # noqa: E501
44
{{#if flags}}
55
{{#eq flags.size 1}}
66
flags={{#each flags}}re.{{#eq this "i"}}I{{/eq}}{{#eq this "m"}}M{{/eq}}{{#eq this "s"}}S{{/eq}}{{#eq this "u"}}U{{/eq}}{{/each}},

0 commit comments

Comments
 (0)