Skip to content

Commit

Permalink
fixup! feat!: improve deserialization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jun 1, 2024
1 parent 7e7fc67 commit 42fe46c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
13 changes: 13 additions & 0 deletions lib/src/core/definitions/extensions/json_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ extension ParseField on Map<String, dynamic> {
throw FormatException("$T");
}

List<T> parseRequiredArrayField<T>(
String name, [
Set<String>? parsedFields,
]) {
final result = parseArrayField<T>(name, parsedFields);

if (result == null) {
throw FormatException("Missing required field $name");
}

return result;
}

/// Parses a field with a given [name] as a [DataSchema].
///
/// Returns `null` if the field should not be present or if it is not a JSON
Expand Down
3 changes: 1 addition & 2 deletions lib/src/core/definitions/thing_description.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import "package:curie/curie.dart";
import "package:meta/meta.dart";

import "../exceptions.dart";
import "additional_expected_response.dart";
import "context.dart";
import "data_schema.dart";
Expand Down Expand Up @@ -86,7 +85,7 @@ class ThingDescription {
final id = json.parseField<String>("id", parsedFields);

final security =
json.parseArrayField<String>("security", parsedFields) ?? [];
json.parseRequiredArrayField<String>("security", parsedFields);

final securityDefinitions =
json.parseSecurityDefinitions(prefixMapping, parsedFields) ?? {};
Expand Down
2 changes: 1 addition & 1 deletion test/core/thing_description_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void main() {

expect(
() => ThingDescription.fromJson(thingDescriptionJson),
throwsA(isA<ValidationException>()),
throwsA(isA<FormatException>()),
);
});

Expand Down

0 comments on commit 42fe46c

Please sign in to comment.