Skip to content

Commit

Permalink
feat(dart-dio): correctly handle Map<String, Object>, List<Object> us…
Browse files Browse the repository at this point in the history
…ing 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]>
  • Loading branch information
amondnet authored and wing328 committed Nov 17, 2019
1 parent d175673 commit 78d7ffb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public DartDioClientCodegen() {

importMapping.put("BuiltList", "package:built_collection/built_collection.dart");
importMapping.put("BuiltMap", "package:built_collection/built_collection.dart");
importMapping.put("JsonObject", "package:built_value/json_object.dart");
importMapping.put("Uint8List", "dart:typed_data");
}

Expand Down Expand Up @@ -129,6 +130,13 @@ public String toEnumVarName(String name, String datatype) {
return name;
}

@Override
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
//super.addAdditionPropertiesToCodeGenModel(codegenModel, schema);
codegenModel.additionalPropertiesType = getSchemaType(ModelUtils.getAdditionalProperties(schema));
addImport(codegenModel, codegenModel.additionalPropertiesType);
}

@Override
public void processOpts() {
if (StringUtils.isEmpty(System.getenv("DART_POST_PROCESS_FILE"))) {
Expand Down Expand Up @@ -230,9 +238,16 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
//Updates any List properties on a model to a BuiltList. This happens in post processing rather
//than type mapping as we only want this to apply to models, not every other class.
if ("List".equals(property.baseType)) {
property.setDatatype(property.dataType.replaceAll(property.baseType, "BuiltList"));
property.setDatatype(
property.dataType.replaceAll(property.baseType, "BuiltList"));
property.setBaseType("BuiltList");
model.imports.add("BuiltList");
if ("Object".equals(property.items.baseType)) {
property.setDatatype(
property.dataType.replaceAll("Object", "JsonObject"));
property.items.setDatatype("JsonObject");
model.imports.add("JsonObject");
}
}
}
if (property.isMapContainer) {
Expand All @@ -242,6 +257,11 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
property.setDatatype(property.dataType.replaceAll(property.baseType, "BuiltMap"));
property.setBaseType("BuiltMap");
model.imports.add("BuiltMap");
if ("Object".equals(property.items.baseType)) {
property.setDatatype(property.dataType.replaceAll("Object", "JsonObject"));
property.items.setDatatype("JsonObject");
model.imports.add("JsonObject");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library serializers;

import 'package:built_value/serializer.dart';
import 'package:built_collection/built_collection.dart';
import 'package:built_value/json_object.dart';
import 'package:built_value/standard_json_plugin.dart';

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

Serializers standardSerializers =
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

0 comments on commit 78d7ffb

Please sign in to comment.