From 5468f89f53263a0fa2c00367c561d41816fc77d8 Mon Sep 17 00:00:00 2001 From: Djcharles26 Date: Tue, 17 May 2022 18:33:37 -0500 Subject: [PATCH 1/3] Updated how null on exception is executed --- lib/body_utils.dart | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/body_utils.dart b/lib/body_utils.dart index 2c92215..cf2acc1 100644 --- a/lib/body_utils.dart +++ b/lib/body_utils.dart @@ -112,25 +112,33 @@ dynamic jsonClassField ( T? retval; - dynamic body = jsonField (json, field, nullable: nullable); + try { + + dynamic body = jsonField (json, field, nullable: nullable); + + if (body != null) { + try { + retval = fromJson (body); + } on BodyException { + rethrow; + } on NoSuchMethodError catch (_) { + throw BodyException( + type: BodyExceptionType.isNull, + fieldName: field.join ("_"), + failedType: T, + currentType: retval.runtimeType + ); + } catch (error) { + throw BodyException( + type: BodyExceptionType.undefined, + fieldName: field.join ("_"), + ); + } + } - if (body != null) { - try { - retval = fromJson (body); - } on BodyException { + } on BodyException { + if (!nullOnException) { rethrow; - } on NoSuchMethodError catch (_) { - throw BodyException( - type: BodyExceptionType.isNull, - fieldName: field.join ("_"), - failedType: T, - currentType: retval.runtimeType - ); - } catch (error) { - throw BodyException( - type: BodyExceptionType.undefined, - fieldName: field.join ("_"), - ); } } From d68431c515e2633befa7b2842485da27a743d610 Mon Sep 17 00:00:00 2001 From: Djcharles26 Date: Tue, 17 May 2022 18:33:48 -0500 Subject: [PATCH 2/3] Added tests for null classes and assertion errors --- test/body_utils_test.dart | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/body_utils_test.dart b/test/body_utils_test.dart index 5731232..06ac07e 100644 --- a/test/body_utils_test.dart +++ b/test/body_utils_test.dart @@ -47,6 +47,14 @@ void main() { "\$oid": "123" }, "stringList": ["Hello", "World"], + "badClass": { + "_id": { + "\$oid": "456" + }, + "integer": 4, + "name": null, + "lastname": "Marin" + }, "organization": null, "integer": 15, "number": 0.45, @@ -92,11 +100,13 @@ void main() { test ('Check for class json fields', () { expect (jsonClassField(json, [], TestClass.fromJson, nullable: false), isA ()); + expect (jsonClassField(json, ["badClass"], TestClass.fromJson, nullOnException: true, nullable: true), null); expect (jsonClassField(json, ["object"], (item) => TestClass.fromJson (item)), null); }); test ('Check for class json fields with incorrect field type', () { expect (() => jsonClassField (json, [], TestListClass.fromJson, nullable: false), throwsA(isA ())); + expect (() => jsonClassField (json, [], TestClass.fromJson, nullOnException: true, nullable: false), throwsA (isA ())); }); test ("Check for exceptions with incorrect file types", () { From 8546f23386fca77036875ee670e4551cb0fd8ba9 Mon Sep 17 00:00:00 2001 From: Djcharles26 Date: Tue, 17 May 2022 18:34:39 -0500 Subject: [PATCH 3/3] Updated version to 0.2.1 --- CHANGELOG.md | 6 +++++- CHANGELOG_LATEST.md | 5 ++--- pubspec.yaml | 2 +- version.txt | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12ff775..f5af679 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,4 +17,8 @@ ## 0.2.0 - Added json list field method for handling lists - Added json class field method for handling custom classes -- Updated tests with new methods \ No newline at end of file +- Updated tests with new methods + +## 0.2.1 +- Updated how null on exception class is executed +- Added tests for null classes and assertion errors \ No newline at end of file diff --git a/CHANGELOG_LATEST.md b/CHANGELOG_LATEST.md index a8edd0f..24f4a7f 100644 --- a/CHANGELOG_LATEST.md +++ b/CHANGELOG_LATEST.md @@ -1,3 +1,2 @@ -- Added json list field method for handling lists -- Added json class field method for handling custom classes -- Updated tests with new methods \ No newline at end of file +- Updated how null on exception class is executed +- Added tests for null classes and assertion errors \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 770067c..8fd963e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: http_request_utils description: Http requests utils -version: 0.2.0 +version: 0.2.1 homepage: https://github.com/Djcharles26/ repository: https://github.com/Djcharles26/http_utils.git diff --git a/version.txt b/version.txt index 341cf11..7dff5b8 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.2.0 \ No newline at end of file +0.2.1 \ No newline at end of file