Skip to content

Commit fbe81f0

Browse files
bdtkarlssonDaniel Karlsson
andauthored
Add new uncamelize lambda (#18109)
Co-authored-by: Daniel Karlsson <daniel.karlsson@cinnober.com>
1 parent 41dbe51 commit fbe81f0

4 files changed

Lines changed: 105 additions & 0 deletions

File tree

docs/templating.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ Many generators (*those extending DefaultCodegen*) come with a small set of lamb
822822
- `uppercase` - Converts all of the characters in this fragment to upper case using the rules of the `ROOT` locale.
823823
- `titlecase` - Converts text in a fragment to title case. For example `once upon a time` to `Once Upon A Time`.
824824
- `camelcase` - Converts text in a fragment to camelCase. For example `Input-text` to `inputText`.
825+
- `uncamelize` - Converts text in a fragment from camelCase or PascalCase to a string of words separated by whitespaces. For example `inputText` to `Input Text`.
825826
- `indented` - Prepends 4 spaces indention from second line of a fragment on. First line will be indented by Mustache.
826827
- `indented_8` - Prepends 8 spaces indention from second line of a fragment on. First line will be indented by Mustache.
827828
- `indented_12` - Prepends 12 spaces indention from second line of a fragment on. First line will be indented by Mustache.

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
465465
.put("kebabcase", new KebabCaseLambda())
466466
.put("camelcase", new CamelCaseLambda(true).generator(this))
467467
.put("pascalcase", new CamelCaseLambda(false).generator(this))
468+
.put("uncamelize", new UncamelizeLambda())
468469
.put("forwardslash", new ForwardSlashLambda())
469470
.put("backslash", new BackSlashLambda())
470471
.put("doublequote", new DoubleQuoteLambda())
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
/*
3+
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
4+
* Copyright 2018 SmartBear Software
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.openapitools.codegen.templating.mustache;
20+
21+
import com.samskivert.mustache.Mustache;
22+
import com.samskivert.mustache.Template;
23+
import org.apache.commons.lang3.StringUtils;
24+
25+
import java.io.IOException;
26+
import java.io.Writer;
27+
28+
import static org.openapitools.codegen.utils.StringUtils.camelize;
29+
30+
/**
31+
* Converts text in a fragment from camelCase or PascalCase to a space separated string
32+
*
33+
* Register:
34+
* <pre>
35+
* additionalProperties.put("uncamelize", new UncamelizeLambda());
36+
* </pre>
37+
*
38+
* Use:
39+
* <pre>
40+
* {{#uncamelize}}{{name}}{{/uncamelize}}
41+
* </pre>
42+
*/
43+
public class UncamelizeLambda implements Mustache.Lambda {
44+
45+
public UncamelizeLambda() {}
46+
@Override
47+
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
48+
String input = fragment.execute();
49+
String text = StringUtils.capitalize(StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(input.trim()), StringUtils.SPACE));
50+
writer.write(text.trim().replaceAll(" +", " "));
51+
}
52+
}
53+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.openapitools.codegen.templating.mustache;
2+
import org.mockito.MockitoAnnotations;
3+
import org.testng.annotations.BeforeMethod;
4+
import org.testng.annotations.Test;
5+
6+
import java.util.Map;
7+
8+
public class UncamelizeLambdaTest extends LambdaTest {
9+
@BeforeMethod
10+
public void setup() {
11+
MockitoAnnotations.initMocks(this);
12+
}
13+
14+
@Test
15+
public void camelCaseTest() {
16+
// Given
17+
Map<String, Object> ctx = context("uncamelize", new UncamelizeLambda());
18+
19+
// When & Then
20+
test("Input Text", "{{#uncamelize}}inputText{{/uncamelize}}", ctx);
21+
}
22+
23+
@Test
24+
public void pascalCaseTest() {
25+
// Given
26+
Map<String, Object> ctx = context("uncamelize", new UncamelizeLambda());
27+
28+
// When & Then
29+
test("Input Text", "{{#uncamelize}}InputText{{/uncamelize}}", ctx);
30+
}
31+
32+
33+
@Test
34+
public void emptyStringTest() {
35+
// Given
36+
Map<String, Object> ctx = context("uncamelize", new UncamelizeLambda());
37+
38+
// When & Then
39+
test("", "{{#uncamelize}}{{/uncamelize}}", ctx);
40+
}
41+
42+
@Test
43+
public void nonCamelCaseStringTest() {
44+
// Given
45+
Map<String, Object> ctx = context("uncamelize", new UncamelizeLambda());
46+
47+
// When & Then
48+
test("Input Text", "{{#uncamelize}}Input Text{{/uncamelize}}", ctx);
49+
}
50+
}

0 commit comments

Comments
 (0)