diff --git a/example_protobuf/lib/example.dart b/example_protobuf/lib/example.dart index 817a15526..47407f43a 100644 --- a/example_protobuf/lib/example.dart +++ b/example_protobuf/lib/example.dart @@ -2,7 +2,8 @@ import 'package:dio/dio.dart'; import 'package:retrofit/retrofit.dart'; import 'package:flutter/foundation.dart'; -import 'proto/query.pbserver.dart'; +import 'proto/params.pbserver.dart'; +import 'proto/result.pbserver.dart'; part 'example.g.dart'; @@ -10,10 +11,9 @@ part 'example.g.dart'; abstract class RestClient { factory RestClient(Dio dio, {String baseUrl}) = _RestClient; - @GET("/tags") + @POST("/tags") Future getProtoBufInt(@Body() Params message); - - @GET("/tags1") + @POST("/tags1") Future> getMessage(@Body() Params message); } diff --git a/example_protobuf/lib/proto/params.pb.dart b/example_protobuf/lib/proto/params.pb.dart new file mode 100644 index 000000000..9934159bf --- /dev/null +++ b/example_protobuf/lib/proto/params.pb.dart @@ -0,0 +1,68 @@ +// +// Generated code. Do not modify. +// source: proto/params.proto +// +// @dart = 2.12 + +// ignore_for_file: annotate_overrides, camel_case_types, comment_references +// ignore_for_file: constant_identifier_names, library_prefixes +// ignore_for_file: non_constant_identifier_names, prefer_final_fields +// ignore_for_file: unnecessary_import, unnecessary_this, unused_import + +import 'dart:core' as $core; + +import 'package:protobuf/protobuf.dart' as $pb; + +class Params extends $pb.GeneratedMessage { + factory Params({ + $core.String? key, + }) { + final $result = create(); + if (key != null) { + $result.key = key; + } + return $result; + } + Params._() : super(); + factory Params.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r); + factory Params.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r); + + static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'Params', package: const $pb.PackageName(_omitMessageNames ? '' : 'examples.enumerations'), createEmptyInstance: create) + ..aOS(1, _omitFieldNames ? '' : 'key') + ..hasRequiredFields = false + ; + + @$core.Deprecated( + 'Using this can add significant overhead to your binary. ' + 'Use [GeneratedMessageGenericExtensions.deepCopy] instead. ' + 'Will be removed in next major version') + Params clone() => Params()..mergeFromMessage(this); + @$core.Deprecated( + 'Using this can add significant overhead to your binary. ' + 'Use [GeneratedMessageGenericExtensions.rebuild] instead. ' + 'Will be removed in next major version') + Params copyWith(void Function(Params) updates) => super.copyWith((message) => updates(message as Params)) as Params; + + $pb.BuilderInfo get info_ => _i; + + @$core.pragma('dart2js:noInline') + static Params create() => Params._(); + Params createEmptyInstance() => create(); + static $pb.PbList createRepeated() => $pb.PbList(); + @$core.pragma('dart2js:noInline') + static Params getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor(create); + static Params? _defaultInstance; + + @$pb.TagNumber(1) + $core.String get key => $_getSZ(0); + @$pb.TagNumber(1) + set key($core.String v) { $_setString(0, v); } + @$pb.TagNumber(1) + $core.bool hasKey() => $_has(0); + @$pb.TagNumber(1) + void clearKey() => clearField(1); +} + + +const _omitFieldNames = $core.bool.fromEnvironment('protobuf.omit_field_names'); +const _omitMessageNames = $core.bool.fromEnvironment('protobuf.omit_message_names'); diff --git a/example_protobuf/lib/proto/query.pbenum.dart b/example_protobuf/lib/proto/params.pbenum.dart similarity index 91% rename from example_protobuf/lib/proto/query.pbenum.dart rename to example_protobuf/lib/proto/params.pbenum.dart index 3b7684b49..22b016f92 100644 --- a/example_protobuf/lib/proto/query.pbenum.dart +++ b/example_protobuf/lib/proto/params.pbenum.dart @@ -1,6 +1,6 @@ // // Generated code. Do not modify. -// source: proto/query.proto +// source: proto/params.proto // // @dart = 2.12 diff --git a/example_protobuf/lib/proto/params.pbjson.dart b/example_protobuf/lib/proto/params.pbjson.dart new file mode 100644 index 000000000..52468d1e1 --- /dev/null +++ b/example_protobuf/lib/proto/params.pbjson.dart @@ -0,0 +1,27 @@ +// +// Generated code. Do not modify. +// source: proto/params.proto +// +// @dart = 2.12 + +// ignore_for_file: annotate_overrides, camel_case_types, comment_references +// ignore_for_file: constant_identifier_names, library_prefixes +// ignore_for_file: non_constant_identifier_names, prefer_final_fields +// ignore_for_file: unnecessary_import, unnecessary_this, unused_import + +import 'dart:convert' as $convert; +import 'dart:core' as $core; +import 'dart:typed_data' as $typed_data; + +@$core.Deprecated('Use paramsDescriptor instead') +const Params$json = { + '1': 'Params', + '2': [ + {'1': 'key', '3': 1, '4': 1, '5': 9, '10': 'key'}, + ], +}; + +/// Descriptor for `Params`. Decode as a `google.protobuf.DescriptorProto`. +final $typed_data.Uint8List paramsDescriptor = $convert.base64Decode( + 'CgZQYXJhbXMSEAoDa2V5GAEgASgJUgNrZXk='); + diff --git a/example_protobuf/lib/proto/query.pbserver.dart b/example_protobuf/lib/proto/params.pbserver.dart similarity index 87% rename from example_protobuf/lib/proto/query.pbserver.dart rename to example_protobuf/lib/proto/params.pbserver.dart index 667498118..062bb0e56 100644 --- a/example_protobuf/lib/proto/query.pbserver.dart +++ b/example_protobuf/lib/proto/params.pbserver.dart @@ -1,6 +1,6 @@ // // Generated code. Do not modify. -// source: proto/query.proto +// source: proto/params.proto // // @dart = 2.12 @@ -10,5 +10,5 @@ // ignore_for_file: non_constant_identifier_names, prefer_final_fields // ignore_for_file: unnecessary_import, unnecessary_this, unused_import -export 'query.pb.dart'; +export 'params.pb.dart'; diff --git a/example_protobuf/lib/proto/query.pb.dart b/example_protobuf/lib/proto/result.pb.dart similarity index 56% rename from example_protobuf/lib/proto/query.pb.dart rename to example_protobuf/lib/proto/result.pb.dart index 0c4313161..588154cf0 100644 --- a/example_protobuf/lib/proto/query.pb.dart +++ b/example_protobuf/lib/proto/result.pb.dart @@ -1,6 +1,6 @@ // // Generated code. Do not modify. -// source: proto/query.proto +// source: proto/result.proto // // @dart = 2.12 @@ -13,56 +13,6 @@ import 'dart:core' as $core; import 'package:protobuf/protobuf.dart' as $pb; -class Params extends $pb.GeneratedMessage { - factory Params({ - $core.String? key, - }) { - final $result = create(); - if (key != null) { - $result.key = key; - } - return $result; - } - Params._() : super(); - factory Params.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r); - factory Params.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r); - - static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'Params', createEmptyInstance: create) - ..aOS(1, _omitFieldNames ? '' : 'key') - ..hasRequiredFields = false - ; - - @$core.Deprecated( - 'Using this can add significant overhead to your binary. ' - 'Use [GeneratedMessageGenericExtensions.deepCopy] instead. ' - 'Will be removed in next major version') - Params clone() => Params()..mergeFromMessage(this); - @$core.Deprecated( - 'Using this can add significant overhead to your binary. ' - 'Use [GeneratedMessageGenericExtensions.rebuild] instead. ' - 'Will be removed in next major version') - Params copyWith(void Function(Params) updates) => super.copyWith((message) => updates(message as Params)) as Params; - - $pb.BuilderInfo get info_ => _i; - - @$core.pragma('dart2js:noInline') - static Params create() => Params._(); - Params createEmptyInstance() => create(); - static $pb.PbList createRepeated() => $pb.PbList(); - @$core.pragma('dart2js:noInline') - static Params getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor(create); - static Params? _defaultInstance; - - @$pb.TagNumber(1) - $core.String get key => $_getSZ(0); - @$pb.TagNumber(1) - set key($core.String v) { $_setString(0, v); } - @$pb.TagNumber(1) - $core.bool hasKey() => $_has(0); - @$pb.TagNumber(1) - void clearKey() => clearField(1); -} - class Result extends $pb.GeneratedMessage { factory Result({ $core.String? value, diff --git a/example_protobuf/lib/proto/result.pbenum.dart b/example_protobuf/lib/proto/result.pbenum.dart new file mode 100644 index 000000000..0adb6275d --- /dev/null +++ b/example_protobuf/lib/proto/result.pbenum.dart @@ -0,0 +1,11 @@ +// +// Generated code. Do not modify. +// source: proto/result.proto +// +// @dart = 2.12 + +// ignore_for_file: annotate_overrides, camel_case_types, comment_references +// ignore_for_file: constant_identifier_names, library_prefixes +// ignore_for_file: non_constant_identifier_names, prefer_final_fields +// ignore_for_file: unnecessary_import, unnecessary_this, unused_import + diff --git a/example_protobuf/lib/proto/query.pbjson.dart b/example_protobuf/lib/proto/result.pbjson.dart similarity index 67% rename from example_protobuf/lib/proto/query.pbjson.dart rename to example_protobuf/lib/proto/result.pbjson.dart index cb9660e45..ea9ab4d80 100644 --- a/example_protobuf/lib/proto/query.pbjson.dart +++ b/example_protobuf/lib/proto/result.pbjson.dart @@ -1,6 +1,6 @@ // // Generated code. Do not modify. -// source: proto/query.proto +// source: proto/result.proto // // @dart = 2.12 @@ -13,18 +13,6 @@ import 'dart:convert' as $convert; import 'dart:core' as $core; import 'dart:typed_data' as $typed_data; -@$core.Deprecated('Use paramsDescriptor instead') -const Params$json = { - '1': 'Params', - '2': [ - {'1': 'key', '3': 1, '4': 1, '5': 9, '10': 'key'}, - ], -}; - -/// Descriptor for `Params`. Decode as a `google.protobuf.DescriptorProto`. -final $typed_data.Uint8List paramsDescriptor = $convert.base64Decode( - 'CgZQYXJhbXMSEAoDa2V5GAEgASgJUgNrZXk='); - @$core.Deprecated('Use resultDescriptor instead') const Result$json = { '1': 'Result', diff --git a/example_protobuf/lib/proto/result.pbserver.dart b/example_protobuf/lib/proto/result.pbserver.dart new file mode 100644 index 000000000..a26b5e08c --- /dev/null +++ b/example_protobuf/lib/proto/result.pbserver.dart @@ -0,0 +1,14 @@ +// +// Generated code. Do not modify. +// source: proto/result.proto +// +// @dart = 2.12 + +// ignore_for_file: annotate_overrides, camel_case_types, comment_references +// ignore_for_file: constant_identifier_names +// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes +// ignore_for_file: non_constant_identifier_names, prefer_final_fields +// ignore_for_file: unnecessary_import, unnecessary_this, unused_import + +export 'result.pb.dart'; + diff --git a/example_protobuf/proto/params.proto b/example_protobuf/proto/params.proto new file mode 100644 index 000000000..45ef6e68a --- /dev/null +++ b/example_protobuf/proto/params.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; + +package examples.enumerations; + +message Params { + string key = 1; +} \ No newline at end of file diff --git a/example_protobuf/proto/query.proto b/example_protobuf/proto/result.proto similarity index 60% rename from example_protobuf/proto/query.proto rename to example_protobuf/proto/result.proto index 7a23c82f7..81f068105 100644 --- a/example_protobuf/proto/query.proto +++ b/example_protobuf/proto/result.proto @@ -1,9 +1,5 @@ syntax = "proto3"; -message Params { - string key = 1; -} - message Result { string value = 1; } \ No newline at end of file diff --git a/generator/lib/src/generator.dart b/generator/lib/src/generator.dart index 1278cbc4d..2091bfc2c 100644 --- a/generator/lib/src/generator.dart +++ b/generator/lib/src/generator.dart @@ -459,6 +459,17 @@ class RetrofitGenerator extends GeneratorForAnnotation { literal(contentType.peek('mime')?.stringValue); } + /// gen code for request body for content-type on Protobuf body + final annotation = _getAnnotation(m, retrofit.Body); + final bodyName = annotation?.item1; + if (bodyName != null) { + if (const TypeChecker.fromRuntime(GeneratedMessage) + .isAssignableFromType(bodyName.type)) { + extraOptions[_contentType] = literal( + "application/x-protobuf; \${${bodyName.displayName}.info_.qualifiedMessageName == \"\" ? \"\" :\"messageType=\${${bodyName.displayName}.info_.qualifiedMessageName}\"}"); + } + } + extraOptions[_baseUrlVar] = refer(_baseUrlVar); final responseType = _getResponseTypeAnnotation(m); @@ -2055,19 +2066,10 @@ ${bodyName.displayName} == null _typeChecker(GeneratedMessage).isAssignableFromType(returnType)) { headers.removeWhere( (key, value) => "accept".toLowerCase() == key.toLowerCase()); - headers.addAll({"accept": literal("application/x-protobuf")}); - } - - /// gen code for request body for content-type on Protobuf body - final annotation = _getAnnotation(m, retrofit.Body); - final bodyName = annotation?.item1; - if (bodyName != null) { - if (const TypeChecker.fromRuntime(GeneratedMessage) - .isAssignableFromType(bodyName.type)) { - headers.removeWhere( - (key, value) => "content-type".toLowerCase() == key.toLowerCase()); - headers.addAll({"content-type": literal("application/x-protobuf")}); - } + headers.addAll({ + "accept": literal( + "application/x-protobuf; \${${_displayString(returnType)}.getDefault().info_.qualifiedMessageName == \"\" ? \"\" :\"messageType=\${${_displayString(returnType)}.getDefault().info_.qualifiedMessageName}\"}") + }); } return headers; diff --git a/generator/test/src/generator_test_src.dart b/generator/test/src/generator_test_src.dart index 312ac8c80..4b7d5cd6e 100644 --- a/generator/test/src/generator_test_src.dart +++ b/generator/test/src/generator_test_src.dart @@ -1940,15 +1940,16 @@ abstract class CombineBaseUrls { contains: true, ) @ShouldGenerate( - '''r'accept': 'application/x-protobuf''', - contains: true, -) -@ShouldGenerate( - '''r'content-type': 'application/x-protobuf''', + ''' + r'accept': + 'application/x-protobuf; \${Result.getDefault().info_.qualifiedMessageName == "" ? "" : "messageType=\${Result.getDefault().info_.qualifiedMessageName}"}' + ''', contains: true, ) @ShouldGenerate( - '''contentType: 'application/x-protobuf''', + ''' + contentType: + 'application/x-protobuf; \${params.info_.qualifiedMessageName == "" ? "" : "messageType=\${params.info_.qualifiedMessageName}"}\'''', contains: true, ) @RestApi() diff --git a/generator/test/src/query.pb.dart b/generator/test/src/query.pb.dart index 0c4313161..bbab78f9f 100644 --- a/generator/test/src/query.pb.dart +++ b/generator/test/src/query.pb.dart @@ -1,6 +1,6 @@ // // Generated code. Do not modify. -// source: proto/query.proto +// source: proto/params.proto // // @dart = 2.12