Skip to content

Commit cfe6520

Browse files
authored
fix(avro-schema): fix NPE for null enum values (#19771)
1 parent 817da39 commit cfe6520

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6648,7 +6648,7 @@ protected List<Map<String, Object>> buildEnumVars(List<Object> values, String da
66486648
for (Object value : values) {
66496649
if (value == null) {
66506650
// raw null values in enums are unions for nullable
6651-
// atttributes, not actual enum values, so we remove them here
6651+
// attributes, not actual enum values, so we remove them here
66526652
continue;
66536653
}
66546654
Map<String, Object> enumVar = new HashMap<>();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,11 @@ public String escapeQuotationMark(String input) {
199199

200200
@Override
201201
protected List<Map<String, Object>> buildEnumVars(List<Object> values, String dataType) {
202-
List<Object> sanitizedValues = values.stream().map(Object::toString).map(this::sanitizeEnumValue)
203-
.collect(Collectors.toList());
202+
List<Object> sanitizedValues = values.stream()
203+
.filter(x -> x != null)
204+
.map(Object::toString)
205+
.map(this::sanitizeEnumValue)
206+
.collect(Collectors.toList());
204207
removeEnumValueCollisions(sanitizedValues);
205208
return super.buildEnumVars(sanitizedValues, dataType);
206209
}

modules/openapi-generator/src/test/resources/3_0/avro-schema/valid-enums.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,16 @@ components:
2727
# The next two is to make sure collisions are resolved properly
2828
- 'coll-ision'
2929
- 'coll_ision'
30-
type: 'string'
30+
type: 'string'
31+
another:
32+
enum:
33+
- 'x'
34+
- 'y'
35+
- null
36+
type: 'string'
37+
equivalent:
38+
enum:
39+
- 'x'
40+
- 'y'
41+
type: 'string'
42+
nullable: true

samples/openapi3/schema/valid-enums/avro-schema-enum/Sample.avsc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@
2020
}],
2121
"doc": "",
2222
"default": null
23+
},
24+
{
25+
"name": "another",
26+
"type": ["null", {
27+
"type": "enum",
28+
"name": "Sample_another",
29+
"symbols": [
30+
"x",
31+
"y"
32+
]
33+
}],
34+
"doc": "",
35+
"default": null
36+
},
37+
{
38+
"name": "equivalent",
39+
"type": ["null", {
40+
"type": "enum",
41+
"name": "Sample_equivalent",
42+
"symbols": [
43+
"x",
44+
"y"
45+
]
46+
}],
47+
"doc": "",
48+
"default": null
2349
}
2450
]
2551

0 commit comments

Comments
 (0)