Skip to content

Commit 78d7ffb

Browse files
amondnetwing328
authored andcommitted
feat(dart-dio): correctly handle Map<String, Object>, List<Object> using JsonObject (#4401)
* feat(dart-dio): correctly handle Map<String, Object>, List<Object> using JsonObject Signed-off-by: Minsu Lee <[email protected]> * feat(dart-dio): correctly handle Map<String, Object>, List<Object> using JsonObject Signed-off-by: Minsu Lee <[email protected]>
1 parent d175673 commit 78d7ffb

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public DartDioClientCodegen() {
8080

8181
importMapping.put("BuiltList", "package:built_collection/built_collection.dart");
8282
importMapping.put("BuiltMap", "package:built_collection/built_collection.dart");
83+
importMapping.put("JsonObject", "package:built_value/json_object.dart");
8384
importMapping.put("Uint8List", "dart:typed_data");
8485
}
8586

@@ -129,6 +130,13 @@ public String toEnumVarName(String name, String datatype) {
129130
return name;
130131
}
131132

133+
@Override
134+
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
135+
//super.addAdditionPropertiesToCodeGenModel(codegenModel, schema);
136+
codegenModel.additionalPropertiesType = getSchemaType(ModelUtils.getAdditionalProperties(schema));
137+
addImport(codegenModel, codegenModel.additionalPropertiesType);
138+
}
139+
132140
@Override
133141
public void processOpts() {
134142
if (StringUtils.isEmpty(System.getenv("DART_POST_PROCESS_FILE"))) {
@@ -230,9 +238,16 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
230238
//Updates any List properties on a model to a BuiltList. This happens in post processing rather
231239
//than type mapping as we only want this to apply to models, not every other class.
232240
if ("List".equals(property.baseType)) {
233-
property.setDatatype(property.dataType.replaceAll(property.baseType, "BuiltList"));
241+
property.setDatatype(
242+
property.dataType.replaceAll(property.baseType, "BuiltList"));
234243
property.setBaseType("BuiltList");
235244
model.imports.add("BuiltList");
245+
if ("Object".equals(property.items.baseType)) {
246+
property.setDatatype(
247+
property.dataType.replaceAll("Object", "JsonObject"));
248+
property.items.setDatatype("JsonObject");
249+
model.imports.add("JsonObject");
250+
}
236251
}
237252
}
238253
if (property.isMapContainer) {
@@ -242,6 +257,11 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
242257
property.setDatatype(property.dataType.replaceAll(property.baseType, "BuiltMap"));
243258
property.setBaseType("BuiltMap");
244259
model.imports.add("BuiltMap");
260+
if ("Object".equals(property.items.baseType)) {
261+
property.setDatatype(property.dataType.replaceAll("Object", "JsonObject"));
262+
property.items.setDatatype("JsonObject");
263+
model.imports.add("JsonObject");
264+
}
245265
}
246266
}
247267

modules/openapi-generator/src/main/resources/dart-dio/serializers.mustache

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ library serializers;
22

33
import 'package:built_value/serializer.dart';
44
import 'package:built_collection/built_collection.dart';
5+
import 'package:built_value/json_object.dart';
56
import 'package:built_value/standard_json_plugin.dart';
67

78
{{#models}}{{#model}}import 'package:{{pubName}}/model/{{classFilename}}.dart';
@@ -23,4 +24,4 @@ const FullType(BuiltList, const [const FullType({{classname}})]),
2324
).build();
2425

2526
Serializers standardSerializers =
26-
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
27+
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

0 commit comments

Comments
 (0)