From ff04593c66ceb63b0de0cb434fce73f7516edd10 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Fri, 1 Nov 2024 11:18:29 +0100 Subject: [PATCH] fix(data_schema.dart): don't serialize of default writeOnly and readOnly --- lib/src/core/definitions/data_schema.dart | 28 ++++++++++++++----- .../interaction_affordances/property.dart | 4 +-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/src/core/definitions/data_schema.dart b/lib/src/core/definitions/data_schema.dart index ca9cc0b7..3eb9c2c4 100644 --- a/lib/src/core/definitions/data_schema.dart +++ b/lib/src/core/definitions/data_schema.dart @@ -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, @@ -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 @@ -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? atType; @@ -175,10 +180,10 @@ class DataSchema implements Serializable { final List? 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. /// @@ -292,8 +297,6 @@ class DataSchema implements Serializable { ("const", constant), ("default", defaultValue), ("enum", enumeration), - ("readOnly", readOnly), - ("writeOnly", writeOnly), ("format", format), ("unit", unit), ("type", type), @@ -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; } } diff --git a/lib/src/core/definitions/interaction_affordances/property.dart b/lib/src/core/definitions/interaction_affordances/property.dart index b678b153..3566e6f2 100644 --- a/lib/src/core/definitions/interaction_affordances/property.dart +++ b/lib/src/core/definitions/interaction_affordances/property.dart @@ -101,7 +101,7 @@ class Property extends InteractionAffordance implements DataSchema { List? get oneOf => dataSchema.oneOf; @override - bool get readOnly => dataSchema.readOnly ?? false; + bool get readOnly => dataSchema.readOnly; @override String? get type => dataSchema.type; @@ -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;