Skip to content

Commit

Permalink
Add support for an another json serializer called dart_mappable (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
dagyu authored Dec 6, 2023
1 parent 82947e4 commit 850739a
Show file tree
Hide file tree
Showing 21 changed files with 434 additions and 58 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
- Supports OpenApi v2, v3.0 and v3.1
- Support JSON and YAML format
- Generate REST client files based on Retrofit
- Generate data classes (also on [freezed](https://pub.dev/packages/freezed))
- Generate data classes, using one of the following serializer:
- [json_serializable](https://pub.dev/packages/json_serializable)
- [freezed](https://pub.dev/packages/freezed)
- [dart_mappable](https://pub.dev/packages/dart_mappable)
- Support for multiple languages (Dart, Kotlin)
- Web interface at https://carapacik.github.io/swagger_parser
5 changes: 5 additions & 0 deletions swagger_parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 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).
- Fixed enum generation name that are defined inside an array

## 1.12.2
- Fixes enum duplicate names (#140)[https://github.com/Carapacik/swagger_parser/issues/140]

Expand Down
14 changes: 12 additions & 2 deletions swagger_parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
- Support for generation by link
- Support for multiple schemes
- Generate REST client files based on Retrofit
- Generate data classes (also on [freezed](https://pub.dev/packages/freezed))
- Generate data classes, using one of the following serializer:
- [json_serializable](https://pub.dev/packages/json_serializable)
- [freezed](https://pub.dev/packages/freezed)
- [dart_mappable](https://pub.dev/packages/dart_mappable)
- Support for multiple languages (Dart, Kotlin)
- Web interface at https://carapacik.github.io/swagger_parser

Expand Down Expand Up @@ -144,10 +147,17 @@ swagger_parser:
schemas:
- schema_path: schemas/openapi.json
root_client_name: ApiMicroservice
freezed: true
jsonSerializer: "freezed"
put_in_folder: true
replacement_rules: []
- schema_url: https://petstore.swagger.io/v2/swagger.json
name: pet_service_dart_mappable
jsonSerializer: "dart_mappable"
client_postfix: Service
put_clients_in_folder: true
put_in_folder: true
- schema_url: https://petstore.swagger.io/v2/swagger.json
name: pet_service
client_postfix: Service
Expand Down
2 changes: 2 additions & 0 deletions swagger_parser/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schemas/**
lib/**
7 changes: 7 additions & 0 deletions swagger_parser/example/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ global_options:
json_serializable:
runs_before:
- retrofit_generator
dart_mappable_builder:
runs_before:
- retrofit_generator
options:
renameMethods:
toJson: toJsonString
toMap: toJson
2 changes: 2 additions & 0 deletions swagger_parser/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ environment:
sdk: ^3.0.0

dependencies:
dart_mappable: ^4.0.1
dio: ^5.3.3
freezed_annotation: ^2.4.1
json_annotation: ^4.8.1
Expand All @@ -13,6 +14,7 @@ dependencies:
dev_dependencies:
build_runner: ^2.4.6
carapacik_lints: ^1.4.2
dart_mappable_builder: ^4.0.1
freezed: ^2.4.5
json_serializable: ^6.7.1
retrofit_generator: ^8.0.1
Expand Down
9 changes: 8 additions & 1 deletion swagger_parser/example/swagger_parser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,17 @@ swagger_parser:
schemas:
- schema_path: schemas/openapi.json
root_client_name: ApiMicroservice
freezed: true
jsonSerializer: "freezed"
put_in_folder: true
replacement_rules: []

- schema_url: https://petstore.swagger.io/v2/swagger.json
name: pet_service_dart_mappable
jsonSerializer: "dart_mappable"
client_postfix: Service
put_clients_in_folder: true
put_in_folder: true

- schema_url: https://petstore.swagger.io/v2/swagger.json
name: pet_service
client_postfix: Service
Expand Down
19 changes: 13 additions & 6 deletions swagger_parser/lib/src/config/yaml_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:args/args.dart';
import 'package:collection/collection.dart';
import 'package:yaml/yaml.dart';

import '../generator/models/json_serializer.dart';
import '../generator/models/prefer_schema_source.dart';
import '../generator/models/programming_language.dart';
import '../generator/models/replacement_rule.dart';
Expand All @@ -26,7 +27,7 @@ final class YamlConfig {
this.schemaFromUrlToFile,
this.preferSchemaSource,
this.language,
this.freezed,
this.jsonSerializer,
this.rootClient,
this.rootClientName,
this.exportFile,
Expand Down Expand Up @@ -138,9 +139,15 @@ final class YamlConfig {
}
}

final freezed = yamlConfig['freezed'];
if (freezed is! bool?) {
throw const ConfigException("Config parameter 'freezed' must be bool.");
JsonSerializer? jsonSerializer;
final rawJsonSerializer = yamlConfig['jsonSerializer']?.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)}.",
);
}
}

final rootClient =
Expand Down Expand Up @@ -278,7 +285,7 @@ final class YamlConfig {
schemaFromUrlToFile ?? rootConfig?.schemaFromUrlToFile,
preferSchemaSource: preferSchemaSource ?? rootConfig?.preferSchemaSource,
language: language ?? rootConfig?.language,
freezed: freezed ?? rootConfig?.freezed,
jsonSerializer: jsonSerializer ?? rootConfig?.jsonSerializer,
rootClient: rootClient ?? rootConfig?.rootClient,
rootClientName: rootClientName ?? rootConfig?.rootClientName,
exportFile: exportFile ?? rootConfig?.exportFile,
Expand Down Expand Up @@ -378,7 +385,7 @@ final class YamlConfig {
final bool? schemaFromUrlToFile;
final PreferSchemaSource? preferSchemaSource;
final ProgrammingLanguage? language;
final bool? freezed;
final JsonSerializer? jsonSerializer;
final String? clientPostfix;
final bool? rootClient;
final String? rootClientName;
Expand Down
9 changes: 5 additions & 4 deletions swagger_parser/lib/src/generator/fill_controller.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '../utils/case_utils.dart';
import 'models/generated_file.dart';
import 'models/json_serializer.dart';
import 'models/open_api_info.dart';
import 'models/programming_language.dart';
import 'models/universal_data_class.dart';
Expand All @@ -15,7 +16,7 @@ final class FillController {
String rootClientName = 'RestClient',
String exportFileName = 'export',
bool putClientsInFolder = false,
bool freezed = false,
JsonSerializer jsonSerializer = JsonSerializer.jsonSerializable,
bool enumsToJson = false,
bool unknownEnumValue = true,
bool markFilesAsGenerated = false,
Expand All @@ -26,7 +27,7 @@ final class FillController {
_rootClientName = rootClientName,
_exportFileName = exportFileName,
_putClientsInFolder = putClientsInFolder,
_freezed = freezed,
_jsonSerializer = jsonSerializer,
_enumsToJson = enumsToJson,
_unknownEnumValue = unknownEnumValue,
_markFilesAsGenerated = markFilesAsGenerated,
Expand All @@ -37,7 +38,7 @@ final class FillController {
final String _clientPostfix;
final String _rootClientName;
final String _exportFileName;
final bool _freezed;
final JsonSerializer _jsonSerializer;
final bool _putClientsInFolder;
final bool _enumsToJson;
final bool _unknownEnumValue;
Expand All @@ -51,7 +52,7 @@ final class FillController {
'.${_programmingLanguage.fileExtension}',
contents: _programmingLanguage.dtoFileContent(
dataClass,
freezed: _freezed,
jsonSerializer: _jsonSerializer,
enumsToJson: _enumsToJson,
unknownEnumValue: _unknownEnumValue,
markFilesAsGenerated: _markFilesAsGenerated,
Expand Down
11 changes: 6 additions & 5 deletions swagger_parser/lib/src/generator/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'fill_controller.dart';
import 'generator_exception.dart';
import 'models/generated_file.dart';
import 'models/generation_statistics.dart';
import 'models/json_serializer.dart';
import 'models/open_api_info.dart';
import 'models/prefer_schema_source.dart';
import 'models/programming_language.dart';
Expand All @@ -34,7 +35,7 @@ final class Generator {
PreferSchemaSource? preferSchemeSource,
ProgrammingLanguage? language,
String? name,
bool? freezed,
JsonSerializer? jsonSerializer,
bool? rootClient,
String? clientPostfix,
bool? exportFile,
Expand All @@ -59,7 +60,7 @@ final class Generator {
_outputDirectory = outputDirectory,
_name = name,
_programmingLanguage = language ?? ProgrammingLanguage.dart,
_freezed = freezed ?? false,
_jsonSerializer = jsonSerializer ?? JsonSerializer.jsonSerializable,
_rootClient = rootClient ?? true,
_rootClientName = rootClientName ?? 'RestClient',
_exportFile = exportFile ?? true,
Expand All @@ -86,7 +87,7 @@ final class Generator {
preferSchemeSource: yamlConfig.preferSchemaSource,
language: yamlConfig.language,
name: yamlConfig.name,
freezed: yamlConfig.freezed,
jsonSerializer: yamlConfig.jsonSerializer,
rootClient: yamlConfig.rootClient,
rootClientName: yamlConfig.rootClientName,
exportFile: yamlConfig.exportFile,
Expand Down Expand Up @@ -132,7 +133,7 @@ final class Generator {
final ProgrammingLanguage _programmingLanguage;

/// Use freezed to generate DTOs
final bool _freezed;
final JsonSerializer _jsonSerializer;

/// Generate root client for all Clients
final bool _rootClient;
Expand Down Expand Up @@ -309,7 +310,7 @@ final class Generator {
rootClientName: _rootClientName,
clientPostfix: _clientPostfix,
exportFileName: _name ?? 'export',
freezed: _freezed,
jsonSerializer: _jsonSerializer,
putClientsInFolder: _putClientsInFolder,
enumsToJson: _enumsToJson,
unknownEnumValue: _unknownEnumValue,
Expand Down
18 changes: 18 additions & 0 deletions swagger_parser/lib/src/generator/models/json_serializer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:collection/collection.dart';

enum JsonSerializer {
jsonSerializable('json_serializable'),

freezed('freezed'),

dartMappable('dart_mappable');

const JsonSerializer(this.name);

final String name;

/// Returns [JsonSerializer] from string
static JsonSerializer? fromString(String string) => values.firstWhereOrNull(
(e) => e.name == string,
);
}
36 changes: 22 additions & 14 deletions swagger_parser/lib/src/generator/models/programming_language.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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';
import '../templates/dart_export_file_template.dart';
import '../templates/dart_freezed_dto_template.dart';
Expand All @@ -12,10 +14,8 @@ 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 {
Expand All @@ -38,7 +38,7 @@ enum ProgrammingLanguage {
/// Determines template for generating DTOs by language
String dtoFileContent(
UniversalDataClass dataClass, {
required bool freezed,
required JsonSerializer jsonSerializer,
required bool enumsToJson,
required bool unknownEnumValue,
required bool markFilesAsGenerated,
Expand All @@ -48,7 +48,7 @@ enum ProgrammingLanguage {
if (dataClass is UniversalEnumClass) {
return dartEnumDtoTemplate(
dataClass,
freezed: freezed,
jsonSerializer: jsonSerializer,
enumsToJson: enumsToJson,
unknownEnumValue: unknownEnumValue,
markFileAsGenerated: markFilesAsGenerated,
Expand All @@ -60,16 +60,24 @@ enum ProgrammingLanguage {
markFileAsGenerated: markFilesAsGenerated,
);
}
if (freezed) {
return dartFreezedDtoTemplate(
dataClass,
markFileAsGenerated: markFilesAsGenerated,
);

switch (jsonSerializer) {
case JsonSerializer.freezed:
return dartFreezedDtoTemplate(
dataClass,
markFileAsGenerated: markFilesAsGenerated,
);
case JsonSerializer.jsonSerializable:
return dartJsonSerializableDtoTemplate(
dataClass,
markFileAsGenerated: markFilesAsGenerated,
);
case JsonSerializer.dartMappable:
return dartDartMappableDtoTemplate(
dataClass,
markFileAsGenerated: markFilesAsGenerated,
);
}
return dartJsonSerializableDtoTemplate(
dataClass,
markFileAsGenerated: markFilesAsGenerated,
);
}
case kotlin:
if (dataClass is UniversalEnumClass) {
Expand Down
Loading

0 comments on commit 850739a

Please sign in to comment.