From aacb05d677d86bd3a42b044476ebf7ad2c66cf86 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 | 38 +++++++++---- .../responses/swagger_schema.g.dart | 54 +++++++++---------- 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/lib/src/swagger_models/responses/swagger_schema.dart b/lib/src/swagger_models/responses/swagger_schema.dart index eeb9fb21..4262bd78 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 {}, @@ -28,8 +28,8 @@ class SwaggerSchema { this.title = '', this.readOnly = false, this.writeOnly = false, - this.deprecated = 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; diff --git a/lib/src/swagger_models/responses/swagger_schema.g.dart b/lib/src/swagger_models/responses/swagger_schema.g.dart index f403cca2..aa70749d 100644 --- a/lib/src/swagger_models/responses/swagger_schema.g.dart +++ b/lib/src/swagger_models/responses/swagger_schema.g.dart @@ -8,32 +8,32 @@ part of 'swagger_schema.dart'; SwaggerSchema _$SwaggerSchemaFromJson(Map json) => SwaggerSchema( - type: json['type'] as String? ?? '', - originalRef: json['originalRef'] as String? ?? '', - enumValuesObj: json['enum'] as List? ?? const [], - properties: (json['properties'] as Map?)?.map( - (k, e) => - MapEntry(k, SwaggerSchema.fromJson(e as Map)), + type: json['type'], + originalRef: json['originalRef'] as String? ?? '', + enumValuesObj: json['enum'] as List? ?? const [], + properties: (json['properties'] as Map?)?.map( + (k, e) => + MapEntry(k, SwaggerSchema.fromJson(e as Map)), ) ?? - const {}, - items: json['items'] == null - ? null - : SwaggerSchema.fromJson(json['items'] as Map), - ref: json[r'$ref'] as String? ?? '', - defaultValue: json['default'], - format: json['format'] as String? ?? '', - schema: json['schema'] == null - ? null - : SwaggerSchema.fromJson(json['schema'] as Map), - oneOf: (json['oneOf'] as List?) + const {}, + items: json['items'] == null + ? null + : SwaggerSchema.fromJson(json['items'] as Map), + ref: json[r'$ref'] as String? ?? '', + defaultValue: json['default'], + format: json['format'] as String? ?? '', + schema: json['schema'] == null + ? null + : SwaggerSchema.fromJson(json['schema'] as Map), + oneOf: (json['oneOf'] as List?) ?.map((e) => SwaggerSchema.fromJson(e as Map)) .toList() ?? - const [], - anyOf: (json['anyOf'] as List?) + const [], + anyOf: (json['anyOf'] as List?) ?.map((e) => SwaggerSchema.fromJson(e as Map)) .toList() ?? - const [], - allOf: (json['allOf'] as List?) + const [], + allOf: (json['allOf'] as List?) ?.map((e) => SwaggerSchema.fromJson(e as Map)) .toList() ?? const [], @@ -89,22 +89,22 @@ Map _$SwaggerSchemaToJson(SwaggerSchema instance) => MsEnum _$MsEnumFromJson(Map json) => MsEnum( values: (json['values'] as List?) - ?.map((e) => MsEnumValue.fromJson(e as Map)) - .toList() ?? + ?.map((e) => MsEnumValue.fromJson(e as Map)) + .toList() ?? const [], - ); +); Map _$MsEnumToJson(MsEnum instance) => { 'values': instance.values, - }; +}; MsEnumValue _$MsEnumValueFromJson(Map json) => MsEnumValue( value: json['value'] ?? const [], - ); +); Map _$MsEnumValueToJson(MsEnumValue instance) => { - 'value': instance.value, + 'value': instance.value, }; Discriminator _$DiscriminatorFromJson(Map json) =>