diff --git a/.github/workflows/samples-dart-build-test.yaml b/.github/workflows/samples-dart-build-test.yaml index 71b526d8fff..8aa8a3f4cf3 100644 --- a/.github/workflows/samples-dart-build-test.yaml +++ b/.github/workflows/samples-dart-build-test.yaml @@ -4,18 +4,20 @@ on: push: branches: paths: - #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/** + #- samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/** #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake_tests/** - #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/** + #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/** + - samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/** - samples/openapi3/client/petstore/dart-dio/oneof/** - samples/openapi3/client/petstore/dart-dio/oneof_polymorphism_and_inheritance/** - samples/openapi3/client/petstore/dart-dio/binary_response/** - samples/openapi3/client/petstore/dart-dio/petstore-timemachine/** pull_request: paths: - #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/** + #- samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/** #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake_tests/** - #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/** + #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/** + - samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/** - samples/openapi3/client/petstore/dart-dio/oneof/** - samples/openapi3/client/petstore/dart-dio/oneof_polymorphism_and_inheritance/** - samples/openapi3/client/petstore/dart-dio/binary_response/** @@ -29,9 +31,10 @@ jobs: os: [ubuntu-latest, windows-latest] sdk: ["3.9.0"] sample: - #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/ + #- samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/ #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake_tests/ - #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/ + #- samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/ + - samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/ - samples/openapi3/client/petstore/dart-dio/oneof/ - samples/openapi3/client/petstore/dart-dio/oneof_polymorphism_and_inheritance/ - samples/openapi3/client/petstore/dart-dio/binary_response/ diff --git a/modules/openapi-generator/src/test/resources/3_0/dart/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/openapi-generator/src/test/resources/3_0/dart/petstore-with-fake-endpoints-models-for-testing.yaml index 6c03b22cec2..074a9aa468e 100644 --- a/modules/openapi-generator/src/test/resources/3_0/dart/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/dart/petstore-with-fake-endpoints-models-for-testing.yaml @@ -1928,8 +1928,9 @@ components: properties: type: type: string - enum: - - ChildWithNullable + # comment out as it's causing compilation errors in Dart Dio client generator + #enum: + # - ChildWithNullable nullableProperty: type: string nullable: true diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/child_with_nullable.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/child_with_nullable.dart index b10f87341a2..f0e43000167 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/child_with_nullable.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/child_with_nullable.dart @@ -35,11 +35,10 @@ class ChildWithNullable { name: r'type', required: false, includeIfNull: false, - unknownEnumValue: ChildWithNullableTypeEnum.unknownDefaultOpenApi, ) - final ChildWithNullableTypeEnum? type; + final String? type; @@ -92,19 +91,3 @@ class ChildWithNullable { } - -enum ChildWithNullableTypeEnum { -@JsonValue(r'ChildWithNullable') -childWithNullable(r'ChildWithNullable'), -@JsonValue(r'unknown_default_open_api') -unknownDefaultOpenApi(r'unknown_default_open_api'); - -const ChildWithNullableTypeEnum(this.value); - -final String value; - -@override -String toString() => value; -} - - diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/parent_with_nullable.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/parent_with_nullable.dart index e4dce19ff33..121cf835d54 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/parent_with_nullable.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/parent_with_nullable.dart @@ -30,11 +30,10 @@ class ParentWithNullable { name: r'type', required: false, includeIfNull: false, - unknownEnumValue: ParentWithNullableTypeEnum.unknownDefaultOpenApi, ) - final ParentWithNullableTypeEnum? type; + final String? type; @@ -73,19 +72,3 @@ class ParentWithNullable { } - -enum ParentWithNullableTypeEnum { -@JsonValue(r'ChildWithNullable') -childWithNullable(r'ChildWithNullable'), -@JsonValue(r'unknown_default_open_api') -unknownDefaultOpenApi(r'unknown_default_open_api'); - -const ParentWithNullableTypeEnum(this.value); - -final String value; - -@override -String toString() => value; -} - - diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/child_with_nullable.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/child_with_nullable.dart index 8e40eb1d237..17c17aebfa1 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/child_with_nullable.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/child_with_nullable.dart @@ -3,7 +3,6 @@ // // ignore_for_file: unused_element -import 'package:built_collection/built_collection.dart'; import 'package:openapi/src/model/parent_with_nullable.dart'; import 'package:built_value/built_value.dart'; import 'package:built_value/serializer.dart'; @@ -48,7 +47,7 @@ class _$ChildWithNullableSerializer implements PrimitiveSerializer get serializer => _$childWithNullableTypeEnumSerializer; - - const ChildWithNullableTypeEnum._(String name): super(name); - - static BuiltSet get values => _$childWithNullableTypeEnumValues; - static ChildWithNullableTypeEnum valueOf(String name) => _$childWithNullableTypeEnumValueOf(name); -} - diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/parent_with_nullable.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/parent_with_nullable.dart index bf1252135cd..99300ffdfb5 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/parent_with_nullable.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/parent_with_nullable.dart @@ -3,7 +3,6 @@ // // ignore_for_file: unused_element -import 'package:built_collection/built_collection.dart'; import 'package:openapi/src/model/child_with_nullable.dart'; import 'package:built_value/built_value.dart'; import 'package:built_value/serializer.dart'; @@ -18,8 +17,7 @@ part 'parent_with_nullable.g.dart'; @BuiltValue(instantiable: false) abstract class ParentWithNullable { @BuiltValueField(wireName: r'type') - ParentWithNullableTypeEnum? get type; - // enum typeEnum { ChildWithNullable, }; + String? get type; @BuiltValueField(wireName: r'nullableProperty') String? get nullableProperty; @@ -67,7 +65,7 @@ class _$ParentWithNullableSerializer implements PrimitiveSerializer get serializer => _$parentWithNullableTypeEnumSerializer; - - const ParentWithNullableTypeEnum._(String name): super(name); - - static BuiltSet get values => _$parentWithNullableTypeEnumValues; - static ParentWithNullableTypeEnum valueOf(String name) => _$parentWithNullableTypeEnumValueOf(name); -} - diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/child_with_nullable.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/child_with_nullable.dart index 2092b2c7684..b50013df7b6 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/child_with_nullable.dart +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/child_with_nullable.dart @@ -18,7 +18,13 @@ class ChildWithNullable { this.otherProperty, }); - ChildWithNullableTypeEnum? type; + /// + /// Please note: This property should have been non-nullable! Since the specification file + /// does not include a default value (using the "default:" property), however, the generated + /// source code must fall back to having a nullable type. + /// Consider adding a "default:" property in the specification file to hide this note. + /// + String? type; String? nullableProperty; @@ -85,7 +91,7 @@ class ChildWithNullable { }()); return ChildWithNullable( - type: ChildWithNullableTypeEnum.fromJson(json[r'type']), + type: mapValueOfType(json, r'type'), nullableProperty: mapValueOfType(json, r'nullableProperty'), otherProperty: mapValueOfType(json, r'otherProperty'), ); @@ -138,74 +144,3 @@ class ChildWithNullable { }; } - -class ChildWithNullableTypeEnum { - /// Instantiate a new enum with the provided [value]. - const ChildWithNullableTypeEnum._(this.value); - - /// The underlying value of this enum member. - final String value; - - @override - String toString() => value; - - String toJson() => value; - - static const childWithNullable = ChildWithNullableTypeEnum._(r'ChildWithNullable'); - - /// List of all possible values in this [enum][ChildWithNullableTypeEnum]. - static const values = [ - childWithNullable, - ]; - - static ChildWithNullableTypeEnum? fromJson(dynamic value) => ChildWithNullableTypeEnumTypeTransformer().decode(value); - - static List listFromJson(dynamic json, {bool growable = false,}) { - final result = []; - if (json is List && json.isNotEmpty) { - for (final row in json) { - final value = ChildWithNullableTypeEnum.fromJson(row); - if (value != null) { - result.add(value); - } - } - } - return result.toList(growable: growable); - } -} - -/// Transformation class that can [encode] an instance of [ChildWithNullableTypeEnum] to String, -/// and [decode] dynamic data back to [ChildWithNullableTypeEnum]. -class ChildWithNullableTypeEnumTypeTransformer { - factory ChildWithNullableTypeEnumTypeTransformer() => _instance ??= const ChildWithNullableTypeEnumTypeTransformer._(); - - const ChildWithNullableTypeEnumTypeTransformer._(); - - String encode(ChildWithNullableTypeEnum data) => data.value; - - /// Decodes a [dynamic value][data] to a ChildWithNullableTypeEnum. - /// - /// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully, - /// then null is returned. However, if [allowNull] is false and the [dynamic value][data] - /// cannot be decoded successfully, then an [UnimplementedError] is thrown. - /// - /// The [allowNull] is very handy when an API changes and a new enum value is added or removed, - /// and users are still using an old app with the old code. - ChildWithNullableTypeEnum? decode(dynamic data, {bool allowNull = true}) { - if (data != null) { - switch (data) { - case r'ChildWithNullable': return ChildWithNullableTypeEnum.childWithNullable; - default: - if (!allowNull) { - throw ArgumentError('Unknown enum value to decode: $data'); - } - } - } - return null; - } - - /// Singleton [ChildWithNullableTypeEnumTypeTransformer] instance. - static ChildWithNullableTypeEnumTypeTransformer? _instance; -} - - diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/parent_with_nullable.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/parent_with_nullable.dart index 219967ad59f..6ff6eb1c4f3 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/parent_with_nullable.dart +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/parent_with_nullable.dart @@ -17,7 +17,13 @@ class ParentWithNullable { this.nullableProperty, }); - ParentWithNullableTypeEnum? type; + /// + /// Please note: This property should have been non-nullable! Since the specification file + /// does not include a default value (using the "default:" property), however, the generated + /// source code must fall back to having a nullable type. + /// Consider adding a "default:" property in the specification file to hide this note. + /// + String? type; String? nullableProperty; @@ -69,7 +75,7 @@ class ParentWithNullable { }()); return ParentWithNullable( - type: ParentWithNullableTypeEnum.fromJson(json[r'type']), + type: mapValueOfType(json, r'type'), nullableProperty: mapValueOfType(json, r'nullableProperty'), ); } @@ -121,74 +127,3 @@ class ParentWithNullable { }; } - -class ParentWithNullableTypeEnum { - /// Instantiate a new enum with the provided [value]. - const ParentWithNullableTypeEnum._(this.value); - - /// The underlying value of this enum member. - final String value; - - @override - String toString() => value; - - String toJson() => value; - - static const childWithNullable = ParentWithNullableTypeEnum._(r'ChildWithNullable'); - - /// List of all possible values in this [enum][ParentWithNullableTypeEnum]. - static const values = [ - childWithNullable, - ]; - - static ParentWithNullableTypeEnum? fromJson(dynamic value) => ParentWithNullableTypeEnumTypeTransformer().decode(value); - - static List listFromJson(dynamic json, {bool growable = false,}) { - final result = []; - if (json is List && json.isNotEmpty) { - for (final row in json) { - final value = ParentWithNullableTypeEnum.fromJson(row); - if (value != null) { - result.add(value); - } - } - } - return result.toList(growable: growable); - } -} - -/// Transformation class that can [encode] an instance of [ParentWithNullableTypeEnum] to String, -/// and [decode] dynamic data back to [ParentWithNullableTypeEnum]. -class ParentWithNullableTypeEnumTypeTransformer { - factory ParentWithNullableTypeEnumTypeTransformer() => _instance ??= const ParentWithNullableTypeEnumTypeTransformer._(); - - const ParentWithNullableTypeEnumTypeTransformer._(); - - String encode(ParentWithNullableTypeEnum data) => data.value; - - /// Decodes a [dynamic value][data] to a ParentWithNullableTypeEnum. - /// - /// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully, - /// then null is returned. However, if [allowNull] is false and the [dynamic value][data] - /// cannot be decoded successfully, then an [UnimplementedError] is thrown. - /// - /// The [allowNull] is very handy when an API changes and a new enum value is added or removed, - /// and users are still using an old app with the old code. - ParentWithNullableTypeEnum? decode(dynamic data, {bool allowNull = true}) { - if (data != null) { - switch (data) { - case r'ChildWithNullable': return ParentWithNullableTypeEnum.childWithNullable; - default: - if (!allowNull) { - throw ArgumentError('Unknown enum value to decode: $data'); - } - } - } - return null; - } - - /// Singleton [ParentWithNullableTypeEnumTypeTransformer] instance. - static ParentWithNullableTypeEnumTypeTransformer? _instance; -} - -