Skip to content

Commit f318aab

Browse files
authored
Merge pull request #1129 from Backbase/feature/add-support-generator-params-nameMappings-enumNameMappings
NOJIRA: Add additional openapi-generator parameter
2 parents 428efb2 + e1819eb commit f318aab

File tree

9 files changed

+138
-7
lines changed

9 files changed

+138
-7
lines changed

.github/workflows/release-drafter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ jobs:
1818
needs: correct_repository
1919
runs-on: ubuntu-latest
2020
steps:
21-
- uses: release-drafter/release-drafter@v6
21+
- uses: release-drafter/release-drafter@b1476f6e6eb133afa41ed8589daba6dc69b4d3f5
2222
env:
2323
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ jobs:
119119
git commit -m "[version bump] new dev version" -a
120120
121121
- name: Push Development Version to Branch
122-
uses: ad-m/github-push-action@master
122+
uses: ad-m/github-push-action@77c5b412c50b723d2a4fbc6d71fb5723bcd439aa
123123
with:
124124
github_token: ${{ secrets.GITHUB_TOKEN }}
125125
branch: ${{github.event.release.target_commitish || 'main'}}

.github/workflows/secrets.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ jobs:
88
steps:
99
- uses: actions/checkout@v5
1010
- name: wget
11-
uses: wei/wget@v1
11+
uses: wei/wget@c15e476d1463f4936cb54f882170d9d631f1aba5
1212
with:
1313
args: -O .rules.toml https://raw.githubusercontent.com/fnxpt/gitleaks-action/rules/.rules.toml
1414
- name: gitleaks-action
15-
uses: zricethezav/gitleaks-action@master
15+
uses: gitleaks/gitleaks-action@bf2dc8e55639c1e091e9b45970152e4313705814
1616
with:
1717
config-path: .rules.toml

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ It currently consists of
1515

1616
# Release Notes
1717
BOAT is still under development and subject to change.
18+
19+
## 0.17.63
20+
Add support params of openapi-generator `name-mappings` and `enum-name-mappings`.
21+
Check `Property & Enum Name Mappings (new)` in [boat-maven-plugin](boat-maven-plugin/README.md)
22+
1823
## 0.17.62
1924
* Fixed reactive generation of code to not include extra Flux<> to return parameter
2025
## 0.17.61

boat-maven-plugin/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,60 @@ Same with `generate` but with opinionated defaults for Rest Template Client
9696
</additionalProperties>
9797
</configuration>
9898

99+
### Property & Enum Name Mappings (new)
100+
101+
Two new optional parameters are supported by the BOAT plugin (mirroring OpenAPI Generator capabilities) to rename generated members without post-processing:
102+
103+
nameMappings A map of property names to their new names.
104+
enumNameMappings A map of enum (symbol) names to their new names.
105+
106+
You can configure them directly as top-level plugin parameters OR via `configOptions` using the underlying generator keys `name-mappings` and `enum-name-mappings`.
107+
108+
Supported input formats:
109+
1. Top-level list form (preferred for clarity):
110+
111+
<configuration>
112+
...
113+
<nameMappings>
114+
<nameMapping>old_property_name=NewPropertyName</nameMapping>
115+
<nameMapping>id=identifier</nameMapping>
116+
</nameMappings>
117+
<enumNameMappings>
118+
<enumNameMapping>old_enum_name=NewEnumName</enumNameMapping>
119+
<enumNameMapping>Status=ApiStatus</enumNameMapping>
120+
</enumNameMappings>
121+
</configuration>
122+
123+
2. Via system properties / CLI (helpful for experimentation):
124+
125+
mvn clean compile \
126+
-Dopenapi.generator.maven.plugin.nameMappings=old_property_name=NewPropertyName,id=identifier \
127+
-Dopenapi.generator.maven.plugin.enumNameMappings=old_enum_name=NewEnumName,Status=ApiStatus
128+
129+
Rules & notes:
130+
- Key/value pairs use '='. Whitespace around keys/values is trimmed.
131+
- For multiple entries in system property form, separate by comma.
132+
- Duplicate keys: last one wins.
133+
- These mappings apply after any model / type renames from other mapping options.
134+
- `nameMappings` affects property (field) identifiers in generated models & parameter names (where supported by the underlying generator).
135+
- `enumNameMappings` affects enum constant identifiers (not their serialized wire values). To change wire values, adjust the OpenAPI schema itself.
136+
137+
Example combining with other options:
138+
139+
<plugin>
140+
<groupId>com.backbase.oss</groupId>
141+
<artifactId>boat-maven-plugin</artifactId>
142+
<configuration>
143+
<nameMappings>
144+
<nameMapping>created_at=createdAt</nameMapping>
145+
<nameMapping>updated_at=updatedAt</nameMapping>
146+
</nameMappings>
147+
<enumNameMappings>
148+
<enumNameMapping>status=ResourceStatus</enumNameMapping>
149+
</enumNameMappings>
150+
</configuration>
151+
</plugin>
152+
99153
## boat:generate-webclient-embedded
100154

101155
Same with `generate` but with opinionated defaults for Web Client

boat-maven-plugin/src/main/java/com/backbase/oss/boat/GenerateMojo.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
88
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyAdditionalPropertiesKvp;
99
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyAdditionalPropertiesKvpList;
10+
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyEnumNameMappingsKvpList;
1011
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyImportMappingsKvp;
1112
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyImportMappingsKvpList;
1213
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyInstantiationTypesKvp;
1314
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyInstantiationTypesKvpList;
1415
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyLanguageSpecificPrimitivesCsv;
1516
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyLanguageSpecificPrimitivesCsvList;
17+
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyNameMappingsKvpList;
1618
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyOpenAPINormalizerKvpList;
1719
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyReservedWordsMappingsKvp;
1820
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyReservedWordsMappingsKvpList;
@@ -492,15 +494,36 @@ static String uniqueJoin(Collection<String> values) {
492494
private List<String> openapiNormalizer;
493495

494496
/**
495-
* A map of scheme and the new one
497+
* A map of scheme and the new one.
496498
*/
497499
@Parameter(name = "schemaMappings", property = "openapi.generator.maven.plugin.schemaMappings")
498500
private List<String> schemaMappings;
499501

502+
/**
503+
* A map of property names and the new names.
504+
*/
505+
@Parameter(name = "nameMappings", property = "openapi.generator.maven.plugin.nameMappings")
506+
private List<String> nameMappings;
507+
508+
/**
509+
* A map of enum names and the new names.
510+
*/
511+
@Parameter(name = "enumNameMappings", property = "openapi.generator.maven.plugin.enumNameMappings")
512+
private List<String> enumNameMappings;
513+
514+
500515
public void setBuildContext(BuildContext buildContext) {
501516
this.buildContext = buildContext;
502517
}
503518

519+
public void setNameMappings(List<String> nameMappings) {
520+
this.nameMappings = nameMappings;
521+
}
522+
523+
public void setEnumNameMappings(List<String> enumNameMappings) {
524+
this.enumNameMappings = enumNameMappings;
525+
}
526+
504527
@Override
505528
@SuppressWarnings({"java:S3776", "java:S1874"})
506529
public void execute() throws MojoExecutionException, MojoFailureException {
@@ -865,6 +888,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
865888
applySchemaMappingsKvpList(schemaMappings, configurator);
866889
}
867890

891+
// Apply Name Mappings
892+
if (nameMappings != null && (configOptions == null || !configOptions.containsKey("name-mappings"))) {
893+
applyNameMappingsKvpList(nameMappings, configurator);
894+
}
895+
896+
// Apply Enum Name Mappings
897+
if (enumNameMappings != null && (configOptions == null || !configOptions.containsKey("enum-name-mappings"))) {
898+
applyEnumNameMappingsKvpList(enumNameMappings, configurator);
899+
}
900+
868901
if (environmentVariables != null) {
869902

870903
for (Entry<String, String> entry : environmentVariables.entrySet()) {

boat-maven-plugin/src/test/java/com/backbase/oss/boat/GenerateMojoTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.backbase.oss.boat;
22

3+
import static java.util.Collections.singletonList;
34
import static java.util.Collections.singletonMap;
5+
import static org.hamcrest.CoreMatchers.is;
46
import static org.hamcrest.MatcherAssert.assertThat;
57
import static org.hamcrest.Matchers.containsInAnyOrder;
68
import static org.hamcrest.Matchers.endsWith;
79
import static org.hamcrest.Matchers.equalTo;
810
import static org.hamcrest.Matchers.hasSize;
11+
import static org.junit.jupiter.api.Assertions.assertAll;
912
import static org.junit.jupiter.api.Assertions.assertEquals;
1013
import static org.junit.jupiter.api.Assertions.assertNotNull;
1114

@@ -14,6 +17,7 @@
1417
import java.io.File;
1518
import java.util.HashMap;
1619
import java.util.Map;
20+
import java.util.stream.Stream;
1721
import org.apache.maven.plugin.MojoExecutionException;
1822
import org.apache.maven.plugin.MojoFailureException;
1923
import org.apache.maven.project.MavenProject;
@@ -55,8 +59,14 @@ void addTestCompileSourceRoot() throws MojoExecutionException, MojoFailureExcept
5559
void useJavaBoat() throws MojoExecutionException, MojoFailureException {
5660
GenerateMojo mojo = configure(new GenerateMojo(), "java");
5761

62+
mojo.setEnumNameMappings(singletonList("LARGE=SUPER-BIG"));
63+
mojo.setNameMappings(singletonList("size=petSize"));
64+
5865
mojo.execute();
5966

67+
assertGeneratedClientModels(mojo.output, "Pet.java", "petSize");
68+
assertGeneratedClientModels(mojo.output, "Size.java", "SUPER-BIG");
69+
6070
assertThat(mojo.generatorName, equalTo(BoatJavaCodeGen.NAME));
6171
}
6272

@@ -187,4 +197,25 @@ private <T extends GenerateMojo> T configure(T mojo, String generatorName) {
187197
mojo.generatorName = generatorName;
188198
return mojo;
189199
}
200+
201+
private void assertGeneratedClientModels(File mojoOutput, String fileName, String... expectedProperties) {
202+
File dir = mojoOutput.getAbsoluteFile()
203+
.toPath()
204+
.resolve("src/main/java/org/openapitools/client/model")
205+
.toFile();
206+
File target = new File(dir, fileName);
207+
assertThat(target.exists(), is(true));
208+
try {
209+
String content = java.nio.file.Files.readString(
210+
target.toPath(),
211+
java.nio.charset.StandardCharsets.UTF_8
212+
);
213+
assertAll(Stream.of(expectedProperties).map(prop ->
214+
() -> assertThat("Expected content not found: " + prop, content.contains(prop), is(true))
215+
));
216+
} catch (Exception e) {
217+
throw new RuntimeException(e);
218+
}
219+
}
220+
190221
}

boat-maven-plugin/src/test/resources/oas-examples/petstore.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,19 @@ components:
114114
type: string
115115
tag:
116116
type: string
117+
size:
118+
$ref: '#/components/schemas/Size'
117119
Pets:
118120
type: array
119121
items:
120122
$ref: "#/components/schemas/Pet"
123+
Size:
124+
type: string
125+
description: Size of the pet
126+
enum:
127+
- SMALL
128+
- MEDIUM
129+
- LARGE
121130
Error:
122131
type: object
123132
required:

boat-scaffold/src/test/java/com/backbase/oss/codegen/java/MavenProjectCompiler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ public int compile(File projectDir) {
5353
log.debug("Compiling mvn project in: {}", projectDir);
5454
var mavenCli = new MavenCli(new ClassWorld("myRealm", contextClassLoader));
5555
final String initialDir = System.getProperty(MavenCli.MULTIMODULE_PROJECT_DIRECTORY);
56-
try {
56+
try(PrintStream out = new PrintStream(new File(projectDir, "mvn.log"))) {
5757
System.setProperty(MavenCli.MULTIMODULE_PROJECT_DIRECTORY, projectDir.getAbsolutePath());
5858
String[] args = generateMavenCliArgs();
5959
log.debug("mvn cli args: {}", Arrays.toString(args));
6060
log.info("Building: {}", projectDir.getName());
61-
PrintStream out = new PrintStream(new File(projectDir, "mvn.log"));
6261
int compilationStatus = mavenCli.doMain(args, projectDir.getAbsolutePath(), out, out);
6362
log.debug("compilation status={}", compilationStatus);
6463
return compilationStatus;

0 commit comments

Comments
 (0)