Skip to content

Commit

Permalink
fix(data_schema.dart): don't serialize of default writeOnly and readOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Nov 1, 2024
1 parent d73e2fe commit ff04593
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
28 changes: 21 additions & 7 deletions lib/src/core/definitions/data_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class DataSchema implements Serializable {
this.unit,
this.oneOf,
this.enumeration,
this.readOnly = false,
this.writeOnly = false,
bool? readOnly,
bool? writeOnly,
this.format,
this.type,
this.minimum,
Expand All @@ -50,7 +50,8 @@ class DataSchema implements Serializable {
this.contentEncoding,
this.contentMediaType,
this.additionalFields = const {},
});
}) : readOnly = readOnly ?? _defaultReadOnly,
writeOnly = writeOnly ?? _defaultWriteOnly;

// TODO: Consider creating separate classes for each data type.
// Also see https://github.com/w3c/wot-thing-description/issues/1390
Expand Down Expand Up @@ -142,6 +143,10 @@ class DataSchema implements Serializable {
);
}

static const _defaultWriteOnly = false;

static const _defaultReadOnly = false;

/// JSON-LD keyword (@type) to label the object with semantic tags (or types).
final List<String>? atType;

Expand Down Expand Up @@ -175,10 +180,10 @@ class DataSchema implements Serializable {
final List<Object?>? enumeration;

/// Indicates if a value is read only.
final bool? readOnly;
final bool readOnly;

/// Indicates if a value is write only.
final bool? writeOnly;
final bool writeOnly;

/// Allows validation based on a format pattern.
///
Expand Down Expand Up @@ -292,8 +297,6 @@ class DataSchema implements Serializable {
("const", constant),
("default", defaultValue),
("enum", enumeration),
("readOnly", readOnly),
("writeOnly", writeOnly),
("format", format),
("unit", unit),
("type", type),
Expand Down Expand Up @@ -332,6 +335,17 @@ class DataSchema implements Serializable {
result[key] = convertedValue;
}

final keyValuePairsWithDefault = [
("readOnly", readOnly, _defaultReadOnly),
("writeOnly", writeOnly, _defaultWriteOnly),
];

for (final (key, value, defaultValue) in keyValuePairsWithDefault) {
if (value != defaultValue) {
result[key] = value;
}
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Property extends InteractionAffordance implements DataSchema {
List<DataSchema>? get oneOf => dataSchema.oneOf;

@override
bool get readOnly => dataSchema.readOnly ?? false;
bool get readOnly => dataSchema.readOnly;

@override
String? get type => dataSchema.type;
Expand All @@ -110,7 +110,7 @@ class Property extends InteractionAffordance implements DataSchema {
String? get unit => dataSchema.unit;

@override
bool get writeOnly => dataSchema.writeOnly ?? false;
bool get writeOnly => dataSchema.writeOnly;

@override
String? get contentEncoding => dataSchema.contentEncoding;
Expand Down

0 comments on commit ff04593

Please sign in to comment.