diff --git a/docs/templating.md b/docs/templating.md index 3a4988cf740..eab039bb222 100644 --- a/docs/templating.md +++ b/docs/templating.md @@ -822,6 +822,7 @@ Many generators (*those extending DefaultCodegen*) come with a small set of lamb - `uppercase` - Converts all of the characters in this fragment to upper case using the rules of the `ROOT` locale. - `titlecase` - Converts text in a fragment to title case. For example `once upon a time` to `Once Upon A Time`. - `camelcase` - Converts text in a fragment to camelCase. For example `Input-text` to `inputText`. +- `uncamelize` - Converts text in a fragment from camelCase or PascalCase to a string of words separated by whitespaces. For example `inputText` to `Input Text`. - `indented` - Prepends 4 spaces indention from second line of a fragment on. First line will be indented by Mustache. - `indented_8` - Prepends 8 spaces indention from second line of a fragment on. First line will be indented by Mustache. - `indented_12` - Prepends 12 spaces indention from second line of a fragment on. First line will be indented by Mustache. 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 e8fd8c44b00..417bb97fb2c 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 @@ -465,6 +465,7 @@ public class DefaultCodegen implements CodegenConfig { .put("kebabcase", new KebabCaseLambda()) .put("camelcase", new CamelCaseLambda(true).generator(this)) .put("pascalcase", new CamelCaseLambda(false).generator(this)) + .put("uncamelize", new UncamelizeLambda()) .put("forwardslash", new ForwardSlashLambda()) .put("backslash", new BackSlashLambda()) .put("doublequote", new DoubleQuoteLambda()) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/mustache/UncamelizeLambda.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/mustache/UncamelizeLambda.java new file mode 100644 index 00000000000..ee75c3f50fb --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/mustache/UncamelizeLambda.java @@ -0,0 +1,53 @@ + +/* + * 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 org.apache.commons.lang3.StringUtils; + +import java.io.IOException; +import java.io.Writer; + +import static org.openapitools.codegen.utils.StringUtils.camelize; + +/** + * Converts text in a fragment from camelCase or PascalCase to a space separated string + * + * Register: + *
+ * additionalProperties.put("uncamelize", new UncamelizeLambda());
+ * 
+ * + * Use: + *
+ * {{#uncamelize}}{{name}}{{/uncamelize}}
+ * 
+ */ +public class UncamelizeLambda implements Mustache.Lambda { + + public UncamelizeLambda() {} + @Override + public void execute(Template.Fragment fragment, Writer writer) throws IOException { + String input = fragment.execute(); + String text = StringUtils.capitalize(StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(input.trim()), StringUtils.SPACE)); + writer.write(text.trim().replaceAll(" +", " ")); + } +} + diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/UncamelizeLambdaTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/UncamelizeLambdaTest.java new file mode 100644 index 00000000000..7d590e319b0 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/UncamelizeLambdaTest.java @@ -0,0 +1,50 @@ +package org.openapitools.codegen.templating.mustache; +import org.mockito.MockitoAnnotations; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Map; + +public class UncamelizeLambdaTest extends LambdaTest { + @BeforeMethod + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void camelCaseTest() { + // Given + Map ctx = context("uncamelize", new UncamelizeLambda()); + + // When & Then + test("Input Text", "{{#uncamelize}}inputText{{/uncamelize}}", ctx); + } + + @Test + public void pascalCaseTest() { + // Given + Map ctx = context("uncamelize", new UncamelizeLambda()); + + // When & Then + test("Input Text", "{{#uncamelize}}InputText{{/uncamelize}}", ctx); + } + + + @Test + public void emptyStringTest() { + // Given + Map ctx = context("uncamelize", new UncamelizeLambda()); + + // When & Then + test("", "{{#uncamelize}}{{/uncamelize}}", ctx); + } + + @Test + public void nonCamelCaseStringTest() { + // Given + Map ctx = context("uncamelize", new UncamelizeLambda()); + + // When & Then + test("Input Text", "{{#uncamelize}}Input Text{{/uncamelize}}", ctx); + } +} \ No newline at end of file