Skip to content

Commit 144c7fd

Browse files
newtorka-d
andauthored
fix: [OpenAPI] Correct @Nullable on inner enum fromValue(String) methods (#746)
Co-authored-by: Alexander Dümont <[email protected]>
1 parent 7561eca commit 144c7fd

File tree

3 files changed

+101
-2
lines changed
  • datamodel/openapi/openapi-generator/src

3 files changed

+101
-2
lines changed

datamodel/openapi/openapi-generator/src/main/resources/openapi-generator/mustache-templates/modelInnerEnum.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
{{#jackson}}
6666
@JsonCreator
6767
{{/jackson}}
68-
@Nonnull public static {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(@Nonnull final {{{dataType}}} value) {
68+
{{#isNullable}}@Nullable{{/isNullable}}{{^isNullable}}@Nonnull{{/isNullable}} public static {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(@Nonnull final {{{dataType}}} value) {
6969
for ({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
7070
if (b.value.{{^isString}}equals{{/isString}}{{#isString}}{{#useEnumCaseInsensitive}}equalsIgnoreCase{{/useEnumCaseInsensitive}}{{^useEnumCaseInsensitive}}equals{{/useEnumCaseInsensitive}}{{/isString}}(value)) {
7171
return b;

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

+5
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@
179179
"type": "number",
180180
"format": "float"
181181
},
182+
"diet": {
183+
"type": "string",
184+
"enum": ["sugar", "zero", "light"],
185+
"nullable": true
186+
},
182187
"embedding":{
183188
"type": "array",
184189
"items": {

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

+95-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,68 @@ public class Soda
6363
@JsonProperty("price")
6464
private Float price;
6565

66+
/**
67+
* Gets or Sets diet
68+
*/
69+
public enum DietEnum {
70+
/**
71+
* The SUGAR option of this Soda
72+
*/
73+
SUGAR("sugar"),
74+
75+
/**
76+
* The ZERO option of this Soda
77+
*/
78+
ZERO("zero"),
79+
80+
/**
81+
* The LIGHT option of this Soda
82+
*/
83+
LIGHT("light");
84+
85+
private String value;
86+
87+
DietEnum(String value) {
88+
this.value = value;
89+
}
90+
91+
/**
92+
* Get the value of the enum
93+
* @return The enum value
94+
*/
95+
@JsonValue
96+
@Nonnull public String getValue() {
97+
return value;
98+
}
99+
100+
/**
101+
* Get the String value of the enum value.
102+
* @return The enum value as String
103+
*/
104+
@Override
105+
@Nonnull public String toString() {
106+
return String.valueOf(value);
107+
}
108+
109+
/**
110+
* Get the enum value from a String value
111+
* @param value The String value
112+
* @return The enum value of type Soda
113+
*/
114+
@JsonCreator
115+
@Nullable public static DietEnum fromValue(@Nonnull final String value) {
116+
for (DietEnum b : DietEnum.values()) {
117+
if (b.value.equals(value)) {
118+
return b;
119+
}
120+
}
121+
return null;
122+
}
123+
}
124+
125+
@JsonProperty("diet")
126+
private DietEnum diet;
127+
66128
@JsonProperty("embedding")
67129
private float[] embedding;
68130

@@ -244,6 +306,35 @@ public void setPrice( @Nonnull final Float price) {
244306
this.price = price;
245307
}
246308

309+
/**
310+
* Set the diet of this {@link Soda} instance and return the same instance.
311+
*
312+
* @param diet The diet of this {@link Soda}
313+
* @return The same instance of this {@link Soda} class
314+
*/
315+
@Nonnull public Soda diet( @Nullable final DietEnum diet) {
316+
this.diet = diet;
317+
return this;
318+
}
319+
320+
/**
321+
* Get diet
322+
* @return diet The diet of this {@link Soda} instance.
323+
*/
324+
@Nullable
325+
public DietEnum getDiet() {
326+
return diet;
327+
}
328+
329+
/**
330+
* Set the diet of this {@link Soda} instance.
331+
*
332+
* @param diet The diet of this {@link Soda}
333+
*/
334+
public void setDiet( @Nullable final DietEnum diet) {
335+
this.diet = diet;
336+
}
337+
247338
/**
248339
* Set the embedding of this {@link Soda} instance and return the same instance.
249340
*
@@ -315,6 +406,7 @@ public Map<String, Object> toMap()
315406
if( isAvailable != null ) declaredFields.put("isAvailable", isAvailable);
316407
if( flavor != null ) declaredFields.put("flavor", flavor);
317408
if( price != null ) declaredFields.put("price", price);
409+
if( diet != null ) declaredFields.put("diet", diet);
318410
if( embedding != null ) declaredFields.put("embedding", embedding);
319411
return declaredFields;
320412
}
@@ -348,12 +440,13 @@ public boolean equals(@Nullable final java.lang.Object o) {
348440
Objects.equals(this.isAvailable, soda.isAvailable) &&
349441
Objects.equals(this.flavor, soda.flavor) &&
350442
Objects.equals(this.price, soda.price) &&
443+
Objects.equals(this.diet, soda.diet) &&
351444
Arrays.equals(this.embedding, soda.embedding);
352445
}
353446

354447
@Override
355448
public int hashCode() {
356-
return Objects.hash(id, name, brand, isAvailable, flavor, price, Arrays.hashCode(embedding), cloudSdkCustomFields);
449+
return Objects.hash(id, name, brand, isAvailable, flavor, price, diet, Arrays.hashCode(embedding), cloudSdkCustomFields);
357450
}
358451

359452
@Override
@@ -366,6 +459,7 @@ public int hashCode() {
366459
sb.append(" isAvailable: ").append(toIndentedString(isAvailable)).append("\n");
367460
sb.append(" flavor: ").append(toIndentedString(flavor)).append("\n");
368461
sb.append(" price: ").append(toIndentedString(price)).append("\n");
462+
sb.append(" diet: ").append(toIndentedString(diet)).append("\n");
369463
sb.append(" embedding: ").append(toIndentedString(embedding)).append("\n");
370464
cloudSdkCustomFields.forEach((k,v) -> sb.append(" ").append(k).append(": ").append(toIndentedString(v)).append("\n"));
371465
sb.append("}");

0 commit comments

Comments
 (0)