From 78d7ffbce746be1754796d46862874d534ef471e Mon Sep 17 00:00:00 2001 From: amond Date: Sun, 17 Nov 2019 10:39:55 +0900 Subject: [PATCH] feat(dart-dio): correctly handle Map, List using JsonObject (#4401) * feat(dart-dio): correctly handle Map, List using JsonObject Signed-off-by: Minsu Lee * feat(dart-dio): correctly handle Map, List using JsonObject Signed-off-by: Minsu Lee --- .../languages/DartDioClientCodegen.java | 22 ++++++++++++++++++- .../resources/dart-dio/serializers.mustache | 3 ++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java index f61e1b7b55cb..0729817c4891 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java @@ -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"); } @@ -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"))) { @@ -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) { @@ -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"); + } } } diff --git a/modules/openapi-generator/src/main/resources/dart-dio/serializers.mustache b/modules/openapi-generator/src/main/resources/dart-dio/serializers.mustache index 141eaa307e4c..dea3a0ecff1b 100644 --- a/modules/openapi-generator/src/main/resources/dart-dio/serializers.mustache +++ b/modules/openapi-generator/src/main/resources/dart-dio/serializers.mustache @@ -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'; @@ -23,4 +24,4 @@ const FullType(BuiltList, const [const FullType({{classname}})]), ).build(); Serializers standardSerializers = -(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build(); \ No newline at end of file +(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();