From d584f659edf8604f57f0565ef73cc177b264706e Mon Sep 17 00:00:00 2001 From: chizhanov <5873123+chizhanov@users.noreply.github.com> Date: Fri, 30 May 2025 19:52:51 +0300 Subject: [PATCH 1/2] [dart-dio] Fix compile error in enum properties with "default" values(#20495) --- .../dio/serialization/built_value/class_members.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/class_members.mustache b/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/class_members.mustache index 0df06e1e3073..d3d55fb723f1 100644 --- a/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/class_members.mustache +++ b/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/class_members.mustache @@ -32,7 +32,7 @@ @BuiltValueHook(initializeBuilder: true) static void _defaults({{{classname}}}Builder b) => b{{#vendorExtensions.x-parent-discriminator}}..{{propertyName}}=b.discriminatorValue{{/vendorExtensions.x-parent-discriminator}}{{#vendorExtensions.x-self-and-ancestor-only-props}}{{#defaultValue}} - ..{{{name}}} = {{#isEnum}}{{^isContainer}}const {{{enumName}}}._({{/isContainer}}{{/isEnum}}{{{defaultValue}}}{{#isEnum}}{{^isContainer}}){{/isContainer}}{{/isEnum}}{{/defaultValue}}{{/vendorExtensions.x-self-and-ancestor-only-props}}; + ..{{{name}}} = {{#isEnum}}{{^isContainer}}{{{defaultValue}}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{defaultValue}}}{{/isEnum}}{{/defaultValue}}{{/vendorExtensions.x-self-and-ancestor-only-props}}; {{/vendorExtensions.x-is-parent}} @BuiltValueSerializer(custom: true) static Serializer<{{classname}}> get serializer => _${{classname}}Serializer(); \ No newline at end of file From 24f0a6ac2dd23d282b714a4f8ab87340a45a4b21 Mon Sep 17 00:00:00 2001 From: chizhanov <5873123+chizhanov@users.noreply.github.com> Date: Fri, 11 Jul 2025 12:30:37 +0300 Subject: [PATCH 2/2] Add a test schema with enum properties --- ...ith-fake-endpoints-models-for-testing.yaml | 19 +++ .../.openapi-generator/FILES | 4 + .../README.md | 2 + .../doc/TestEnum.md | 14 ++ .../doc/TestItem.md | 16 +++ .../lib/openapi.dart | 2 + .../lib/src/deserialize.dart | 6 + .../lib/src/model/test_enum.dart | 25 ++++ .../lib/src/model/test_item.dart | 74 ++++++++++ .../test/test_enum_test.dart | 9 ++ .../test/test_item_test.dart | 21 +++ .../.openapi-generator/FILES | 4 + .../petstore_client_lib_fake/README.md | 2 + .../petstore_client_lib_fake/doc/TestEnum.md | 14 ++ .../petstore_client_lib_fake/doc/TestItem.md | 16 +++ .../petstore_client_lib_fake/lib/openapi.dart | 2 + .../lib/src/model/test_enum.dart | 38 ++++++ .../lib/src/model/test_item.dart | 126 ++++++++++++++++++ .../lib/src/serializers.dart | 4 + .../test/test_enum_test.dart | 9 ++ .../test/test_item_test.dart | 21 +++ .../.openapi-generator/FILES | 4 + .../dart2/petstore_client_lib_fake/README.md | 2 + .../petstore_client_lib_fake/doc/TestEnum.md | 14 ++ .../petstore_client_lib_fake/doc/TestItem.md | 16 +++ .../petstore_client_lib_fake/lib/api.dart | 2 + .../lib/api_client.dart | 4 + .../lib/api_helper.dart | 3 + .../lib/model/test_enum.dart | 88 ++++++++++++ .../lib/model/test_item.dart | 126 ++++++++++++++++++ .../test/test_enum_test.dart | 21 +++ .../test/test_item_test.dart | 32 +++++ 32 files changed, 740 insertions(+) create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/TestEnum.md create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/TestItem.md create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/test_enum.dart create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/test_item.dart create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/test/test_enum_test.dart create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/test/test_item_test.dart create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/doc/TestEnum.md create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/doc/TestItem.md create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/test_enum.dart create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/test_item.dart create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/test/test_enum_test.dart create mode 100644 samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/test/test_item_test.dart create mode 100644 samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/doc/TestEnum.md create mode 100644 samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/doc/TestItem.md create mode 100644 samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/test_enum.dart create mode 100644 samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/test_item.dart create mode 100644 samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/test/test_enum_test.dart create mode 100644 samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/test/test_item_test.dart 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 e4957b4e19d1..86872a16c451 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 @@ -2062,3 +2062,22 @@ components: enum: - admin - user + TestEnum: + type: string + enum: + - "" + - "1" + - "2" + title: TestEnum + TestItem: + type: object + required: + - test + properties: + test: + type: integer + title: test + testEmum: + $ref: '#/components/schemas/TestEnum' + default: "" + title: TestItem diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/.openapi-generator/FILES b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/.openapi-generator/FILES index 1a5e5be75940..9792f2e6c77d 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/.openapi-generator/FILES @@ -56,7 +56,9 @@ doc/SingleRefType.md doc/SpecialModelName.md doc/StoreApi.md doc/Tag.md +doc/TestEnum.md doc/TestInlineFreeformAdditionalPropertiesRequest.md +doc/TestItem.md doc/User.md doc/UserApi.md lib/openapi.dart @@ -122,6 +124,8 @@ lib/src/model/read_only_first.dart lib/src/model/single_ref_type.dart lib/src/model/special_model_name.dart lib/src/model/tag.dart +lib/src/model/test_enum.dart lib/src/model/test_inline_freeform_additional_properties_request.dart +lib/src/model/test_item.dart lib/src/model/user.dart pubspec.yaml diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/README.md b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/README.md index af05eec6c1ce..9b34fb73ac1c 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/README.md +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/README.md @@ -164,7 +164,9 @@ Class | Method | HTTP request | Description - [SingleRefType](doc/SingleRefType.md) - [SpecialModelName](doc/SpecialModelName.md) - [Tag](doc/Tag.md) + - [TestEnum](doc/TestEnum.md) - [TestInlineFreeformAdditionalPropertiesRequest](doc/TestInlineFreeformAdditionalPropertiesRequest.md) + - [TestItem](doc/TestItem.md) - [User](doc/User.md) diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/TestEnum.md b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/TestEnum.md new file mode 100644 index 000000000000..cfa8841fa2be --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/TestEnum.md @@ -0,0 +1,14 @@ +# openapi.model.TestEnum + +## Load the model package +```dart +import 'package:openapi/api.dart'; +``` + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/TestItem.md b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/TestItem.md new file mode 100644 index 000000000000..3988d0bc30d2 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/TestItem.md @@ -0,0 +1,16 @@ +# openapi.model.TestItem + +## Load the model package +```dart +import 'package:openapi/api.dart'; +``` + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**test** | **int** | | +**testEmum** | [**TestEnum**](TestEnum.md) | | [optional] + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/openapi.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/openapi.dart index a462009ebf73..bf6f8507b80f 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/openapi.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/openapi.dart @@ -65,6 +65,8 @@ export 'package:openapi/src/model/read_only_first.dart'; export 'package:openapi/src/model/single_ref_type.dart'; export 'package:openapi/src/model/special_model_name.dart'; export 'package:openapi/src/model/tag.dart'; +export 'package:openapi/src/model/test_enum.dart'; export 'package:openapi/src/model/test_inline_freeform_additional_properties_request.dart'; +export 'package:openapi/src/model/test_item.dart'; export 'package:openapi/src/model/user.dart'; diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/deserialize.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/deserialize.dart index af74c7ee5e41..c7c94493e0a6 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/deserialize.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/deserialize.dart @@ -41,6 +41,7 @@ import 'package:openapi/src/model/read_only_first.dart'; import 'package:openapi/src/model/special_model_name.dart'; import 'package:openapi/src/model/tag.dart'; import 'package:openapi/src/model/test_inline_freeform_additional_properties_request.dart'; +import 'package:openapi/src/model/test_item.dart'; import 'package:openapi/src/model/user.dart'; final _regList = RegExp(r'^List<(.*)>$'); @@ -163,8 +164,13 @@ final _regMap = RegExp(r'^Map$'); return SpecialModelName.fromJson(value as Map) as ReturnType; case 'Tag': return Tag.fromJson(value as Map) as ReturnType; + case 'TestEnum': + + case 'TestInlineFreeformAdditionalPropertiesRequest': return TestInlineFreeformAdditionalPropertiesRequest.fromJson(value as Map) as ReturnType; + case 'TestItem': + return TestItem.fromJson(value as Map) as ReturnType; case 'User': return User.fromJson(value as Map) as ReturnType; default: diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/test_enum.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/test_enum.dart new file mode 100644 index 000000000000..ac4d415ae610 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/test_enum.dart @@ -0,0 +1,25 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +// ignore_for_file: unused_element +import 'package:json_annotation/json_annotation.dart'; + + +enum TestEnum { + @JsonValue(r'') + empty(r''), + @JsonValue(r'1') + n1(r'1'), + @JsonValue(r'2') + n2(r'2'), + @JsonValue(r'unknown_default_open_api') + unknownDefaultOpenApi(r'unknown_default_open_api'); + + const TestEnum(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/test_item.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/test_item.dart new file mode 100644 index 000000000000..84e4ee9a79a0 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/test_item.dart @@ -0,0 +1,74 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +// ignore_for_file: unused_element +import 'package:openapi/src/model/test_enum.dart'; +import 'package:json_annotation/json_annotation.dart'; + +part 'test_item.g.dart'; + + +@JsonSerializable( + checked: true, + createToJson: true, + disallowUnrecognizedKeys: false, + explicitToJson: true, +) +class TestItem { + /// Returns a new [TestItem] instance. + TestItem({ + + required this.test, + + this.testEmum, + }); + + @JsonKey( + + name: r'test', + required: true, + includeIfNull: false, + ) + + + final int test; + + + + @JsonKey( + + name: r'testEmum', + required: false, + includeIfNull: false, + unknownEnumValue: TestEnum.unknownDefaultOpenApi, + ) + + + final TestEnum? testEmum; + + + + + + @override + bool operator ==(Object other) => identical(this, other) || other is TestItem && + other.test == test && + other.testEmum == testEmum; + + @override + int get hashCode => + test.hashCode + + testEmum.hashCode; + + factory TestItem.fromJson(Map json) => _$TestItemFromJson(json); + + Map toJson() => _$TestItemToJson(this); + + @override + String toString() { + return toJson().toString(); + } + +} + diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/test/test_enum_test.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/test/test_enum_test.dart new file mode 100644 index 000000000000..79c8b09cb714 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/test/test_enum_test.dart @@ -0,0 +1,9 @@ +import 'package:test/test.dart'; +import 'package:openapi/openapi.dart'; + +// tests for TestEnum +void main() { + + group(TestEnum, () { + }); +} diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/test/test_item_test.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/test/test_item_test.dart new file mode 100644 index 000000000000..079db4d50226 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/test/test_item_test.dart @@ -0,0 +1,21 @@ +import 'package:test/test.dart'; +import 'package:openapi/openapi.dart'; + +// tests for TestItem +void main() { + final TestItem? instance = /* TestItem(...) */ null; + // TODO add properties to the entity + + group(TestItem, () { + // int test + test('to test the property `test`', () async { + // TODO + }); + + // TestEnum testEmum + test('to test the property `testEmum`', () async { + // TODO + }); + + }); +} diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/.openapi-generator/FILES b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/.openapi-generator/FILES index 21ad1188dd90..3e9e6768947c 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/.openapi-generator/FILES @@ -55,7 +55,9 @@ doc/SingleRefType.md doc/SpecialModelName.md doc/StoreApi.md doc/Tag.md +doc/TestEnum.md doc/TestInlineFreeformAdditionalPropertiesRequest.md +doc/TestItem.md doc/User.md doc/UserApi.md lib/openapi.dart @@ -123,7 +125,9 @@ lib/src/model/read_only_first.dart lib/src/model/single_ref_type.dart lib/src/model/special_model_name.dart lib/src/model/tag.dart +lib/src/model/test_enum.dart lib/src/model/test_inline_freeform_additional_properties_request.dart +lib/src/model/test_item.dart lib/src/model/user.dart lib/src/serializers.dart pubspec.yaml diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/README.md b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/README.md index 836e67864d5b..016876cf4040 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/README.md +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/README.md @@ -163,7 +163,9 @@ Class | Method | HTTP request | Description - [SingleRefType](doc/SingleRefType.md) - [SpecialModelName](doc/SpecialModelName.md) - [Tag](doc/Tag.md) + - [TestEnum](doc/TestEnum.md) - [TestInlineFreeformAdditionalPropertiesRequest](doc/TestInlineFreeformAdditionalPropertiesRequest.md) + - [TestItem](doc/TestItem.md) - [User](doc/User.md) diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/doc/TestEnum.md b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/doc/TestEnum.md new file mode 100644 index 000000000000..cfa8841fa2be --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/doc/TestEnum.md @@ -0,0 +1,14 @@ +# openapi.model.TestEnum + +## Load the model package +```dart +import 'package:openapi/api.dart'; +``` + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/doc/TestItem.md b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/doc/TestItem.md new file mode 100644 index 000000000000..3988d0bc30d2 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/doc/TestItem.md @@ -0,0 +1,16 @@ +# openapi.model.TestItem + +## Load the model package +```dart +import 'package:openapi/api.dart'; +``` + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**test** | **int** | | +**testEmum** | [**TestEnum**](TestEnum.md) | | [optional] + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/openapi.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/openapi.dart index 8b407aba3bcc..82f5f57560c8 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/openapi.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/openapi.dart @@ -66,6 +66,8 @@ export 'package:openapi/src/model/read_only_first.dart'; export 'package:openapi/src/model/single_ref_type.dart'; export 'package:openapi/src/model/special_model_name.dart'; export 'package:openapi/src/model/tag.dart'; +export 'package:openapi/src/model/test_enum.dart'; export 'package:openapi/src/model/test_inline_freeform_additional_properties_request.dart'; +export 'package:openapi/src/model/test_item.dart'; export 'package:openapi/src/model/user.dart'; diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/test_enum.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/test_enum.dart new file mode 100644 index 000000000000..834ff705794c --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/test_enum.dart @@ -0,0 +1,38 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +// ignore_for_file: unused_element +import 'package:built_collection/built_collection.dart'; +import 'package:built_value/built_value.dart'; +import 'package:built_value/serializer.dart'; + +part 'test_enum.g.dart'; + +class TestEnum extends EnumClass { + + @BuiltValueEnumConst(wireName: r'') + static const TestEnum empty = _$empty; + @BuiltValueEnumConst(wireName: r'1') + static const TestEnum n1 = _$n1; + @BuiltValueEnumConst(wireName: r'2') + static const TestEnum n2 = _$n2; + @BuiltValueEnumConst(wireName: r'unknown_default_open_api', fallback: true) + static const TestEnum unknownDefaultOpenApi = _$unknownDefaultOpenApi; + + static Serializer get serializer => _$testEnumSerializer; + + const TestEnum._(String name): super(name); + + static BuiltSet get values => _$values; + static TestEnum valueOf(String name) => _$valueOf(name); +} + +/// Optionally, enum_class can generate a mixin to go with your enum for use +/// with Angular. It exposes your enum constants as getters. So, if you mix it +/// in to your Dart component class, the values become available to the +/// corresponding Angular template. +/// +/// Trigger mixin generation by writing a line like this one next to your enum. +abstract class TestEnumMixin = Object with _$TestEnumMixin; + diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/test_item.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/test_item.dart new file mode 100644 index 000000000000..dce8bef5a287 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/test_item.dart @@ -0,0 +1,126 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// + +// ignore_for_file: unused_element +import 'package:openapi/src/model/test_enum.dart'; +import 'package:built_value/built_value.dart'; +import 'package:built_value/serializer.dart'; + +part 'test_item.g.dart'; + +/// TestItem +/// +/// Properties: +/// * [test] +/// * [testEmum] +@BuiltValue() +abstract class TestItem implements Built { + @BuiltValueField(wireName: r'test') + int get test; + + @BuiltValueField(wireName: r'testEmum') + TestEnum? get testEmum; + // enum testEmumEnum { , 1, 2, }; + + TestItem._(); + + factory TestItem([void updates(TestItemBuilder b)]) = _$TestItem; + + @BuiltValueHook(initializeBuilder: true) + static void _defaults(TestItemBuilder b) => b; + + @BuiltValueSerializer(custom: true) + static Serializer get serializer => _$TestItemSerializer(); +} + +class _$TestItemSerializer implements PrimitiveSerializer { + @override + final Iterable types = const [TestItem, _$TestItem]; + + @override + final String wireName = r'TestItem'; + + Iterable _serializeProperties( + Serializers serializers, + TestItem object, { + FullType specifiedType = FullType.unspecified, + }) sync* { + yield r'test'; + yield serializers.serialize( + object.test, + specifiedType: const FullType(int), + ); + if (object.testEmum != null) { + yield r'testEmum'; + yield serializers.serialize( + object.testEmum, + specifiedType: const FullType(TestEnum), + ); + } + } + + @override + Object serialize( + Serializers serializers, + TestItem object, { + FullType specifiedType = FullType.unspecified, + }) { + return _serializeProperties(serializers, object, specifiedType: specifiedType).toList(); + } + + void _deserializeProperties( + Serializers serializers, + Object serialized, { + FullType specifiedType = FullType.unspecified, + required List serializedList, + required TestItemBuilder result, + required List unhandled, + }) { + for (var i = 0; i < serializedList.length; i += 2) { + final key = serializedList[i] as String; + final value = serializedList[i + 1]; + switch (key) { + case r'test': + final valueDes = serializers.deserialize( + value, + specifiedType: const FullType(int), + ) as int; + result.test = valueDes; + break; + case r'testEmum': + final valueDes = serializers.deserialize( + value, + specifiedType: const FullType(TestEnum), + ) as TestEnum; + result.testEmum = valueDes; + break; + default: + unhandled.add(key); + unhandled.add(value); + break; + } + } + } + + @override + TestItem deserialize( + Serializers serializers, + Object serialized, { + FullType specifiedType = FullType.unspecified, + }) { + final result = TestItemBuilder(); + final serializedList = (serialized as Iterable).toList(); + final unhandled = []; + _deserializeProperties( + serializers, + serialized, + specifiedType: specifiedType, + serializedList: serializedList, + unhandled: unhandled, + result: result, + ); + return result.build(); + } +} + diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/serializers.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/serializers.dart index 9ec6472d69eb..badd31ced0de 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/serializers.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/serializers.dart @@ -62,7 +62,9 @@ import 'package:openapi/src/model/read_only_first.dart'; import 'package:openapi/src/model/single_ref_type.dart'; import 'package:openapi/src/model/special_model_name.dart'; import 'package:openapi/src/model/tag.dart'; +import 'package:openapi/src/model/test_enum.dart'; import 'package:openapi/src/model/test_inline_freeform_additional_properties_request.dart'; +import 'package:openapi/src/model/test_item.dart'; import 'package:openapi/src/model/user.dart'; part 'serializers.g.dart'; @@ -116,7 +118,9 @@ part 'serializers.g.dart'; SingleRefType, SpecialModelName, Tag, + TestEnum, TestInlineFreeformAdditionalPropertiesRequest, + TestItem, User, ]) Serializers serializers = (_$serializers.toBuilder() diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/test/test_enum_test.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/test/test_enum_test.dart new file mode 100644 index 000000000000..79c8b09cb714 --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/test/test_enum_test.dart @@ -0,0 +1,9 @@ +import 'package:test/test.dart'; +import 'package:openapi/openapi.dart'; + +// tests for TestEnum +void main() { + + group(TestEnum, () { + }); +} diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/test/test_item_test.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/test/test_item_test.dart new file mode 100644 index 000000000000..a0d333c69efa --- /dev/null +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/test/test_item_test.dart @@ -0,0 +1,21 @@ +import 'package:test/test.dart'; +import 'package:openapi/openapi.dart'; + +// tests for TestItem +void main() { + final instance = TestItemBuilder(); + // TODO add properties to the builder and call build() + + group(TestItem, () { + // int test + test('to test the property `test`', () async { + // TODO + }); + + // TestEnum testEmum + test('to test the property `testEmum`', () async { + // TODO + }); + + }); +} diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/.openapi-generator/FILES b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/.openapi-generator/FILES index fbfe8cdd1809..0318e8e9482a 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/.openapi-generator/FILES @@ -56,7 +56,9 @@ doc/SingleRefType.md doc/SpecialModelName.md doc/StoreApi.md doc/Tag.md +doc/TestEnum.md doc/TestInlineFreeformAdditionalPropertiesRequest.md +doc/TestItem.md doc/User.md doc/UserApi.md git_push.sh @@ -124,6 +126,8 @@ lib/model/read_only_first.dart lib/model/single_ref_type.dart lib/model/special_model_name.dart lib/model/tag.dart +lib/model/test_enum.dart lib/model/test_inline_freeform_additional_properties_request.dart +lib/model/test_item.dart lib/model/user.dart pubspec.yaml diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/README.md b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/README.md index 6d960aed4757..6a7a7e85d05c 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/README.md +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/README.md @@ -157,7 +157,9 @@ Class | Method | HTTP request | Description - [SingleRefType](doc//SingleRefType.md) - [SpecialModelName](doc//SpecialModelName.md) - [Tag](doc//Tag.md) + - [TestEnum](doc//TestEnum.md) - [TestInlineFreeformAdditionalPropertiesRequest](doc//TestInlineFreeformAdditionalPropertiesRequest.md) + - [TestItem](doc//TestItem.md) - [User](doc//User.md) diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/doc/TestEnum.md b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/doc/TestEnum.md new file mode 100644 index 000000000000..cfa8841fa2be --- /dev/null +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/doc/TestEnum.md @@ -0,0 +1,14 @@ +# openapi.model.TestEnum + +## Load the model package +```dart +import 'package:openapi/api.dart'; +``` + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/doc/TestItem.md b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/doc/TestItem.md new file mode 100644 index 000000000000..3988d0bc30d2 --- /dev/null +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/doc/TestItem.md @@ -0,0 +1,16 @@ +# openapi.model.TestItem + +## Load the model package +```dart +import 'package:openapi/api.dart'; +``` + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**test** | **int** | | +**testEmum** | [**TestEnum**](TestEnum.md) | | [optional] + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api.dart index e2a18ff4f827..32a4a03ae640 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api.dart +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api.dart @@ -84,7 +84,9 @@ part 'model/read_only_first.dart'; part 'model/single_ref_type.dart'; part 'model/special_model_name.dart'; part 'model/tag.dart'; +part 'model/test_enum.dart'; part 'model/test_inline_freeform_additional_properties_request.dart'; +part 'model/test_item.dart'; part 'model/user.dart'; diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_client.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_client.dart index b23eea980a86..a220191a9cc4 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_client.dart +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_client.dart @@ -278,8 +278,12 @@ class ApiClient { return SpecialModelName.fromJson(value); case 'Tag': return Tag.fromJson(value); + case 'TestEnum': + return TestEnumTypeTransformer().decode(value); case 'TestInlineFreeformAdditionalPropertiesRequest': return TestInlineFreeformAdditionalPropertiesRequest.fromJson(value); + case 'TestItem': + return TestItem.fromJson(value); case 'User': return User.fromJson(value); default: diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_helper.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_helper.dart index a07a5497a661..d4cebed49204 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_helper.dart +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_helper.dart @@ -73,6 +73,9 @@ String parameterToString(dynamic value) { if (value is SingleRefType) { return SingleRefTypeTypeTransformer().encode(value).toString(); } + if (value is TestEnum) { + return TestEnumTypeTransformer().encode(value).toString(); + } return value.toString(); } diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/test_enum.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/test_enum.dart new file mode 100644 index 000000000000..c88e6cc0be29 --- /dev/null +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/test_enum.dart @@ -0,0 +1,88 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.18 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +part of openapi.api; + + +class TestEnum { + /// Instantiate a new enum with the provided [value]. + const TestEnum._(this.value); + + /// The underlying value of this enum member. + final String value; + + @override + String toString() => value; + + String toJson() => value; + + static const empty = TestEnum._(r''); + static const n1 = TestEnum._(r'1'); + static const n2 = TestEnum._(r'2'); + + /// List of all possible values in this [enum][TestEnum]. + static const values = [ + empty, + n1, + n2, + ]; + + static TestEnum? fromJson(dynamic value) => TestEnumTypeTransformer().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 = TestEnum.fromJson(row); + if (value != null) { + result.add(value); + } + } + } + return result.toList(growable: growable); + } +} + +/// Transformation class that can [encode] an instance of [TestEnum] to String, +/// and [decode] dynamic data back to [TestEnum]. +class TestEnumTypeTransformer { + factory TestEnumTypeTransformer() => _instance ??= const TestEnumTypeTransformer._(); + + const TestEnumTypeTransformer._(); + + String encode(TestEnum data) => data.value; + + /// Decodes a [dynamic value][data] to a TestEnum. + /// + /// 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. + TestEnum? decode(dynamic data, {bool allowNull = true}) { + if (data != null) { + switch (data) { + case r'': return TestEnum.empty; + case r'1': return TestEnum.n1; + case r'2': return TestEnum.n2; + default: + if (!allowNull) { + throw ArgumentError('Unknown enum value to decode: $data'); + } + } + } + return null; + } + + /// Singleton [TestEnumTypeTransformer] instance. + static TestEnumTypeTransformer? _instance; +} + diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/test_item.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/test_item.dart new file mode 100644 index 000000000000..b4b5aef1f60c --- /dev/null +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/test_item.dart @@ -0,0 +1,126 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.18 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +part of openapi.api; + +class TestItem { + /// Returns a new [TestItem] instance. + TestItem({ + required this.test, + this.testEmum, + }); + + int test; + + /// + /// 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. + /// + TestEnum? testEmum; + + @override + bool operator ==(Object other) => identical(this, other) || other is TestItem && + other.test == test && + other.testEmum == testEmum; + + @override + int get hashCode => + // ignore: unnecessary_parenthesis + (test.hashCode) + + (testEmum == null ? 0 : testEmum!.hashCode); + + @override + String toString() => 'TestItem[test=$test, testEmum=$testEmum]'; + + Map toJson() { + final json = {}; + json[r'test'] = this.test; + if (this.testEmum != null) { + json[r'testEmum'] = this.testEmum; + } else { + json[r'testEmum'] = null; + } + return json; + } + + /// Returns a new [TestItem] instance and imports its values from + /// [value] if it's a [Map], null otherwise. + // ignore: prefer_constructors_over_static_methods + static TestItem? fromJson(dynamic value) { + if (value is Map) { + final json = value.cast(); + + // Ensure that the map contains the required keys. + // Note 1: the values aren't checked for validity beyond being non-null. + // Note 2: this code is stripped in release mode! + assert(() { + requiredKeys.forEach((key) { + assert(json.containsKey(key), 'Required key "TestItem[$key]" is missing from JSON.'); + assert(json[key] != null, 'Required key "TestItem[$key]" has a null value in JSON.'); + }); + return true; + }()); + + return TestItem( + test: mapValueOfType(json, r'test')!, + testEmum: TestEnum.fromJson(json[r'testEmum']), + ); + } + return null; + } + + static List listFromJson(dynamic json, {bool growable = false,}) { + final result = []; + if (json is List && json.isNotEmpty) { + for (final row in json) { + final value = TestItem.fromJson(row); + if (value != null) { + result.add(value); + } + } + } + return result.toList(growable: growable); + } + + static Map mapFromJson(dynamic json) { + final map = {}; + if (json is Map && json.isNotEmpty) { + json = json.cast(); // ignore: parameter_assignments + for (final entry in json.entries) { + final value = TestItem.fromJson(entry.value); + if (value != null) { + map[entry.key] = value; + } + } + } + return map; + } + + // maps a json object with a list of TestItem-objects as value to a dart map + static Map> mapListFromJson(dynamic json, {bool growable = false,}) { + final map = >{}; + if (json is Map && json.isNotEmpty) { + // ignore: parameter_assignments + json = json.cast(); + for (final entry in json.entries) { + map[entry.key] = TestItem.listFromJson(entry.value, growable: growable,); + } + } + return map; + } + + /// The list of required keys that must be present in a JSON. + static const requiredKeys = { + 'test', + }; +} + diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/test/test_enum_test.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/test/test_enum_test.dart new file mode 100644 index 000000000000..c8a265eb28c0 --- /dev/null +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/test/test_enum_test.dart @@ -0,0 +1,21 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.18 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +import 'package:openapi/api.dart'; +import 'package:test/test.dart'; + +// tests for TestEnum +void main() { + + group('test TestEnum', () { + + }); + +} diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/test/test_item_test.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/test/test_item_test.dart new file mode 100644 index 000000000000..38c9d87409af --- /dev/null +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/test/test_item_test.dart @@ -0,0 +1,32 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.18 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +import 'package:openapi/api.dart'; +import 'package:test/test.dart'; + +// tests for TestItem +void main() { + // final instance = TestItem(); + + group('test TestItem', () { + // int test + test('to test the property `test`', () async { + // TODO + }); + + // TestEnum testEmum + test('to test the property `testEmum`', () async { + // TODO + }); + + + }); + +}