[BUG][DART] anyOf serializer throws exception
Created by: szekelyisz
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The generated Dart serializer code for anyOf
schemas throws an exception when the referenced types are also schemas themselves.
openapi-generator version
6.3.0-SNAPSHOT 90e468b9
OpenAPI declaration file content or url
swagger.json
{
"components": {
"examples": {},
"headers": {},
"parameters": {},
"requestBodies": {},
"responses": {},
"schemas": {
"NetworkConfigManual": {
"properties": {
"method": {
"type": "string",
"enum": [
"manual"
],
"nullable": false
},
"address": {
"type": "string"
},
"prefix": {
"type": "integer",
"format": "int32"
},
"gateway": {
"type": "string"
},
"dnss": {
"items": {
"type": "string"
},
"type": "array"
}
},
"required": [
"method",
"address",
"prefix",
"dnss"
],
"type": "object",
"additionalProperties": false
},
"NetworkConfigAuto": {
"properties": {
"method": {
"type": "string",
"enum": [
"auto"
],
"nullable": false
}
},
"required": [
"method"
],
"type": "object",
"additionalProperties": false
},
"NetworkConfig": {
"anyOf": [
{
"$ref": "#/components/schemas/NetworkConfigManual"
},
{
"$ref": "#/components/schemas/NetworkConfigAuto"
}
]
}
},
"securitySchemes": {}
},
"info": {
"title": "MyAPI",
"version": "1.0.0"
},
"openapi": "3.0.0",
"paths": {}
}
Generation Details
generate -i swagger.json -g dart-dio -o my_api/generated --additional-properties=legacyDiscriminatorBehavior=false
Steps to reproduce
Generate Dart code using the above schema and command.
Not sure if it's correct but this how I pass data to this serializer:
networkConfig: (NetworkConfigBuilder()
..anyOf = AnyOf2(values: {
1: (NetworkConfigManualBuilder()
..method = NetworkConfigManualMethodEnum.manual
..address = address.address
..prefix = prefix
..gateway = gateway?.address
..dnss = ListBuilder(dnss.map((e) => e.address)))
.build()
}))
.build()
Suggest a fix
Here's the generated Dart serializer code for the NetworkConfig
schema from the above specification:
@override
Object serialize(
Serializers serializers,
NetworkConfig object, {
FullType specifiedType = FullType.unspecified,
}) {
final anyOf = object.anyOf;
final result = <Object?>[];
for (var _valueEntry in anyOf.values.entries) {
final _typeIndex = _valueEntry.key;
final _type = anyOf.types[_typeIndex];
final _value = _valueEntry.value;
result.addAll(serializers.serialize(_value, specifiedType: FullType(_type)) as Iterable<Object?>);
}
return result;
}
serializers
become BuiltJsonSerializers
in this case. The problem here is that the returned value by serializers.serialize
is a _InternalLinkedHashMap<String, Object?>
and it is force-cast to Iterable<Object?>
which obviously throws an exception.
Maybe there needs to be a distinction whether the referenced data types are schemas or primitives and serialization needs to branch accordingly? I don't have enough insight into openapi-generator to tell this.