From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 23B9D75045 for ; Wed, 23 Jun 2021 12:05:18 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1CF3E9412 for ; Wed, 23 Jun 2021 12:04:48 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 039029295 for ; Wed, 23 Jun 2021 12:04:46 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id D8B9E437B2 for ; Wed, 23 Jun 2021 12:04:45 +0200 (CEST) From: Aaron Lauterer To: pve-devel@lists.proxmox.com Date: Wed, 23 Jun 2021 12:04:41 +0200 Message-Id: <20210623100444.1440650-2-a.lauterer@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623100444.1440650-1-a.lauterer@proxmox.com> References: <20210623100444.1440650-1-a.lauterer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.752 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH dart_api_client 1/4] Add string serializer to handle int and double values X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Jun 2021 10:05:18 -0000 Adding a custom serializer for Strings allows us to catch situations where the PVE API provides an int or a double instead of the expected string and convert that to a string. Signed-off-by: Aaron Lauterer --- lib/src/models/pve_string_serializer.dart | 25 +++++++++++++++++++++++ lib/src/models/serializers.dart | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 lib/src/models/pve_string_serializer.dart diff --git a/lib/src/models/pve_string_serializer.dart b/lib/src/models/pve_string_serializer.dart new file mode 100644 index 0000000..0622360 --- /dev/null +++ b/lib/src/models/pve_string_serializer.dart @@ -0,0 +1,25 @@ +import 'package:built_collection/built_collection.dart'; +import 'package:built_value/serializer.dart'; + +class PveStringSerializer implements PrimitiveSerializer { + final bool structured = false; + @override + final Iterable types = BuiltList([String]); + @override + final String wireName = 'String'; + + @override + Object serialize(Serializers serializers, String string, + {FullType specifiedType = FullType.unspecified}) { + return string; + } + + @override + String deserialize(Serializers serializers, Object? serialized, + {FullType specifiedType = FullType.unspecified}) { + if (serialized is int || serialized is double) { + return serialized.toString(); + } + return serialized as String; + } +} diff --git a/lib/src/models/serializers.dart b/lib/src/models/serializers.dart index c079ee3..57b06f4 100644 --- a/lib/src/models/serializers.dart +++ b/lib/src/models/serializers.dart @@ -3,6 +3,7 @@ import 'package:built_value/json_object.dart'; import 'package:built_value/serializer.dart'; import 'package:built_value/standard_json_plugin.dart'; import 'package:proxmox_dart_api_client/src/models/pve_bool_serializer.dart'; +import 'package:proxmox_dart_api_client/src/models/pve_string_serializer.dart'; import 'package:proxmox_dart_api_client/src/models/pve_datetime_from_epoch_serializer.dart'; import 'package:proxmox_dart_api_client/src/models/pve_models.dart'; @@ -41,5 +42,6 @@ part 'serializers.g.dart'; final Serializers serializers = (_$serializers.toBuilder() ..addPlugin(StandardJsonPlugin()) ..add(PveBoolSerializer()) + ..add(PveStringSerializer()) ..add(PveDateTimeFromEpoch())) .build(); -- 2.30.2