Skip to content

Commit

Permalink
feat: remove unused header content type && add protobuf message in co…
Browse files Browse the repository at this point in the history
…ntent type for proxy tools (#626)

Co-authored-by: jiechic <[email protected]>
  • Loading branch information
jiechic and jiechic authored Oct 8, 2023
1 parent 4f29d29 commit d245f4a
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 95 deletions.
8 changes: 4 additions & 4 deletions example_protobuf/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ 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';

@RestApi(baseUrl: "https://5d42a6e2bc64f90014a56ca0.mockapi.io/api/v1/")
abstract class RestClient {
factory RestClient(Dio dio, {String baseUrl}) = _RestClient;

@GET("/tags")
@POST("/tags")
Future<Result> getProtoBufInt(@Body() Params message);


@GET("/tags1")
@POST("/tags1")
Future<List<int>> getMessage(@Body() Params message);
}
68 changes: 68 additions & 0 deletions example_protobuf/lib/proto/params.pb.dart
Original file line number Diff line number Diff line change
@@ -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<Params> createRepeated() => $pb.PbList<Params>();
@$core.pragma('dart2js:noInline')
static Params getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<Params>(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');
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Generated code. Do not modify.
// source: proto/query.proto
// source: proto/params.proto
//
// @dart = 2.12

Expand Down
27 changes: 27 additions & 0 deletions example_protobuf/lib/proto/params.pbjson.dart
Original file line number Diff line number Diff line change
@@ -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=');

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Generated code. Do not modify.
// source: proto/query.proto
// source: proto/params.proto
//
// @dart = 2.12

Expand All @@ -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';

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Generated code. Do not modify.
// source: proto/query.proto
// source: proto/result.proto
//
// @dart = 2.12

Expand All @@ -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<Params> createRepeated() => $pb.PbList<Params>();
@$core.pragma('dart2js:noInline')
static Params getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<Params>(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,
Expand Down
11 changes: 11 additions & 0 deletions example_protobuf/lib/proto/result.pbenum.dart
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Generated code. Do not modify.
// source: proto/query.proto
// source: proto/result.proto
//
// @dart = 2.12

Expand All @@ -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',
Expand Down
14 changes: 14 additions & 0 deletions example_protobuf/lib/proto/result.pbserver.dart
Original file line number Diff line number Diff line change
@@ -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';

7 changes: 7 additions & 0 deletions example_protobuf/proto/params.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package examples.enumerations;

message Params {
string key = 1;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
syntax = "proto3";

message Params {
string key = 1;
}

message Result {
string value = 1;
}
28 changes: 15 additions & 13 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,17 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
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);
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions generator/test/src/generator_test_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion generator/test/src/query.pb.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Generated code. Do not modify.
// source: proto/query.proto
// source: proto/params.proto
//
// @dart = 2.12

Expand Down

0 comments on commit d245f4a

Please sign in to comment.