diff --git a/swagger_parser/CHANGELOG.md b/swagger_parser/CHANGELOG.md index 254a39b7..9ace638a 100644 --- a/swagger_parser/CHANGELOG.md +++ b/swagger_parser/CHANGELOG.md @@ -1,6 +1,6 @@ ## 1.13.0 -- Added support for (`dart_mappable`)[https://github.com/schultek/dart_mappable] serializer -- Changed `freezed` schema property to `jsonSerializer`, which can be set to `freezed`, `dart_mappable` or `json_serializable` (default). +- Added support for (`dart_mappable`)[https://pub.dev/packages/dart_mappable] +- Changed `freezed` schema property to `json_serializer`, which can be set to `freezed`, `dart_mappable` or `json_serializable` (default). - Fixed enum generation name that are defined inside an array ## 1.12.2 diff --git a/swagger_parser/README.md b/swagger_parser/README.md index 2680286d..fd966616 100644 --- a/swagger_parser/README.md +++ b/swagger_parser/README.md @@ -31,6 +31,7 @@ In your pubspec.yaml, add the following dependencies: ```yaml dependencies: + # dart_mappable: ^4.0.1 # for dart_mappable # dio: ^5.3.0 # freezed_annotation: ^2.4.1 # for freezed # json_annotation: ^4.8.1 @@ -38,6 +39,7 @@ dependencies: dev_dependencies: # build_runner: ^2.4.6 + # dart_mappable_builder: ^4.0.1 # for dart_mappable # freezed: ^2.4.5 # for freezed # json_serializable: ^6.7.1 # retrofit_generator: ^8.0.4 @@ -55,6 +57,7 @@ swagger_parser: # Sets the OpenApi schema path directory for api definition. schema_path: schemas/openapi.json + # Sets the url of the OpenApi schema. schema_url: https://petstore.swagger.io/v2/swagger.json @@ -69,8 +72,9 @@ swagger_parser: # Current available options are: path, url. prefer_schema_source: url - # Optional (dart only). Set 'true' to generate data classes using freezed package. - freezed: false + # Optional (dart only). + # Current available serializers are: 'json_serializable', 'freezed' and 'dart_mappable'. + json_serializer: json_serializable # Optional (dart only). Set 'true' to generate root client # with interface and all clients instances. diff --git a/swagger_parser/example/swagger_parser.yaml b/swagger_parser/example/swagger_parser.yaml index 187a3836..a58215cb 100644 --- a/swagger_parser/example/swagger_parser.yaml +++ b/swagger_parser/example/swagger_parser.yaml @@ -17,8 +17,9 @@ swagger_parser: # Current available options are: path, url. prefer_schema_source: url - # Optional (dart only). Set 'true' to generate data classes using freezed package. - freezed: false + # Optional (dart only). + # Current available serializers are: 'json_serializable', 'freezed' and 'dart_mappable'. + json_serializer: json_serializable # Optional (dart only). Set 'true' to generate root client # with interface and all clients instances. diff --git a/swagger_parser/lib/src/config/yaml_config.dart b/swagger_parser/lib/src/config/yaml_config.dart index 4c3dbd8f..39e60138 100644 --- a/swagger_parser/lib/src/config/yaml_config.dart +++ b/swagger_parser/lib/src/config/yaml_config.dart @@ -140,12 +140,12 @@ final class YamlConfig { } JsonSerializer? jsonSerializer; - final rawJsonSerializer = yamlConfig['jsonSerializer']?.toString(); + final rawJsonSerializer = yamlConfig['json_serializer']?.toString(); if (rawJsonSerializer != null) { jsonSerializer = JsonSerializer.fromString(rawJsonSerializer); if (jsonSerializer == null) { throw ConfigException( - "'jsonSerializer' field must be contained in ${JsonSerializer.values.map((e) => e.name)}.", + "'json_serializer' field must be contained in ${JsonSerializer.values.map((e) => e.name)}.", ); } } diff --git a/swagger_parser/lib/src/generator/models/programming_language.dart b/swagger_parser/lib/src/generator/models/programming_language.dart index 5aabeb11..178de595 100644 --- a/swagger_parser/lib/src/generator/models/programming_language.dart +++ b/swagger_parser/lib/src/generator/models/programming_language.dart @@ -1,6 +1,5 @@ import 'package:collection/collection.dart'; -import '../../../swagger_parser.dart'; import '../generator_exception.dart'; import '../templates/dart_dart_mappable_dto_template.dart'; import '../templates/dart_enum_dto_template.dart'; @@ -14,8 +13,11 @@ import '../templates/kotlin_enum_dto_template.dart'; import '../templates/kotlin_moshi_dto_template.dart'; import '../templates/kotlin_retrofit_client_template.dart'; import '../templates/kotlin_typedef_template.dart'; +import 'generated_file.dart'; import 'json_serializer.dart'; import 'open_api_info.dart'; +import 'universal_data_class.dart'; +import 'universal_rest_client.dart'; /// Enumerates supported programming languages to determine templates enum ProgrammingLanguage { diff --git a/swagger_parser/lib/src/generator/templates/dart_dart_mappable_dto_template.dart b/swagger_parser/lib/src/generator/templates/dart_dart_mappable_dto_template.dart index b4979939..f426a358 100644 --- a/swagger_parser/lib/src/generator/templates/dart_dart_mappable_dto_template.dart +++ b/swagger_parser/lib/src/generator/templates/dart_dart_mappable_dto_template.dart @@ -1,10 +1,11 @@ import 'package:collection/collection.dart'; -import '../../../swagger_parser.dart'; import '../../utils/case_utils.dart'; import '../../utils/type_utils.dart'; import '../../utils/utils.dart'; import '../models/json_serializer.dart'; +import '../models/programming_language.dart'; +import '../models/universal_data_class.dart'; import '../models/universal_type.dart'; import 'dart_import_dto_template.dart'; @@ -26,7 +27,7 @@ ${indentation(2)}const $className(${getParameters(dataClass)}); ${getFields(dataClass)} -${indentation(2)}static $className fromJson(Map json) => ${className}Mapper.ensureInitialized().decodeMap<${className}>(json); +${indentation(2)}static $className fromJson(Map json) => ${className}Mapper.ensureInitialized().decodeMap<$className>(json); } '''; } diff --git a/swagger_parser/lib/src/generator/templates/dart_enum_dto_template.dart b/swagger_parser/lib/src/generator/templates/dart_enum_dto_template.dart index ea8d73be..8fca2ad6 100644 --- a/swagger_parser/lib/src/generator/templates/dart_enum_dto_template.dart +++ b/swagger_parser/lib/src/generator/templates/dart_enum_dto_template.dart @@ -1,6 +1,5 @@ import 'package:collection/collection.dart'; -import '../../../swagger_parser.dart'; import '../../utils/case_utils.dart'; import '../../utils/type_utils.dart'; import '../../utils/utils.dart'; @@ -35,7 +34,7 @@ String dartEnumDtoTemplate( jsonParam: jsonParam, ), ).join(',\n')}${unknownEnumValue ? ',' : ';'}'; - final unkownEnumValueStr = unknownEnumValue ? _unkownEnumValue() : ''; + final unknownEnumValueStr = unknownEnumValue ? _unkownEnumValue() : ''; final constructorStr = jsonParam ? _constructor(className) : ''; final fromJsonStr = unknownEnumValue ? _fromJson(className, enumClass) : ''; final jsonFieldStr = jsonParam ? _jsonField(enumClass) : ''; @@ -48,7 +47,7 @@ ${generatedFileComment( ${descriptionComment(enumClass.description)}@JsonEnum() enum $className { -$values$unkownEnumValueStr$constructorStr$fromJsonStr$jsonFieldStr$toJsonStr +$values$unknownEnumValueStr$constructorStr$fromJsonStr$jsonFieldStr$toJsonStr } '''; } diff --git a/swagger_parser/lib/swagger_parser.dart b/swagger_parser/lib/swagger_parser.dart index 1169d126..3242c32d 100644 --- a/swagger_parser/lib/swagger_parser.dart +++ b/swagger_parser/lib/swagger_parser.dart @@ -5,6 +5,7 @@ library swagger_parser; export 'src/generator/fill_controller.dart'; export 'src/generator/generator.dart'; export 'src/generator/models/generated_file.dart'; +export 'src/generator/models/json_serializer.dart'; export 'src/generator/models/programming_language.dart'; export 'src/generator/models/replacement_rule.dart'; export 'src/generator/models/universal_data_class.dart'; diff --git a/swagger_parser_pages/lib/content/generator_content.dart b/swagger_parser_pages/lib/content/generator_content.dart index ab54f3bf..a8077e18 100644 --- a/swagger_parser_pages/lib/content/generator_content.dart +++ b/swagger_parser_pages/lib/content/generator_content.dart @@ -15,8 +15,8 @@ class _GeneratorContentState extends State { late final _name = TextEditingController(); late final _rootClientName = TextEditingController(); ProgrammingLanguage _language = ProgrammingLanguage.dart; + JsonSerializer _jsonSerializer = JsonSerializer.jsonSerializable; bool _isYaml = false; - bool _freezed = false; bool _rootClient = true; bool _putClientsInFolder = false; bool _putInFolder = false; @@ -44,8 +44,7 @@ class _GeneratorContentState extends State { Padding( padding: const EdgeInsets.symmetric(horizontal: 20), child: ConstrainedBox( - constraints: - const BoxConstraints(maxWidth: 400, maxHeight: 500), + constraints: const BoxConstraints(maxWidth: 400, maxHeight: 500), child: TextField( decoration: InputDecoration( border: OutlineInputBorder( @@ -72,8 +71,7 @@ class _GeneratorContentState extends State { children: [ const Text( 'Config parameters', - style: - TextStyle(fontSize: 32, fontWeight: FontWeight.w800), + style: TextStyle(fontSize: 32, fontWeight: FontWeight.w800), ), const SizedBox(height: 24), Center( @@ -92,21 +90,22 @@ class _GeneratorContentState extends State { const SizedBox(height: 16), AnimatedCrossFade( duration: const Duration(milliseconds: 600), - crossFadeState: _language == ProgrammingLanguage.dart - ? CrossFadeState.showSecond - : CrossFadeState.showFirst, + crossFadeState: + _language == ProgrammingLanguage.dart ? CrossFadeState.showSecond : CrossFadeState.showFirst, sizeCurve: Curves.fastOutSlowIn, // for correct animation firstChild: Container(), - secondChild: StatefulBuilder( - builder: (context, setState) => CheckboxListTile( - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(16), - ), - title: const Text('Use freezed'), - value: _freezed, - onChanged: (value) => - setState(() => _freezed = value!), + secondChild: Center( + child: DropdownMenu( + label: const Text('Json serializer'), + width: 300, + initialSelection: JsonSerializer.jsonSerializable, + dropdownMenuEntries: JsonSerializer.values + .map( + (e) => DropdownMenuEntry(value: e, label: e.name), + ) + .toList(growable: false), + onSelected: (js) => setState(() => _jsonSerializer = js!), ), ), ), @@ -133,9 +132,8 @@ class _GeneratorContentState extends State { ), AnimatedCrossFade( duration: const Duration(milliseconds: 600), - crossFadeState: _language == ProgrammingLanguage.dart - ? CrossFadeState.showSecond - : CrossFadeState.showFirst, + crossFadeState: + _language == ProgrammingLanguage.dart ? CrossFadeState.showSecond : CrossFadeState.showFirst, sizeCurve: Curves.fastOutSlowIn, firstChild: Container(), secondChild: TextField( @@ -152,9 +150,8 @@ class _GeneratorContentState extends State { ), AnimatedCrossFade( duration: const Duration(milliseconds: 600), - crossFadeState: _language == ProgrammingLanguage.dart - ? CrossFadeState.showSecond - : CrossFadeState.showFirst, + crossFadeState: + _language == ProgrammingLanguage.dart ? CrossFadeState.showSecond : CrossFadeState.showFirst, sizeCurve: Curves.fastOutSlowIn, firstChild: Container(), secondChild: StatefulBuilder( @@ -166,8 +163,7 @@ class _GeneratorContentState extends State { 'Generate root client for REST clients', ), value: _rootClient, - onChanged: (value) => - setState(() => _rootClient = value!), + onChanged: (value) => setState(() => _rootClient = value!), ), ), ), @@ -189,8 +185,7 @@ class _GeneratorContentState extends State { ), title: const Text('Put the all api in its folder'), value: _putInFolder, - onChanged: (value) => - setState(() => _putInFolder = value!), + onChanged: (value) => setState(() => _putInFolder = value!), ), ), StatefulBuilder( @@ -200,8 +195,7 @@ class _GeneratorContentState extends State { ), title: const Text('Put all clients in clients folder'), value: _putClientsInFolder, - onChanged: (value) => - setState(() => _putClientsInFolder = value!), + onChanged: (value) => setState(() => _putClientsInFolder = value!), ), ), StatefulBuilder( @@ -211,8 +205,7 @@ class _GeneratorContentState extends State { ), title: const Text('Squash all clients in one client'), value: _squashClients, - onChanged: (value) => - setState(() => _squashClients = value!), + onChanged: (value) => setState(() => _squashClients = value!), ), ), StatefulBuilder( @@ -222,8 +215,7 @@ class _GeneratorContentState extends State { ), title: const Text('Generate method name from url path'), value: _pathMethodName, - onChanged: (value) => - setState(() => _pathMethodName = value!), + onChanged: (value) => setState(() => _pathMethodName = value!), ), ), StatefulBuilder( @@ -233,15 +225,13 @@ class _GeneratorContentState extends State { ), title: const Text('Mark files as generated'), value: _markFilesAsGenerated, - onChanged: (value) => - setState(() => _markFilesAsGenerated = value!), + onChanged: (value) => setState(() => _markFilesAsGenerated = value!), ), ), AnimatedCrossFade( duration: const Duration(milliseconds: 600), - crossFadeState: _language == ProgrammingLanguage.dart - ? CrossFadeState.showSecond - : CrossFadeState.showFirst, + crossFadeState: + _language == ProgrammingLanguage.dart ? CrossFadeState.showSecond : CrossFadeState.showFirst, sizeCurve: Curves.fastOutSlowIn, // for correct animation firstChild: Container(), @@ -252,8 +242,7 @@ class _GeneratorContentState extends State { ), title: const Text('Generate to json in Enum'), value: _enumsToJson, - onChanged: (value) => - setState(() => _enumsToJson = value!), + onChanged: (value) => setState(() => _enumsToJson = value!), ), ), ), @@ -266,8 +255,7 @@ class _GeneratorContentState extends State { 'Generate enum name with prefix from parent component', ), value: _enumsPrefix, - onChanged: (value) => - setState(() => _enumsPrefix = value!), + onChanged: (value) => setState(() => _enumsPrefix = value!), ), ), const SizedBox(height: 32), @@ -285,7 +273,7 @@ class _GeneratorContentState extends State { clientPostfix: _clientPostfix.text, language: _language, isYaml: _isYaml, - freezed: _freezed, + jsonSerializer: _jsonSerializer, rootClient: _rootClient, rootClientName: _rootClientName.text, name: _name.text, @@ -314,7 +302,7 @@ Future _generateOutputs( required String schema, required String clientPostfix, required ProgrammingLanguage language, - required bool freezed, + required JsonSerializer jsonSerializer, required String name, required bool putInFolder, required bool putClientsInFolder, @@ -333,7 +321,7 @@ Future _generateOutputs( isYaml: isYaml, language: language, outputDirectory: '', - freezed: freezed, + jsonSerializer: jsonSerializer, rootClient: rootClient, rootClientName: rootClientName.trim().isEmpty ? null : rootClientName, clientPostfix: clientPostfix.trim().isEmpty ? null : clientPostfix, diff --git a/swagger_parser_pages/pubspec.yaml b/swagger_parser_pages/pubspec.yaml index aab88ff2..5abc427f 100644 --- a/swagger_parser_pages/pubspec.yaml +++ b/swagger_parser_pages/pubspec.yaml @@ -3,10 +3,10 @@ description: Web interface for swagger_parser publish_to: none version: 1.0.0+1 environment: - sdk: ^3.1.0 + sdk: ^3.2.0 dependencies: - archive: ^3.4.6 + archive: ^3.4.9 flutter: sdk: flutter flutter_web_plugins: @@ -14,10 +14,10 @@ dependencies: swagger_parser: path: ../swagger_parser - url_launcher: ^6.1.14 + url_launcher: ^6.2.1 dev_dependencies: - carapacik_lints: ^1.5.1 + carapacik_lints: ^1.6.0 flutter: uses-material-design: true