From 40738352de4afe2fddf1345903464fb1eaccc01e Mon Sep 17 00:00:00 2001 From: Julian Steenbakker Date: Wed, 24 Jul 2024 15:58:59 +0200 Subject: [PATCH] bug: fix type not being recognized when specified as array (#742) * bug: fix type not being recognized when specified as array * bug: fix null type in array * bug: fix nullable type not being propagated --- .../responses/swagger_schema.dart | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/src/swagger_models/responses/swagger_schema.dart b/lib/src/swagger_models/responses/swagger_schema.dart index eeb9fb21..a15861da 100644 --- a/lib/src/swagger_models/responses/swagger_schema.dart +++ b/lib/src/swagger_models/responses/swagger_schema.dart @@ -6,7 +6,7 @@ part 'swagger_schema.g.dart'; @JsonSerializable() class SwaggerSchema { SwaggerSchema({ - this.type = '', + dynamic type = '', this.originalRef = '', this.enumValuesObj = const [], this.properties = const {}, @@ -29,7 +29,7 @@ class SwaggerSchema { this.readOnly = false, this.writeOnly = false, this.deprecated = false, - }); + }) : _type = type; @JsonKey(name: 'readOnly') bool readOnly; @@ -38,7 +38,20 @@ class SwaggerSchema { bool writeOnly; @JsonKey(name: 'type') - String type; + dynamic _type; + String get type { + if (_type is String?) return _type as String? ?? ''; + if (_type is List) { + for (final types in _type as List) { + if ((types as String?) != 'null') return types ?? ''; + } + } + return ''; + } + + set type(dynamic value) { + _type = value; + } @JsonKey(name: 'deprecated') bool deprecated; @@ -76,8 +89,8 @@ class SwaggerSchema { List get enumValues { final values = (msEnum?.values.isNotEmpty == true - ? msEnum?.values.map((e) => e.value) - : enumValuesObj) ?? + ? msEnum?.values.map((e) => e.value) + : enumValuesObj) ?? []; return values.map((e) => e.toString()).toList(); @@ -99,12 +112,15 @@ class SwaggerSchema { @JsonKey(name: 'nullable') bool? isNullable; - bool get shouldBeNullable => isNullable == true || readOnly || writeOnly; + bool get shouldBeNullable => + isNullable == true || + readOnly || + writeOnly || + (_type is List && (_type as List).contains('null')); @JsonKey(name: 'schema') SwaggerSchema? schema; - @JsonKey(name: 'oneOf') List oneOf; @JsonKey(name: 'anyOf') @@ -126,9 +142,9 @@ class SwaggerSchema { ..isNullable = (json[kIsNullable] ?? json[kNullable] ?? false) as bool; Map toJson() => { - ..._$SwaggerSchemaToJson(this), - if (enumNames != null) kEnumNames: enumNames, - }; + ..._$SwaggerSchemaToJson(this), + if (enumNames != null) kEnumNames: enumNames, + }; } bool _additionalsFromJson(dynamic value) => value != false;