From 433e414fe7aa0872a397493d64a6df057139f416 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Fri, 14 Oct 2022 00:25:17 +0200 Subject: [PATCH] dynamite: Implement objects without named properties as maps --- packages/dynamite/lib/src/openapi_builder.dart | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index d5b30de4..8e7220fb 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -403,6 +403,14 @@ class OpenAPIBuilder implements Builder { result = TypeResolveResult('dynamic'); break; } + if (schema.properties!.isEmpty) { + result = TypeResolveResult( + 'Map', + isBaseType: false, + isMap: true, + ); + break; + } if (!resolvedTypes.contains(identifier)) { resolvedTypes.add(identifier); @@ -1218,6 +1226,7 @@ class TypeResolveResult { this.typeName, { this.isBaseType = true, this.isList = false, + this.isMap = false, this.isEnum = false, this.subType, }); @@ -1225,6 +1234,7 @@ class TypeResolveResult { final String typeName; final bool isBaseType; final bool isList; + final bool isMap; final bool isEnum; final TypeResolveResult? subType; } @@ -1232,6 +1242,8 @@ class TypeResolveResult { String _serializeFunctionForType(final String object, final TypeResolveResult result) { if (result.isList) { return '($object as ${result.typeName}).map((final e) => ${_serializeFunctionForType('e', result.subType!)}).toList()'; + } else if (result.isMap) { + return '($object as ${result.typeName})'; } else if (result.isBaseType) { return '$object.toString()'; } else if (result.isEnum) { @@ -1247,6 +1259,8 @@ String _deserializeFunctionForType(final String object, final TypeResolveResult return '$object as List'; } return '($object as List).map<${result.subType!.typeName}>((final e) => ${_deserializeFunctionForType('e', result.subType!)}).toList()'; + } else if (result.isMap) { + return '$object as Map'; } else if (result.isBaseType) { return '$object as ${result.typeName}'; } else if (result.isEnum) {