Skip to content

Commit

Permalink
fix: Multipart @part body (map) breaks when multiple @path parameter …
Browse files Browse the repository at this point in the history
…is used. (#633)

* feat: added PreventNullToAbsent annotation, which allows sending null values to server.

* fix: Multipart @part body (map) doesn't breaks when multiple @path parameter is used.

* fix: Multipart @part body (map) doesn't breaks when multiple @path parameter is used.

* test: Multipart @part body (map) doesn't breaks when multiple @path parameter is used.
  • Loading branch information
iamriajul authored Oct 28, 2023
1 parent a53b4f0 commit ddcfd26
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
8 changes: 8 additions & 0 deletions example/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ abstract class RestClient {
Future<String> nestedGenericOther(
@Body() ValueWrapper<ValueWrapper<TaskQuery>> request,
);

@MultiPart()
@POST('post/{id}/comments/{commentId}')
Future<String> multipartBodyWithMultiplePathParameter(
@Path("id") String id,
@Path("commentId") String commentId,
@Part() Map<String, dynamic> body,
);
}

@JsonSerializable()
Expand Down
18 changes: 3 additions & 15 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1747,32 +1747,20 @@ ${bodyName.displayName} == null

final parts = _getAnnotations(m, retrofit.Part);
if (parts.isNotEmpty) {
if (m.parameters.length == 1 && m.parameters.first.type.isDartCoreMap) {
if (parts.length == 1 && parts.keys.first.type.isDartCoreMap) {
blocks.add(
declareFinal(dataVar)
.assign(
refer('FormData').newInstanceNamed(
'fromMap',
[CodeExpression(Code(m.parameters.first.displayName))],
),
)
.statement,
);
return;
} else if (m.parameters.length == 2 &&
m.parameters[1].type.isDartCoreMap) {
blocks.add(
declareFinal(dataVar)
.assign(
refer('FormData').newInstanceNamed(
'fromMap',
[CodeExpression(Code(m.parameters[1].displayName))],
[CodeExpression(Code(parts.keys.first.displayName))],
),
)
.statement,
);
return;
}

blocks.add(
declareFinal(dataVar)
.assign(refer('FormData').newInstance([]))
Expand Down
50 changes: 50 additions & 0 deletions generator/test/src/generator_test_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1957,3 +1957,53 @@ abstract class ProtoSupport {
@GET('/')
Future<Result> get(@Body() Params params);
}

@ShouldGenerate(
'''
final _data = FormData.fromMap(body);
''',
contains: true,
)
@RestApi()
abstract class MultipartWithMultplePathParams {
@MultiPart()
@POST('post/{id}/comments/{commentId}')
Future<String> multipartBodyWithMultiplePathParameter(
@Path("id") String id,
@Path("commentId") String commentId,
@Part() Map<String, dynamic> body,
);
}

@ShouldGenerate(
'''
final _data = FormData.fromMap(body);
''',
contains: true,
)
@RestApi()
abstract class MultipartWithSinglePathParams {
@MultiPart()
@POST('post/{id}/comments')
Future<String> multipartBodyWithSinglePathParameter(
@Path("id") String id,
@Part() Map<String, dynamic> body,
);
}

@ShouldGenerate(
'''
final _data = FormData.fromMap(body);
''',
contains: true,
)
@RestApi()
abstract class MultipartWithMultplePathParamsPUT {
@MultiPart()
@PUT('post/{id}/comments/{commentId}')
Future<String> multipartBodyWithMultiplePathParameter(
@Path("id") String id,
@Path("commentId") String commentId,
@Part() Map<String, dynamic> body,
);
}

0 comments on commit ddcfd26

Please sign in to comment.