diff --git a/CHANGELOG.md b/CHANGELOG.md index 57b7e81..cafb0ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,4 +28,8 @@ - Fixed missing default value return in json class field ## 0.2.3 -- Added skip exceptions value in json list field \ No newline at end of file +- Added skip exceptions value in json list field + +## 0.2.4 +- Small changes applied on json class field to handle correctly null item +- Refactor nullOnException for skipException \ No newline at end of file diff --git a/CHANGELOG_LATEST.md b/CHANGELOG_LATEST.md index 618e411..9f8ca8a 100644 --- a/CHANGELOG_LATEST.md +++ b/CHANGELOG_LATEST.md @@ -1 +1,2 @@ -- Added skip exceptions value in json list field \ No newline at end of file +- Small changes applied on json class field to handle correctly null item +- Refactor nullOnException for skipException \ No newline at end of file diff --git a/lib/body_utils.dart b/lib/body_utils.dart index 178f548..263f8fe 100644 --- a/lib/body_utils.dart +++ b/lib/body_utils.dart @@ -111,14 +111,14 @@ dynamic jsonListField ( } dynamic jsonClassField ( - dynamic json, List field, T Function (dynamic) fromJson, - {bool nullable = true, bool nullOnException = false, T? defaultValue} + dynamic json, List field, T? Function (dynamic) fromJson, + {bool nullable = true, bool skipException = false, T? defaultValue} ) { assert ( ( - nullable && ( nullOnException || !nullOnException ) + nullable && ( skipException || !skipException ) ) - || (!nullable && !nullOnException) + || (!nullable && !skipException) ); T? retval; @@ -130,6 +130,10 @@ dynamic jsonClassField ( if (body != null) { try { retval = fromJson (body); + + if (retval == null && !nullable) { + throw NoSuchMethodError; + } } on BodyException { rethrow; } on NoSuchMethodError catch (_) { @@ -148,7 +152,7 @@ dynamic jsonClassField ( } } on BodyException { - if (!nullOnException) { + if (!skipException) { rethrow; } } diff --git a/pubspec.yaml b/pubspec.yaml index 0a2b706..bb699ae 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: http_request_utils description: Http requests utils -version: 0.2.3 +version: 0.2.4 homepage: https://github.com/Djcharles26/ repository: https://github.com/Djcharles26/http_utils.git diff --git a/test/body_utils_test.dart b/test/body_utils_test.dart index 0383f1b..72e33f2 100644 --- a/test/body_utils_test.dart +++ b/test/body_utils_test.dart @@ -122,19 +122,19 @@ 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, ["badClass"], TestClass.fromJson, skipException: true, nullable: true), null); expect (jsonClassField(json, ["object"], (item) => TestClass.fromJson (item)), null); expect ( jsonClassField( json, ["object"], TestClass.fromJson, - nullable: true, nullOnException: true, defaultValue: TestClass.empty () + nullable: true, skipException: true, defaultValue: TestClass.empty () ), isA() ); }); 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 ())); + expect (() => jsonClassField (json, [], TestClass.fromJson, skipException: true, nullable: false), throwsA (isA ())); }); test ("Check for exceptions with incorrect file types", () { diff --git a/version.txt b/version.txt index 373f8c6..72f9fa8 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.2.3 \ No newline at end of file +0.2.4 \ No newline at end of file