From f455ac166c4ecc8ae7fce0005beacd9640f56acf Mon Sep 17 00:00:00 2001 From: Anton Averchenkov <84287187+averche@users.noreply.github.com> Date: Tue, 14 Feb 2023 22:14:59 -0500 Subject: [PATCH] Add 'endsWith' handlebars helper function (#14634) * Add 'endsWith' handlebars helper function * add tests * fix test --- .../templating/handlebars/StringHelpers.java | 58 ++++++++++++++++++- .../handlebars/StringHelpersTest.java | 51 +++++++++++++++- 2 files changed, 105 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/handlebars/StringHelpers.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/handlebars/StringHelpers.java index c8a8170ca96f..bd7c412244bc 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/handlebars/StringHelpers.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/handlebars/StringHelpers.java @@ -10,7 +10,7 @@ import java.util.Locale; public enum StringHelpers implements Helper { /** - * Indicates the string starts with the defined value. + * Indicates whether the string starts with the given value. * For example: * *
@@ -39,8 +39,6 @@ public enum StringHelpers implements Helper {
      * 
      *   {{startsWith a b yes='y' no='n'}}
      * 
- * - * If value is "handlebars.java", the output will be "Handlebars.java". */ startsWith { @Override @@ -57,6 +55,60 @@ public enum StringHelpers implements Helper { return result ? options.fn() : options.inverse(); } + return result + ? options.hash("yes", true) + : options.hash("no", false); + } + }, + + /** + * Indicates whether the string ends with the given value. + * For example: + * + *
+     * {{endsWith a b ["insensitive"]}}
+     * 
+ * + *
+     * {{endsWith a text='b' [insensitive=true]}}
+     * 
+ * + * Render 'yes' or 'no': + *
+     *   {{#endsWith a b}}
+     *     yes
+     *   {{else}}
+     *     no
+     *   {{/endsWith}}
+     * 
+ * + * Render 'true' or 'false': + *
+     *   {{endsWith a b}}
+     * 
+ * + * Render 'y' or 'n': + *
+     *   {{endsWith a b yes='y' no='n'}}
+     * 
+ */ + endsWith { + @Override + public Object apply(Object value, Options options) throws IOException { + String match = options.param(0, options.hash("text", "")); + if (match.length() < 1) { + return false; + } + + boolean caseInsensitive = options.hash("insensitive", false); + boolean result = caseInsensitive + ? value.toString().toLowerCase(Locale.ROOT).endsWith(match.toLowerCase(Locale.ROOT)) + : value.toString().endsWith(match); + + if (options.tagType == TagType.SECTION) { + return result ? options.fn() : options.inverse(); + } + return result ? options.hash("yes", true) : options.hash("no", false); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/handlebars/StringHelpersTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/handlebars/StringHelpersTest.java index 5bea5678bbba..d3594029159f 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/handlebars/StringHelpersTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/handlebars/StringHelpersTest.java @@ -83,4 +83,53 @@ public class StringHelpersTest { template = "{{startsWith asdf b yes='y' no='n'}}"; evaluate(data, template, "n"); } -} \ No newline at end of file + + @Test(description = "Handlebars StringHelpers.endsWith, section") + public void endsWithSectionalTest() throws IOException { + HashMap data = new HashMap() {{ + put("asdf", "asdf"); + put("df", "df"); + put("b", "b"); + }}; + + String template = "{{~#endsWith asdf df ~}}yes{{~else~}}no{{~/endsWith~}}"; + evaluate(data, template, "yes"); + + template = "{{~#endsWith asdf b ~}}yes{{~else~}}no{{~/endsWith~}}"; + evaluate(data, template, "no"); + } + + @Test(description = "Handlebars StringHelpers.endsWith") + public void endsWithTest() throws IOException { + HashMap data = new HashMap() {{ + put("asdf", "asdf"); + put("ASDF", "ASDF"); + put("f", "f"); + put("b", "b"); + }}; + evaluate(data, "{{endsWith asdf f}}", "true"); + evaluate(data, "{{endsWith asdf b}}", "false"); + evaluate(data, "{{endsWith ASDF f insensitive=true }}", "true"); + evaluate(data, "{{endsWith ASDF f insensitive=false }}", "false"); + evaluate(data, "{{endsWith ASDF 'f' insensitive=true }}", "true"); + evaluate(data, "{{endsWith ASDF b insensitive=true }}", "false"); + evaluate(data, "{{endsWith ASDF 'b' insensitive=true }}", "false"); + evaluate(data, "{{endsWith ASDF insensitive=true text='f'}}", "true"); + evaluate(data, "{{endsWith ASDF insensitive=true text='f' yes='✓' no='✘'}}", "✓"); + evaluate(data, "{{endsWith ASDF insensitive=false text='f' yes='✓' no='✘'}}", "✘"); + } + + @Test(description = "Handlebars StringHelpers.endsWith, yes/no override") + public void endsWithYesOverrideTest() throws IOException { + HashMap data = new HashMap() {{ + put("asdf", "asdf"); + put("f", "f"); + put("b", "b"); + }}; + String template = "{{endsWith asdf f yes='y' no='n'}}"; + evaluate(data, template, "y"); + + template = "{{endsWith asdf b yes='y' no='n'}}"; + evaluate(data, template, "n"); + } +}