forked from loafle/openapi-generator-original
Fixed typescript codegen pattern compiler (#14180)
Co-authored-by: Manon Grivot <manon.grivot@lyra-network.com>
This commit is contained in:
@@ -1144,43 +1144,11 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
return fullPrefix + example + closeChars;
|
||||
} else if (StringUtils.isNotBlank(schema.getPattern())) {
|
||||
String pattern = schema.getPattern();
|
||||
/*
|
||||
RxGen does not support our ECMA dialect https://github.com/curious-odd-man/RgxGen/issues/56
|
||||
So strip off the leading / and trailing / and turn on ignore case if we have it
|
||||
*/
|
||||
Pattern valueExtractor = Pattern.compile("^/?(.+?)/?(.?)$");
|
||||
Matcher m = valueExtractor.matcher(pattern);
|
||||
RgxGen rgxGen = null;
|
||||
if (m.find()) {
|
||||
int groupCount = m.groupCount();
|
||||
if (groupCount == 1) {
|
||||
// only pattern found
|
||||
String isolatedPattern = m.group(1);
|
||||
rgxGen = new RgxGen(isolatedPattern);
|
||||
} else if (groupCount == 2) {
|
||||
// patterns and flag found
|
||||
String isolatedPattern = m.group(1);
|
||||
String flags = m.group(2);
|
||||
if (flags.contains("i")) {
|
||||
rgxGen = new RgxGen(isolatedPattern);
|
||||
RgxGenProperties properties = new RgxGenProperties();
|
||||
RgxGenOption.CASE_INSENSITIVE.setInProperties(properties, true);
|
||||
rgxGen.setProperties(properties);
|
||||
} else {
|
||||
rgxGen = new RgxGen(isolatedPattern);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
rgxGen = new RgxGen(pattern);
|
||||
}
|
||||
RgxGen rgxGen = new RgxGen(pattern);
|
||||
|
||||
// this seed makes it so if we have [a-z] we pick a
|
||||
Random random = new Random(18);
|
||||
if (rgxGen != null){
|
||||
example = rgxGen.generate(random);
|
||||
} else {
|
||||
throw new RuntimeException("rgxGen cannot be null. Please open an issue in the openapi-generator github repo.");
|
||||
}
|
||||
example = rgxGen.generate(random);
|
||||
} else if (schema.getMinLength() != null) {
|
||||
example = "";
|
||||
int len = schema.getMinLength().intValue();
|
||||
|
||||
@@ -141,4 +141,21 @@ public class TypeScriptClientCodegenTest {
|
||||
Assert.assertEquals(tsImports.get(0).get("filename"), mappedName);
|
||||
Assert.assertEquals(tsImports.get(0).get("classname"), "ApiResponse");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompilePattern() {
|
||||
final DefaultCodegen codegen = new TypeScriptClientCodegen();
|
||||
final StringSchema prop = new StringSchema();
|
||||
prop.setPattern("[A-Z]{3}");
|
||||
final Schema root = new ObjectSchema().addProperty("stringPattern", prop);
|
||||
final OpenAPI openApi = TestUtils.createOpenAPIWithOneSchema("sample", root);
|
||||
codegen.setOpenAPI(openApi);
|
||||
|
||||
try {
|
||||
final CodegenModel model = codegen.fromModel("sample", root);
|
||||
Assert.assertEquals(model.getAllVars().get(0).getPattern(), "/[A-Z]{3}/");
|
||||
} catch (Exception ex) {
|
||||
Assert.fail("Exception was thrown.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user