Skip to content

Commit

Permalink
Include content type for JSON parts (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzsonni authored Jul 1, 2024
1 parent 7922ea3 commit 7db7d08
Showing 1 changed file with 49 additions and 10 deletions.
59 changes: 49 additions & 10 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2006,16 +2006,55 @@ ${bodyName.displayName} == null
if (_missingToJson(ele)) {
throw Exception('toJson() method have to add to ${p.type}');
} else {
blocks.add(
refer(dataVar).property('fields').property('add').call([
refer('MapEntry').newInstance([
literal(fieldName),
refer(
"jsonEncode(${p.displayName}${p.type.nullabilitySuffix == NullabilitySuffix.question ? ' ?? <String,dynamic>{}' : ''})",
)
])
]).statement,
);
if (contentType != null) {
final uploadFileInfo = refer('$MultipartFile.fromString').call([
refer(
"jsonEncode(${p.displayName}${p.type.nullabilitySuffix == NullabilitySuffix.question ? ' ?? <String,dynamic>{}' : ''})",
)
], {
'contentType':
refer('MediaType', 'package:http_parser/http_parser.dart')
.property('parse')
.call([literal(contentType)])
});

final optionalFile = m.parameters
.firstWhereOrNull((pp) => pp.displayName == p.displayName)
?.isOptional ??
false;

final returnCode =
refer(dataVar).property('files').property('add').call([
refer('MapEntry')
.newInstance([literal(fieldName), uploadFileInfo])
]).statement;
if (optionalFile) {
final condition =
refer(p.displayName).notEqualTo(literalNull).code;
blocks.addAll(
[
const Code('if('),
condition,
const Code(') {'),
returnCode,
const Code('}')
],
);
} else {
blocks.add(returnCode);
}
} else {
blocks.add(
refer(dataVar).property('fields').property('add').call([
refer('MapEntry').newInstance([
literal(fieldName),
refer(
"jsonEncode(${p.displayName}${p.type.nullabilitySuffix == NullabilitySuffix.question ? ' ?? <String,dynamic>{}' : ''})",
)
])
]).statement,
);
}
}
} else {
blocks.add(
Expand Down

0 comments on commit 7db7d08

Please sign in to comment.