From 5877f308a913d645c31409c6a163bc555b28c4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=92=E5=B0=BA?= Date: Thu, 9 Nov 2023 10:08:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20pb=20support=20error=20on=20MapSerializa?= =?UTF-8?q?ble=E3=80=81DartJsonMapper=E3=80=81FlutterCompute=20(#636)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: jiechic --- generator/lib/src/generator.dart | 17 ++--- generator/test/src/generator_test_src.dart | 87 +++++++++++++++++++++- 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/generator/lib/src/generator.dart b/generator/lib/src/generator.dart index 081de842b..e4c2ba94e 100644 --- a/generator/lib/src/generator.dart +++ b/generator/lib/src/generator.dart @@ -1571,6 +1571,14 @@ if (T != dynamic && ); break; } + } else if (_typeChecker(GeneratedMessage).isSuperTypeOf(bodyName.type)) { + if (bodyName.type.nullabilitySuffix != NullabilitySuffix.none) { + log.warning( + "GeneratedMessage body ${_displayString(bodyName.type)} can not be nullable."); + } + blocks.add(declareFinal(dataVar) + .assign(refer("${bodyName.displayName}.writeToBuffer()")) + .statement); } else if (bodyTypeElement != null && _typeChecker(File).isExactly(bodyTypeElement)) { blocks.add( @@ -1614,15 +1622,6 @@ if (T != dynamic && ).statement, ); } - } else if (_typeChecker(GeneratedMessage) - .isSuperTypeOf(bodyName.type)) { - if (bodyName.type.nullabilitySuffix != NullabilitySuffix.none) { - log.warning( - "GeneratedMessage body ${_displayString(bodyName.type)} can not be nullable."); - } - blocks.add(declareFinal(dataVar) - .assign(refer("${bodyName.displayName}.writeToBuffer()")) - .statement); } else { if (_missingToJson(ele)) { log.warning( diff --git a/generator/test/src/generator_test_src.dart b/generator/test/src/generator_test_src.dart index 865146615..242ed27bf 100644 --- a/generator/test/src/generator_test_src.dart +++ b/generator/test/src/generator_test_src.dart @@ -1952,8 +1952,93 @@ abstract class CombineBaseUrls { 'application/x-protobuf; \${params.info_.qualifiedMessageName == "" ? "" : "messageType=\${params.info_.qualifiedMessageName}"}\'''', contains: true, ) +@ShouldGenerate( + '''await compute(Result.fromBuffer, _result.data!);''', + contains: true, +) @RestApi() -abstract class ProtoSupport { +abstract class ProtoSupportParserJsonSerializable { + @GET('/') + Future get(@Body() Params params); +} + +@ShouldGenerate( + '''final _data = params.writeToBuffer();''', + contains: true, +) +@ShouldGenerate( + ''' + r'accept': + 'application/x-protobuf; \${Result.getDefault().info_.qualifiedMessageName == "" ? "" : "messageType=\${Result.getDefault().info_.qualifiedMessageName}"}' + ''', + contains: true, +) +@ShouldGenerate( + ''' + contentType: + 'application/x-protobuf; \${params.info_.qualifiedMessageName == "" ? "" : "messageType=\${params.info_.qualifiedMessageName}"}\'''', + contains: true, +) +@ShouldGenerate( + '''await compute(Result.fromBuffer, _result.data!);''', + contains: true, +) +@RestApi(parser: Parser.DartJsonMapper) +abstract class ProtoSupportParserDartJsonMapper { + @GET('/') + Future get(@Body() Params params); +} + +@ShouldGenerate( + '''final _data = params.writeToBuffer();''', + contains: true, +) +@ShouldGenerate( + ''' + r'accept': + 'application/x-protobuf; \${Result.getDefault().info_.qualifiedMessageName == "" ? "" : "messageType=\${Result.getDefault().info_.qualifiedMessageName}"}' + ''', + contains: true, +) +@ShouldGenerate( + ''' + contentType: + 'application/x-protobuf; \${params.info_.qualifiedMessageName == "" ? "" : "messageType=\${params.info_.qualifiedMessageName}"}\'''', + contains: true, +) +@ShouldGenerate( + '''await compute(Result.fromBuffer, _result.data!);''', + contains: true, +) +@RestApi(parser: Parser.MapSerializable) +abstract class ProtoSupportParserMapSerializable { + @GET('/') + Future get(@Body() Params params); +} + +@ShouldGenerate( + '''final _data = params.writeToBuffer();''', + contains: true, +) +@ShouldGenerate( + ''' + r'accept': + 'application/x-protobuf; \${Result.getDefault().info_.qualifiedMessageName == "" ? "" : "messageType=\${Result.getDefault().info_.qualifiedMessageName}"}' + ''', + contains: true, +) +@ShouldGenerate( + ''' + contentType: + 'application/x-protobuf; \${params.info_.qualifiedMessageName == "" ? "" : "messageType=\${params.info_.qualifiedMessageName}"}\'''', + contains: true, +) +@ShouldGenerate( + '''await compute(Result.fromBuffer, _result.data!);''', + contains: true, +) +@RestApi(parser: Parser.FlutterCompute) +abstract class ProtoSupportParserFlutterCompute { @GET('/') Future get(@Body() Params params); }