Skip to content

Commit

Permalink
fixup! feat!: improve deserialization and production logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jun 2, 2024
1 parent 19a3a1d commit cfca780
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
27 changes: 21 additions & 6 deletions lib/src/core/definitions/extensions/json_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -295,22 +295,31 @@ extension ParseField on Map<String, dynamic> {

/// Parses a field with a given [name] as a [Map] of [DataSchema]s.
///
/// Returns `null` if the field should not be present or if it is not a
/// JSON object containing other objects.
/// Returns `null` if the field should not be present and throws a
/// [FormatException] if it is not a [Map] containing at least a number a
/// [minimalSize] of other [Map]s.
///
/// If a [Set] of [parsedFields] is passed to this function, the field [name]
/// will added. This can be used for filtering when parsing additional fields.
Map<String, DataSchema>? parseDataSchemaMapField(
String name,
PrefixMapping prefixMapping,
Set<String>? parsedFields,
) {
Set<String>? parsedFields, {
int minimalSize = 0,
}) {
final fieldValue = parseField<Map<String, dynamic>>(name, parsedFields);

if (fieldValue == null) {
return null;
}

final length = fieldValue.length;
if (fieldValue.length < minimalSize) {
throw FormatException(
"Expected this Map to contain at least $minimalSize other Map(s), "
"got $length.");
}

final result = <String, DataSchema>{};
for (final entry in fieldValue.entries) {
final value = entry.value;
Expand Down Expand Up @@ -390,15 +399,21 @@ extension ParseField on Map<String, dynamic> {
///
/// Adds the key `securityDefinitions` to the set of [parsedFields], if
/// defined.
Map<String, SecurityScheme>? parseSecurityDefinitions(
Map<String, SecurityScheme> parseSecurityDefinitions(
PrefixMapping prefixMapping,
Set<String> parsedFields,
) {
final fieldValue =
parseMapField<dynamic>("securityDefinitions", parsedFields);

if (fieldValue == null) {
return null;
throw const FormatException("Missing required securityDefinitions field");
}

if (fieldValue.isEmpty) {
throw const FormatException(
"securityDefinitions has to contain at least one element but is empty.",
);
}

final Map<String, SecurityScheme> result = {};
Expand Down
3 changes: 2 additions & 1 deletion lib/src/core/definitions/thing_description.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ class ThingDescription {
final security = json.parseRequiredArrayField<String>(
"security",
parsedFields: parsedFields,
minimalSize: 1,
);

final securityDefinitions =
json.parseSecurityDefinitions(prefixMapping, parsedFields) ?? {};
json.parseSecurityDefinitions(prefixMapping, parsedFields);

final forms = json.parseForms(prefixMapping, parsedFields);
// TODO: Move somewhere else
Expand Down

0 comments on commit cfca780

Please sign in to comment.