From ffaf173db1ab2c1052ae704164fd378438e9cc47 Mon Sep 17 00:00:00 2001 From: Manon Grivot <14910727+zodiia@users.noreply.github.com> Date: Mon, 5 Dec 2022 18:44:16 +0100 Subject: [PATCH] Fixed typescript codegen pattern compiler (#14180) Co-authored-by: Manon Grivot --- .../languages/TypeScriptClientCodegen.java | 36 ++----------------- .../TypeScriptClientCodegenTest.java | 17 +++++++++ 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java index 93ef3994ec42..2e4f34c64659 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java @@ -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(); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/TypeScriptClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/TypeScriptClientCodegenTest.java index 925a6adcbc56..0e352a754726 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/TypeScriptClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/TypeScriptClientCodegenTest.java @@ -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."); + } + } }