Skip to content

Commit 637ea1a

Browse files
newtorka-d
andauthored
feat: OpenAPI Generator to drop the redundant isIsBoolean() method prefix (#735)
Co-authored-by: Alexander Dümont <[email protected]>
1 parent 0e62cad commit 637ea1a

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

datamodel/openapi/openapi-generator/src/main/java/com/sap/cloud/sdk/datamodel/openapi/generator/GenerationConfigurationConverter.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sap.cloud.sdk.datamodel.openapi.generator;
22

3+
import static com.sap.cloud.sdk.datamodel.openapi.generator.GeneratorCustomProperties.FIX_REDUNDANT_IS_BOOLEAN_PREFIX;
34
import static com.sap.cloud.sdk.datamodel.openapi.generator.GeneratorCustomProperties.USE_ONE_OF_CREATORS;
45

56
import java.nio.file.Path;
@@ -10,8 +11,10 @@
1011
import java.util.List;
1112
import java.util.Map;
1213
import java.util.Set;
14+
import java.util.regex.Pattern;
1315

1416
import javax.annotation.Nonnull;
17+
import javax.annotation.Nullable;
1518

1619
import org.openapitools.codegen.ClientOptInput;
1720
import org.openapitools.codegen.CodegenConstants;
@@ -78,8 +81,20 @@ static ClientOptInput convertGenerationConfiguration(
7881
private static JavaClientCodegen createCodegenConfig( @Nonnull final GenerationConfiguration config )
7982
{
8083
final var primitives = Set.of("String", "Integer", "Long", "Double", "Float", "Byte");
84+
final var doubleIs = Pattern.compile("^isIs[A-Z]").asPredicate();
8185
return new JavaClientCodegen()
8286
{
87+
@Override
88+
@Nullable
89+
public String toBooleanGetter( @Nullable final String name )
90+
{
91+
final String result = super.toBooleanGetter(name);
92+
if( FIX_REDUNDANT_IS_BOOLEAN_PREFIX.isEnabled(config) && result != null && doubleIs.test(result) ) {
93+
return "is" + result.substring(4);
94+
}
95+
return result;
96+
}
97+
8398
// Custom processor to inject "x-return-nullable" extension
8499
@Override
85100
@Nonnull

datamodel/openapi/openapi-generator/src/main/java/com/sap/cloud/sdk/datamodel/openapi/generator/GeneratorCustomProperties.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ enum GeneratorCustomProperties
1515
/**
1616
* Use JsonCreator instead of sub-type deduction for oneOf and anyOf schemas.
1717
*/
18-
USE_ONE_OF_CREATORS("useOneOfCreators", "false");
18+
USE_ONE_OF_CREATORS("useOneOfCreators", "false"),
19+
20+
/**
21+
* Fix isIsBoolean() to isBoolean() for fields specified as `"isBoolean":{"type":"boolean"}`.
22+
*/
23+
FIX_REDUNDANT_IS_BOOLEAN_PREFIX("fixRedundantIsBooleanPrefix", "false");
1924

2025
private final String key;
2126
private final String defaultValue;

datamodel/openapi/openapi-generator/src/test/java/com/sap/cloud/sdk/datamodel/openapi/generator/DataModelGeneratorIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private enum TestCase
3434
true,
3535
true,
3636
6,
37-
Map.of("aiSdkConstructor", "true")),
37+
Map.of("aiSdkConstructor", "true", "fixRedundantIsBooleanPrefix", "true")),
3838
API_CLASS_VENDOR_EXTENSION_YAML(
3939
"api-class-vendor-extension-yaml",
4040
"sodastore.yaml",

datamodel/openapi/openapi-generator/src/test/resources/DataModelGeneratorIntegrationTest/api-class-for-ai-sdk/input/sodastore.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@
169169
"brand": {
170170
"type": "string"
171171
},
172+
"isAvailable": {
173+
"type": "boolean"
174+
},
172175
"flavor": {
173176
"type": "string"
174177
},

datamodel/openapi/openapi-generator/src/test/resources/DataModelGeneratorIntegrationTest/api-class-for-ai-sdk/output/com/sap/cloud/sdk/services/builder/model/Soda.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public class Soda
5050
@JsonProperty("brand")
5151
private String brand;
5252

53+
@JsonProperty("isAvailable")
54+
private Boolean isAvailable;
55+
5356
@JsonProperty("flavor")
5457
private String flavor;
5558

@@ -147,6 +150,35 @@ public void setBrand( @Nonnull final String brand) {
147150
this.brand = brand;
148151
}
149152

153+
/**
154+
* Set the isAvailable of this {@link Soda} instance and return the same instance.
155+
*
156+
* @param isAvailable The isAvailable of this {@link Soda}
157+
* @return The same instance of this {@link Soda} class
158+
*/
159+
@Nonnull public Soda isAvailable( @Nullable final Boolean isAvailable) {
160+
this.isAvailable = isAvailable;
161+
return this;
162+
}
163+
164+
/**
165+
* Get isAvailable
166+
* @return isAvailable The isAvailable of this {@link Soda} instance.
167+
*/
168+
@Nonnull
169+
public Boolean isAvailable() {
170+
return isAvailable;
171+
}
172+
173+
/**
174+
* Set the isAvailable of this {@link Soda} instance.
175+
*
176+
* @param isAvailable The isAvailable of this {@link Soda}
177+
*/
178+
public void setIsAvailable( @Nullable final Boolean isAvailable) {
179+
this.isAvailable = isAvailable;
180+
}
181+
150182
/**
151183
* Set the flavor of this {@link Soda} instance and return the same instance.
152184
*
@@ -244,6 +276,7 @@ public Map<String, Object> toMap()
244276
if( id != null ) declaredFields.put("id", id);
245277
if( name != null ) declaredFields.put("name", name);
246278
if( brand != null ) declaredFields.put("brand", brand);
279+
if( isAvailable != null ) declaredFields.put("isAvailable", isAvailable);
247280
if( flavor != null ) declaredFields.put("flavor", flavor);
248281
if( price != null ) declaredFields.put("price", price);
249282
return declaredFields;
@@ -275,13 +308,14 @@ public boolean equals(@Nullable final java.lang.Object o) {
275308
Objects.equals(this.id, soda.id) &&
276309
Objects.equals(this.name, soda.name) &&
277310
Objects.equals(this.brand, soda.brand) &&
311+
Objects.equals(this.isAvailable, soda.isAvailable) &&
278312
Objects.equals(this.flavor, soda.flavor) &&
279313
Objects.equals(this.price, soda.price);
280314
}
281315

282316
@Override
283317
public int hashCode() {
284-
return Objects.hash(id, name, brand, flavor, price, cloudSdkCustomFields);
318+
return Objects.hash(id, name, brand, isAvailable, flavor, price, cloudSdkCustomFields);
285319
}
286320

287321
@Override
@@ -291,6 +325,7 @@ public int hashCode() {
291325
sb.append(" id: ").append(toIndentedString(id)).append("\n");
292326
sb.append(" name: ").append(toIndentedString(name)).append("\n");
293327
sb.append(" brand: ").append(toIndentedString(brand)).append("\n");
328+
sb.append(" isAvailable: ").append(toIndentedString(isAvailable)).append("\n");
294329
sb.append(" flavor: ").append(toIndentedString(flavor)).append("\n");
295330
sb.append(" price: ").append(toIndentedString(price)).append("\n");
296331
cloudSdkCustomFields.forEach((k,v) -> sb.append(" ").append(k).append(": ").append(toIndentedString(v)).append("\n"));

0 commit comments

Comments
 (0)