From 940872f03f38704145aa0ec979a69ab386fb4210 Mon Sep 17 00:00:00 2001 From: Ricardo Boss Date: Wed, 20 Mar 2024 02:19:40 +0100 Subject: [PATCH] Sanitize uuid, timeonly and dateonly values to strings before passing them as substitutions --- lib/src/request_information.dart | 12 ++++++++ test/request_information_test.dart | 46 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/lib/src/request_information.dart b/lib/src/request_information.dart index 0f4b951..0df02f0 100644 --- a/lib/src/request_information.dart +++ b/lib/src/request_information.dart @@ -99,6 +99,18 @@ class RequestInformation { return value.toIso8601String(); } + if (value is UuidValue) { + return value.toFormattedString(); + } + + if (value is TimeOnly) { + return value.toRfc3339String(); + } + + if (value is DateOnly) { + return value.toRfc3339String(); + } + if (value is Enum) { return value.name; } diff --git a/test/request_information_test.dart b/test/request_information_test.dart index 147c81c..962c677 100644 --- a/test/request_information_test.dart +++ b/test/request_information_test.dart @@ -5,6 +5,7 @@ import 'dart:typed_data'; import 'package:kiota_abstractions/kiota_abstractions.dart'; import 'package:mockito/annotations.dart'; import 'package:test/test.dart'; +import 'package:uuid/uuid.dart'; import 'request_information_test.mocks.dart'; @@ -92,6 +93,51 @@ void main() { ); }); + test('Sets path parameters of DateOnly type', () { + final requestInfo = RequestInformation( + httpMethod: HttpMethod.get, + urlTemplate: 'http://localhost/{date}', + ); + + // Act + final date = DateOnly.fromComponents(2024, 3, 20); + requestInfo.pathParameters['date'] = date; + + // Assert + expect(requestInfo.uri.toString(), endsWith('2024-03-20')); + }); + + test('Sets path parameters of TimeOnly type', () { + final requestInfo = RequestInformation( + httpMethod: HttpMethod.get, + urlTemplate: 'http://localhost/{time}', + ); + + // Act + final time = TimeOnly.fromComponents(12, 34, 56); + requestInfo.pathParameters['time'] = time; + + // Assert + expect(requestInfo.uri.toString(), endsWith('12%3A34%3A56')); + }); + + test('Sets path parameters of UuidValue type', () { + final requestInfo = RequestInformation( + httpMethod: HttpMethod.get, + urlTemplate: 'http://localhost/{uuid}', + ); + + // Act + final uuid = UuidValue.fromString('01234567-89ab-cdef-0123-456789abcdef'); + requestInfo.pathParameters['uuid'] = uuid; + + // Assert + expect( + requestInfo.uri.toString(), + endsWith('01234567-89ab-cdef-0123-456789abcdef'), + ); + }); + test('Sets path parameters of boolean type', () { // Arrange as the request builders would final requestInfo = RequestInformation(