Skip to content

Commit

Permalink
Merge f8956ab into 31db825
Browse files Browse the repository at this point in the history
  • Loading branch information
Vovanella95 authored Mar 8, 2023
2 parents 31db825 + f8956ab commit 64a1a59
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 2.10.2

* Fixed generation `putIfAbsent` in requests for not needed schemas ([#554](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/554))

# 2.10.2

* Fixed generation of enumMaps in case when no models defined ([#540](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/540))

# 2.10.1
Expand Down
30 changes: 27 additions & 3 deletions lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1275,15 +1275,18 @@ String toString() => jsonEncode(this);

final fromJson = generatedFromJson(schema, validatedClassName);

final toJson = generateToJson(schema, validatedClassName);

final createToJson = generateCreateToJson(schema, validatedClassName);

final generatedClass = '''
@JsonSerializable(explicitToJson: true)
@JsonSerializable(explicitToJson: true $createToJson)
class $validatedClassName{
\t$validatedClassName($generatedConstructorProperties);\n
\t$fromJson\n
\t$toJson\n
$generatedProperties
\tstatic const fromJsonFactory = _\$${validatedClassName}FromJson;
\tstatic const toJsonFactory = _\$${validatedClassName}ToJson;
\tMap<String, dynamic> toJson() => _\$${validatedClassName}ToJson(this);
$equalsOverride
Expand All @@ -1301,6 +1304,27 @@ $copyWithMethod
return 'factory $validatedClassName.fromJson(Map<String, dynamic> json) => _\$${validatedClassName}FromJson(json);';
}

String generateToJson(SwaggerSchema schema, String validatedClassName) {
if (options.generateToJsonFor.isEmpty ||
options.generateToJsonFor.contains(validatedClassName)) {
return '''
\tstatic const toJsonFactory = _\$${validatedClassName}ToJson;
\tMap<String, dynamic> toJson() => _\$${validatedClassName}ToJson(this);
''';
}

return '';
}

String generateCreateToJson(SwaggerSchema schema, String validatedClassName) {
if (options.generateToJsonFor.isEmpty ||
options.generateToJsonFor.contains(validatedClassName)) {
return '';
}

return ', createToJson: false';
}

List<String> _getRequired(
SwaggerSchema schema, Map<String, SwaggerSchema> schemas,
[int recursionCount = 5]) {
Expand Down
10 changes: 7 additions & 3 deletions lib/src/code_generators/swagger_requests_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,12 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase {
}
}
} else {
final neededResponse = response.removeListOrStream();
if (!kBasicTypes.contains(neededResponse)) {
results.add(getValidatedClassName(neededResponse));
if (!response.startsWith('$kMap<')) {
final neededResponse = response.removeListOrStream();

if (!kBasicTypes.contains(neededResponse)) {
results.add(getValidatedClassName(neededResponse));
}
}
}
} else if (successResponse?.schema?.properties.isNotEmpty == true) {
Expand Down Expand Up @@ -1173,6 +1176,7 @@ extension on SwaggerRoot {
Map<String, SwaggerSchema> get allSchemas => {
...definitions,
...components?.schemas ?? {},
...components?.responses ?? {},
};
}

Expand Down
4 changes: 4 additions & 0 deletions lib/src/models/generator_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class GeneratorOptions {
this.overrideToString = true,
this.pageWidth,
this.overridenModels = const [],
this.generateToJsonFor = const [],
});

/// Build options from a JSON map.
Expand Down Expand Up @@ -59,6 +60,9 @@ class GeneratorOptions {
@JsonKey(defaultValue: [])
final List<String> overridenModels;

@JsonKey(defaultValue: [])
final List<String> generateToJsonFor;

@JsonKey(defaultValue: [])
final List<String> additionalHeaders;

Expand Down
5 changes: 5 additions & 0 deletions lib/src/models/generator_options.g2.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SwaggerRequestParameter {
@JsonKey(name: 'items')
SwaggerRequestItems? items;

@JsonKey(ignore: true, defaultValue: '')
@JsonKey(defaultValue: '')
String key;

Map<String, dynamic> toJson() => _$SwaggerRequestParameterToJson(this);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: swagger_dart_code_generator

version: 2.10.2
version: 2.10.3

homepage: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator
repository: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator
Expand Down

0 comments on commit 64a1a59

Please sign in to comment.