From 334593fe8f52802d72e9035a8809dabe0e48cfd6 Mon Sep 17 00:00:00 2001 From: themooks Date: Thu, 3 Dec 2026 10:35:15 +0100 Subject: [PATCH] Add lambda to remove trailing comma from string --- .../openapitools/codegen/DefaultCodegen.java | 1 + .../mustache/RemoveEndCommaLambda.java | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/mustache/RemoveEndCommaLambda.java diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 3763d93b1e5a..7eb8bb4517ea 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -379,6 +379,7 @@ protected ImmutableMap.Builder addMustacheLambdas() { .put("titlecase", new TitlecaseLambda()) .put("snakecase", new SnakecaseLambda()) .put("gocase", new GocaseLambda()) + .put("removeendcomma", new RemoveEndCommaLambda()) .put("camelcase", new CamelCaseLambda(true).generator(this)) .put("pascalcase", new CamelCaseLambda(false).generator(this)) .put("indented", new IndentedLambda()) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/mustache/RemoveEndCommaLambda.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/mustache/RemoveEndCommaLambda.java new file mode 100644 index 000000000000..5c6f197ffcf9 --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/mustache/RemoveEndCommaLambda.java @@ -0,0 +1,54 @@ +/* + * Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) + * Copyright 2018 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openapitools.codegen.templating.mustache; + +import com.samskivert.mustache.Mustache; +import com.samskivert.mustache.Template; + +import java.io.IOException; +import java.io.Writer; + +/** + * Remove ending comma from text if present + *

+ * Register: + *

+ * additionalProperties.put("removeendcomma", new RemoveEndCommaLambda());
+ * 
+ *

+ * Use: + *

+ * {{#removeendcomma}}{{summary}}{{/removeendcomma}}
+ * 
+ */ +public class RemoveEndCommaLambda implements Mustache.Lambda { + @Override + public void execute(Template.Fragment fragment, Writer writer) throws IOException { + String text = fragment.execute(); + if (text.endsWith(",")) { + text = text.substring(0, text.length() - 1); + } else if (text.endsWith(", ")) { + text = text.substring(0, text.length() - 2); + } else if (text.endsWith(",\n")) { + text = text.substring(0, text.length() - 2) + "\n"; + } else if (text.endsWith(", \n")) { + text = text.substring(0, text.length() - 3) + "\n"; + } + writer.write(text); + } +}