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

Commit b0f69bc

Browse files
committed
Adds pattern example generation
1 parent 5deb73a commit b0f69bc

File tree

7 files changed

+45
-17
lines changed

7 files changed

+45
-17
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-
"a"
48+
"\ziEEpmVvrKlTttzGFqCEG"
4949
),
5050
new AbstractMap.SimpleEntry<>(
5151
"origin",
52-
"a"
52+
"\ziEEpmVvrKlTttzGFqCEG"
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-
"a",
117+
"\ziEEpmVvrKlTttzGFqCEG",
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-
"a",
155+
"\ziEEpmVvrKlTttzGFqCEG",
156156
configuration
157157
);
158158
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSch
6666
6767
// String validation
6868
String validatedPayload = ComposedOneOfDifferentTypes.Schema6.validate(
69-
"1970-01-01T00:00:00.00Z",
69+
"2020jUR,rZ#UM/?R,Fp^l6$ARj",
7070
configuration
7171
);
7272
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSch
3232
3333
// String validation
3434
String validatedPayload = DateTimeWithValidations.DateTimeWithValidations1.validate(
35-
"1970-01-01T00:00:00.00Z",
35+
"2020jUR,rZ#UM/?R,Fp^l6$ARj",
3636
configuration
3737
);
3838
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSch
3232
3333
// String validation
3434
String validatedPayload = DateWithValidations.DateWithValidations1.validate(
35-
"2020-12-13",
35+
"2020jUR,rZ#UM/?R,Fp^l6$ARj",
3636
configuration
3737
);
3838
```

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ FormatTest.FormatTestMap validatedPayload =
114114
),
115115
new AbstractMap.SimpleEntry<>(
116116
"string",
117-
"a"
117+
"A"
118118
),
119119
new AbstractMap.SimpleEntry<>(
120120
"binary",
@@ -134,11 +134,11 @@ FormatTest.FormatTestMap validatedPayload =
134134
),
135135
new AbstractMap.SimpleEntry<>(
136136
"pattern_with_digits",
137-
"a"
137+
"\dddddddddd"
138138
),
139139
new AbstractMap.SimpleEntry<>(
140140
"pattern_with_digits_and_delimiter",
141-
"a"
141+
"IMage_\dd"
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-
"a",
254+
"IMage_\dd",
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-
"a",
295+
"\dddddddddd",
296296
configuration
297297
);
298298
```
@@ -420,7 +420,7 @@ static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSch
420420
421421
// String validation
422422
String validatedPayload = FormatTest.StringSchema.validate(
423-
"a",
423+
"A",
424424
configuration
425425
);
426426
```

samples/client/petstore/java/docs/paths/fake/post/requestbody/content/applicationxwwwformurlencoded/Schema.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Schema.SchemaMap validatedPayload =
6363
),
6464
new AbstractMap.SimpleEntry<>(
6565
"pattern_without_delimiter",
66-
"a"
66+
"AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>"
6767
),
6868
new AbstractMap.SimpleEntry<>(
6969
"integer",
@@ -83,7 +83,7 @@ Schema.SchemaMap validatedPayload =
8383
),
8484
new AbstractMap.SimpleEntry<>(
8585
"string",
86-
"a"
86+
"A"
8787
),
8888
new AbstractMap.SimpleEntry<>(
8989
"binary",
@@ -315,7 +315,7 @@ static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSch
315315
316316
// String validation
317317
String validatedPayload = Schema.PatternWithoutDelimiter.validate(
318-
"a",
318+
"AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>",
319319
configuration
320320
);
321321
```
@@ -356,7 +356,7 @@ static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSch
356356
357357
// String validation
358358
String validatedPayload = Schema.StringSchema.validate(
359-
"a",
359+
"A",
360360
configuration
361361
);
362362
```

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import com.github.benmanes.caffeine.cache.Cache;
2121
import com.github.benmanes.caffeine.cache.Caffeine;
2222
import com.github.benmanes.caffeine.cache.Ticker;
23+
import com.github.curiousoddman.rgxgen.RgxGen;
24+
import com.github.curiousoddman.rgxgen.config.RgxGenOption;
25+
import com.github.curiousoddman.rgxgen.config.RgxGenProperties;
2326
import com.google.common.collect.ImmutableMap;
2427
import com.samskivert.mustache.Mustache;
2528
import com.samskivert.mustache.Mustache.Compiler;
@@ -2312,6 +2315,28 @@ private Object getNumberFromSchema(CodegenSchema schema) {
23122315
}
23132316
}
23142317

2318+
private String getPatternExample(CodegenPatternInfo patternInfo) {
2319+
String extractedPattern = patternInfo.pattern;
2320+
LinkedHashSet<String> regexFlags = patternInfo.flags;
2321+
/*
2322+
RxGen does not support our ECMA dialect https://github.com/curious-odd-man/RgxGen/issues/56
2323+
So strip off the leading / and trailing / and turn on ignore case if we have it
2324+
*/
2325+
RgxGen rgxGen;
2326+
if (regexFlags != null && regexFlags.contains("i")) {
2327+
rgxGen = new RgxGen(extractedPattern);
2328+
RgxGenProperties properties = new RgxGenProperties();
2329+
RgxGenOption.CASE_INSENSITIVE.setInProperties(properties, true);
2330+
rgxGen.setProperties(properties);
2331+
} else {
2332+
rgxGen = new RgxGen(extractedPattern);
2333+
}
2334+
2335+
// this seed makes it so if we have [a-z] we pick a
2336+
Random random = new Random(18);
2337+
return rgxGen.generate(random);
2338+
}
2339+
23152340
private Object getStringFromSchema(CodegenSchema schema) {
23162341
if (schema.enumInfo != null && schema.enumInfo.typeToValues.containsKey("string")) {
23172342
for(EnumValue enumValue: schema.enumInfo.typeToValues.get("string")) {
@@ -2325,6 +2350,9 @@ private Object getStringFromSchema(CodegenSchema schema) {
23252350
}
23262351
}
23272352
// todo handle not const or not enum here
2353+
if (schema.patternInfo != null) {
2354+
return getPatternExample(schema.patternInfo);
2355+
}
23282356
if ("date".equals(schema.format)) {
23292357
return "2020-12-13";
23302358
} else if ("date-time".equals(schema.format)) {

0 commit comments

Comments
 (0)