Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions swagger_parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ swagger_parser:
# Default: false
dart_mappable_convenient_when: false

# Optional (dart_mappable only).
# Set to true to use the standard `toMap` and `fromMap` serialization from dart_mappable.
# The default implementation requires you to rename the serialization methods in the `build.yaml` to
# ensure compatibility between retrofit and dart_mappable
# ```yaml
# global_options:
# dart_mappable_builder:
# runs_before:
# - retrofit_generator
# options:
# renameMethods:
# toJson: toJsonString
# toMap: toJson
# ```
#
# Default: false
use_dart_mappable_naming: false

# DART ONLY
# Optional. Set `true` to use MultipartFile instead of File as argument type for file parameters.
use_multipart_file: false
Expand Down Expand Up @@ -398,6 +416,85 @@ To run the code generation with build_runner, execute the following command:
dart run build_runner build -d
```

### (Only for dart_mappable) Generate files using [dart_mappable](https://pub.dev/packages/build_runner) for retrofit and dart_mappable

While most JSON serializer stick to the convention of naming the serializers `fromJson` and `toJson`, `dart_mappable` names these methods
according to the used data type `Map`. Therefore these serializing methods are called `toMap` and `fromMap`. This conflicted with `retrofit`.

#### Default settings

In the default settings the parameter `use_dart_mappable_naming` is set to `false` in the `swagger_parser.yaml`.
`retrofit` will therefore expect a `fromJson` and `toJson` method to be provided. In order to achieve that, set the following options
in the `build.yaml`:

```yaml
global_options:
dart_mappable_builder:
runs_before:
- retrofit_generator
options:
renameMethods:
toJson: toJsonString
toMap: toJson
```

This will result in the following code usage:

```dart

@MappableClass()
class Foo with FooMappable {
final String name;

const Foo(this.name);

static Foo fromJson(Map<String, dynamic> json);
}

void main() {
final Foo foo = Foo('Dave');
// parse to JSON
final Map<String, dynamic> serializedFoo = foo.toJson();
// parse from JSON
final Foo deserializedFoo = Foo.fromJson(serializedFoo);
}
```

This has been made default as `retrofit` prior to version `retrofit: 4.9.2`, did not support `dart_mappable`.
The `retrofit` package was expecting the `toJson` method being able to handle objects of type
`Map<String, dynamic>`. Therefore the standard naming convention in dart_mappable had to be overriden using the `build.yaml`.
To avoid breaking changes, this behaviour has been made default and may be changed in a future major release.

#### use `dart_mappable` standard naming

In order to use the default namings of `dart_mappable` set the `use_dart_mappable_naming` to `true`.

This will result in the following code usage:

```dart

@MappableClass()
class Foo with FooMappable {
final String name;

const Foo(this.name);

static Foo fromMap(Map<String, dynamic> json);
}

void main() {
final Foo foo = Foo('Dave');
// parse to JSON
final Map<String, dynamic> serializedFoo = foo.toMap();
// parse from JSON
final Foo deserializedFoo = Foo.fromMap(serializedFoo);
}
```

**NOTE:** If you want to migrate existing projects that have been using the renaming options, do not forget to remove these
options from the `build.yaml`.


## Contributing

Contributions are welcome!
Expand Down
20 changes: 20 additions & 0 deletions swagger_parser/lib/src/config/swp_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SWPConfig {
this.useMultipartFile = false,
this.fallbackUnion,
this.dartMappableConvenientWhen = false,
this.useDartMappableNaming = false,
this.excludeTags = const <String>[],
this.includeTags = const <String>[],
this.includePaths,
Expand Down Expand Up @@ -91,6 +92,7 @@ class SWPConfig {
required this.fallbackClient,
required this.mergeOutputs,
required this.dartMappableConvenientWhen,
required this.useDartMappableNaming,
required this.includeIfNull,
required this.inferRequiredFromNullable,
required this.useFlutterCompute,
Expand Down Expand Up @@ -284,6 +286,10 @@ class SWPConfig {
rootConfig?.dartMappableConvenientWhen ??
true;

final useDartMappableNaming =
yamlMap['use_dart_mappable_naming'] as bool? ??
rootConfig?.useDartMappableNaming;

final excludedTagsYaml = yamlMap['exclude_tags'] as YamlList?;
List<String>? excludedTags;
if (excludedTagsYaml != null) {
Expand Down Expand Up @@ -419,6 +425,7 @@ class SWPConfig {
fallbackClient: fallbackClient ?? dc.fallbackClient,
mergeOutputs: mergeOutputs ?? dc.mergeOutputs,
dartMappableConvenientWhen: dartMappableConvenientWhen,
useDartMappableNaming: useDartMappableNaming ?? dc.useDartMappableNaming,
includeIfNull: includeIfNull ?? dc.includeIfNull,
inferRequiredFromNullable:
inferRequiredFromNullable ?? dc.inferRequiredFromNullable,
Expand Down Expand Up @@ -476,6 +483,18 @@ class SWPConfig {
/// Optional. Current available serializers are: json_serializable, freezed, dart_mappable.
final JsonSerializer jsonSerializer;

/// DART ONLY
/// Optional, defaults to false. Set to true to use the standard `toMap` and `fromMap` serialization from
/// dart_mappable. This is a new feature introduced in Retrofit 4.9.2. Prior to this
/// it was required to rename these methods in the build.yaml to `fromJson` and `toJson`
/// to make dart_mappable compatible with retrofit. To avoid breaking changes for existing
/// dart_mappable implementations, this flag must be explicitely set to true
///
/// This flag exists to avoid making dart_mappable serialization the default behavior.
// TODO(carapacik): This flag can be removed in the next major version to make the standard
// dart_mappable serialization the default behavior.
final bool useDartMappableNaming;

/// Optional. Set postfix for client classes and files.
final String? clientPostfix;

Expand Down Expand Up @@ -681,6 +700,7 @@ class SWPConfig {
useMultipartFile: useMultipartFile,
fallbackUnion: fallbackUnion,
dartMappableConvenientWhen: dartMappableConvenientWhen,
useDartMappableNaming: useDartMappableNaming,
mergeOutputs: mergeOutputs,
includeIfNull: includeIfNull,
useFlutterCompute: useFlutterCompute,
Expand Down
13 changes: 13 additions & 0 deletions swagger_parser/lib/src/generator/config/generator_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class GeneratorConfig {
this.useMultipartFile = false,
this.fallbackUnion,
this.dartMappableConvenientWhen = true,
this.useDartMappableNaming = false,
this.mergeOutputs = false,
this.includeIfNull = false,
this.useFlutterCompute = false,
Expand Down Expand Up @@ -142,6 +143,18 @@ class GeneratorConfig {
/// Set 'false' to use only native Dart pattern matching.
final bool dartMappableConvenientWhen;

/// DART ONLY
/// Optional, defaults to false. Set to true to use the standard `toMap` and `fromMap` serialization from
/// dart_mappable. This is a new feature introduced in Retrofit 4.9.2. Prior to this
/// it was required to rename these methods in the build.yaml to `fromJson` and `toJson`
/// to make dart_mappable compatible with retrofit. To avoid breaking changes for existing
/// dart_mappable implementations, this flag must be explicitely set to true
///
/// This flag exists to avoid making dart_mappable serialization the default behavior.
// TODO(carapacik): This flag can be removed in the next major version to make the standard
// dart_mappable serialization the default behavior.
final bool useDartMappableNaming;

/// Optional. Set to true to merge all generated code into a single file.
///
/// This is useful when using swagger_parser together with build_runner, which needs to map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class FillController {
useFreezed3: config.useFreezed3,
useMultipartFile: config.useMultipartFile,
dartMappableConvenientWhen: config.dartMappableConvenientWhen,
useDartMappableNaming: config.useDartMappableNaming,
includeIfNull: config.includeIfNull,
useFlutterCompute: config.useFlutterCompute,
fallbackUnion: config.fallbackUnion,
Expand Down Expand Up @@ -100,6 +101,7 @@ final class FillController {
fileName: fileName,
jsonSerializer: config.jsonSerializer,
generateUrlsConstants: config.generateUrlsConstants,
useDartMappableNaming: config.useDartMappableNaming,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ enum ProgrammingLanguage {
required bool useFreezed3,
required bool useMultipartFile,
required bool dartMappableConvenientWhen,
required bool useDartMappableNaming,
required bool includeIfNull,
required List<FieldParser> fieldParsers,
bool useFlutterCompute = false,
Expand Down Expand Up @@ -97,6 +98,7 @@ enum ProgrammingLanguage {
useMultipartFile: useMultipartFile,
fallbackUnion: fallbackUnion,
dartMappableConvenientWhen: dartMappableConvenientWhen,
useDartMappableNaming: useDartMappableNaming,
useFlutterCompute: useFlutterCompute,
),
};
Expand All @@ -122,6 +124,7 @@ enum ProgrammingLanguage {
required String defaultContentType,
required bool useMultipartFile,
required bool generateUrlsConstants,
bool useDartMappableNaming = false,
bool extrasParameterByDefault = false,
bool dioOptionsParameterByDefault = false,
bool addOpenApiMetadata = false,
Expand All @@ -144,6 +147,7 @@ enum ProgrammingLanguage {
generateUrlsConstants: generateUrlsConstants,
fileName: fileName,
jsonSerializer: jsonSerializer,
useDartMappableNaming: useDartMappableNaming,
),
kotlin =>
kotlinRetrofitClientTemplate(restClient: restClient, name: name),
Expand Down
Loading