grpc-rest 0.1.18 → 0.1.20
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +7 -0
- data/Gemfile.lock +1 -1
- data/README.md +24 -1
- data/lib/grpc_rest/version.rb +1 -1
- data/lib/grpc_rest.rb +48 -40
- data/protoc-gen-rails/go.mod +6 -2
- data/protoc-gen-rails/go.sum +14 -0
- data/protoc-gen-rails/google-deps/protoc-gen-openapiv2/options/annotations.proto +44 -0
- data/protoc-gen-rails/google-deps/protoc-gen-openapiv2/options/openapiv2.proto +720 -0
- data/protoc-gen-rails/internal/output.go +21 -7
- data/protoc-gen-rails/internal/parse.go +19 -0
- data/protoc-gen-rails/testdata/base/app/controllers/my_service_controller.rb +4 -4
- data/protoc-gen-rails/testdata/test_service.proto +11 -0
- data/spec/grpc_rest_spec.rb +5 -3
- data/spec/protoc-gen-openapiv2/options/annotations_pb.rb +46 -0
- data/spec/protoc-gen-openapiv2/options/openapiv2_pb.rb +70 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/test_service_pb.rb +2 -1
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b19a325cd920ec6632682c8f91b54e06a09444cece910dbc176e898f8e504cb
|
4
|
+
data.tar.gz: 3fa53b62032088e7e57cae354326188cb93c22312d7dc463111b4d5a6992b68c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 235e5ea946e9e6b10be20485775d0f55f46b0ab1ecb7612f75e98c75be7ff0ca75b438e1649d3abc298196501615425675968cf0605f8968e3771cd8cde897e6
|
7
|
+
data.tar.gz: 10d3caaa87389dca93982af06ce8143e615d0b6b2133f9485c4d51ec6e18b043faaa836960fcc141954e0fac07b644eba50b256062a22f5fed8c5b5a4f04da4a
|
data/CHANGELOG
CHANGED
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## UNRELEASED
|
9
9
|
|
10
|
+
# 0.1.20 - 2024-09-10
|
11
|
+
- Fix: Repeated float values weren't being treated as arrays and were crashing.
|
12
|
+
|
13
|
+
# 0.1.19 - 2024-08-22
|
14
|
+
|
15
|
+
- Add `emit_defaults` extension for JSON responses.
|
16
|
+
|
10
17
|
# 0.1.18 - 2024-08-15
|
11
18
|
- Automatically add prefixes to enum values if not provided
|
12
19
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -130,9 +130,32 @@ end
|
|
130
130
|
|
131
131
|
This gem does not currently support the full path expression capabilities of grpc-gateway or the Google [http proto](https://github.com/googleapis/googleapis/blob/master/google/api/http.proto). It only supports very basic single wildcard globbing (`*`). Contributions are welcome for more complex cases if they are needed.
|
132
132
|
|
133
|
+
## Proto Options
|
134
|
+
|
135
|
+
By default, grpc-rest uses the Protobuf default of omitting empty values on response. You can change this behavior by using an OpenAPI extension with the key `x-grpc-rest-emit-defaults`:
|
136
|
+
|
137
|
+
```protobuf
|
138
|
+
service MyService {
|
139
|
+
rpc Test(MyRequest) returns (MyResponse) {
|
140
|
+
option (google.api.http) = {
|
141
|
+
get: "/test/"
|
142
|
+
};
|
143
|
+
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
|
144
|
+
extensions: {
|
145
|
+
key: 'x-grpc-rest-emit-defaults';
|
146
|
+
value: {
|
147
|
+
bool_value: true;
|
148
|
+
}
|
149
|
+
}
|
150
|
+
};
|
151
|
+
}
|
152
|
+
}
|
153
|
+
```
|
154
|
+
|
155
|
+
|
133
156
|
## To Do
|
134
157
|
|
135
|
-
* Replace Go implementation with Ruby (+ executable)
|
158
|
+
* Replace Go implementation with Ruby (+ executable)
|
136
159
|
|
137
160
|
## Contributing
|
138
161
|
|
data/lib/grpc_rest/version.rb
CHANGED
data/lib/grpc_rest.rb
CHANGED
@@ -63,56 +63,55 @@ module GrpcRest
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
def map_proto_type(
|
66
|
+
def map_proto_type(descriptor, val)
|
67
|
+
if descriptor.subtype.is_a?(Google::Protobuf::EnumDescriptor)
|
68
|
+
return handle_enum_values(descriptor, val)
|
69
|
+
end
|
70
|
+
|
71
|
+
case descriptor.type
|
72
|
+
when *%i(int32 int64 uint32 uint64 sint32 sint64 fixed32 fixed64 sfixed32 sfixed64)
|
73
|
+
return val.to_i
|
74
|
+
when *%i(float double)
|
75
|
+
return val.to_f
|
76
|
+
when :bool
|
77
|
+
return !!val
|
78
|
+
end
|
79
|
+
|
80
|
+
case descriptor.subtype&.name
|
81
|
+
when 'google.protobuf.Struct'
|
82
|
+
return Google::Protobuf::Struct.from_hash(val)
|
83
|
+
when 'google.protobuf.Timestamp'
|
84
|
+
if val.is_a?(Numeric)
|
85
|
+
return Google::Protobuf::Timestamp.from_time(Time.at(val))
|
86
|
+
else
|
87
|
+
return Google::Protobuf::Timestamp.from_time(Time.new(val))
|
88
|
+
end
|
89
|
+
when 'google.protobuf.Value'
|
90
|
+
return Google::Protobuf::Value.from_ruby(val)
|
91
|
+
when 'google.protobuf.ListValue'
|
92
|
+
return Google::Protobuf::ListValue.from_a(val)
|
93
|
+
else
|
94
|
+
return map_proto_record(descriptor.subtype, val)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def map_proto_record(proto, params)
|
67
99
|
proto.to_a.each do |descriptor|
|
68
100
|
field = descriptor.name
|
69
101
|
val = params[field]
|
70
102
|
next if val.nil?
|
71
|
-
if descriptor.subtype.is_a?(Google::Protobuf::EnumDescriptor)
|
72
|
-
if descriptor.label == :repeated
|
73
|
-
params[field] = val.map { |v| handle_enum_values(descriptor, v)}
|
74
|
-
else
|
75
|
-
params[field] = handle_enum_values(descriptor, val)
|
76
|
-
end
|
77
|
-
next
|
78
|
-
end
|
79
|
-
|
80
|
-
case descriptor.type
|
81
|
-
when *%i(int32 int64 uint32 uint64 sint32 sint64 fixed32 fixed64 sfixed32 sfixed64)
|
82
|
-
params[field] = val.to_i
|
83
|
-
when *%i(float double)
|
84
|
-
params[field] = val.to_f
|
85
|
-
when :bool
|
86
|
-
params[field] = !!val
|
87
|
-
end
|
88
103
|
|
89
|
-
|
90
|
-
|
91
|
-
params[field] = Google::Protobuf::Struct.from_hash(val)
|
92
|
-
when 'google.protobuf.Timestamp'
|
93
|
-
if val.is_a?(Numeric)
|
94
|
-
params[field] = Google::Protobuf::Timestamp.from_time(Time.at(val))
|
95
|
-
else
|
96
|
-
params[field] = Google::Protobuf::Timestamp.from_time(Time.new(val))
|
97
|
-
end
|
98
|
-
when 'google.protobuf.Value'
|
99
|
-
params[field] = Google::Protobuf::Value.from_ruby(val)
|
100
|
-
when 'google.protobuf.ListValue'
|
101
|
-
params[field] = Google::Protobuf::ListValue.from_a(val)
|
104
|
+
if descriptor.label == :repeated
|
105
|
+
params[field] = val.map { |v| map_proto_type(descriptor, v) }
|
102
106
|
else
|
103
|
-
|
104
|
-
params[field].each do |item|
|
105
|
-
map_proto_type(descriptor.subtype, item)
|
106
|
-
end
|
107
|
-
else
|
108
|
-
map_proto_type(descriptor.subtype, params[field])
|
109
|
-
end
|
107
|
+
params[field] = map_proto_type(descriptor, val)
|
110
108
|
end
|
111
109
|
end
|
110
|
+
params
|
112
111
|
end
|
113
112
|
|
114
113
|
def init_request(request_class, params)
|
115
|
-
|
114
|
+
map_proto_record(request_class.descriptor, params)
|
116
115
|
request_class.decode_json(JSON.generate(params), ignore_unknown_fields: true)
|
117
116
|
end
|
118
117
|
|
@@ -212,7 +211,7 @@ module GrpcRest
|
|
212
211
|
klass.new.public_send(method, request)
|
213
212
|
end
|
214
213
|
|
215
|
-
def
|
214
|
+
def get_response(service, method, request)
|
216
215
|
if defined?(Gruf)
|
217
216
|
service_obj = service.constantize::Service
|
218
217
|
klass = ::Gruf::Controllers::Base.subclasses.find do |k|
|
@@ -224,6 +223,15 @@ module GrpcRest
|
|
224
223
|
end
|
225
224
|
send_grpc_request(service, method, request)
|
226
225
|
end
|
226
|
+
|
227
|
+
def send_request(service, method, request, options={})
|
228
|
+
response = get_response(service, method, request)
|
229
|
+
if options[:emit_defaults]
|
230
|
+
response.to_json(emit_defaults: true)
|
231
|
+
else
|
232
|
+
response
|
233
|
+
end
|
234
|
+
end
|
227
235
|
end
|
228
236
|
|
229
237
|
end
|
data/protoc-gen-rails/go.mod
CHANGED
@@ -5,17 +5,21 @@ go 1.22.0
|
|
5
5
|
require (
|
6
6
|
github.com/iancoleman/strcase v0.3.0
|
7
7
|
github.com/stretchr/testify v1.8.4
|
8
|
-
google.golang.org/genproto/googleapis/api v0.0.0-
|
9
|
-
google.golang.org/protobuf v1.
|
8
|
+
google.golang.org/genproto/googleapis/api v0.0.0-20240820151423-278611b39280
|
9
|
+
google.golang.org/protobuf v1.34.2
|
10
10
|
)
|
11
11
|
|
12
12
|
require (
|
13
13
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
14
14
|
github.com/google/go-cmp v0.6.0 // indirect
|
15
|
+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
|
15
16
|
github.com/kr/pretty v0.3.1 // indirect
|
16
17
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
17
18
|
github.com/rogpeppe/go-internal v1.12.0 // indirect
|
19
|
+
golang.org/x/text v0.17.0 // indirect
|
18
20
|
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac // indirect
|
21
|
+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240820151423-278611b39280 // indirect
|
22
|
+
google.golang.org/grpc v1.65.0 // indirect
|
19
23
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
20
24
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
21
25
|
)
|
data/protoc-gen-rails/go.sum
CHANGED
@@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|
3
3
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
4
4
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
5
5
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
6
|
+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
|
7
|
+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I=
|
6
8
|
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
|
7
9
|
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
8
10
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
@@ -20,14 +22,26 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
|
|
20
22
|
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
21
23
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
22
24
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
25
|
+
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
26
|
+
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
23
27
|
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac h1:ZL/Teoy/ZGnzyrqK/Optxxp2pmVh+fmJ97slxSRyzUg=
|
24
28
|
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac/go.mod h1:+Rvu7ElI+aLzyDQhpHMFMMltsD6m7nqpuWDd2CwJw3k=
|
25
29
|
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe h1:0poefMBYvYbs7g5UkjS6HcxBPaTRAmznle9jnxYoAI8=
|
26
30
|
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe/go.mod h1:4jWUdICTdgc3Ibxmr8nAJiiLHwQBY0UI0XZcEMaFKaA=
|
27
31
|
google.golang.org/genproto/googleapis/api v0.0.0-20240228224816-df926f6c8641 h1:SO1wX9btGFrwj9EzH3ocqfwiPVOxfv4ggAJajzlHA5s=
|
28
32
|
google.golang.org/genproto/googleapis/api v0.0.0-20240228224816-df926f6c8641/go.mod h1:wLupoVsUfYPgOMwjzhYFbaVklw/INms+dqTp0tc1fv8=
|
33
|
+
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 h1:wKguEg1hsxI2/L3hUYrpo1RVi48K+uTyzKqprwLXsb8=
|
34
|
+
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142/go.mod h1:d6be+8HhtEtucleCbxpPW9PA9XwISACu8nvpPqF0BVo=
|
35
|
+
google.golang.org/genproto/googleapis/api v0.0.0-20240820151423-278611b39280 h1:YDFM9oOjiFhaMAVgbDxfxW+66nRrsvzQzJ51wp3OxC0=
|
36
|
+
google.golang.org/genproto/googleapis/api v0.0.0-20240820151423-278611b39280/go.mod h1:fO8wJzT2zbQbAjbIoos1285VfEIYKDDY+Dt+WpTkh6g=
|
37
|
+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240820151423-278611b39280 h1:XQMA2e105XNlEZ8NRF0HqnUOZzP14sUSsgL09kpdNnU=
|
38
|
+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240820151423-278611b39280/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
|
39
|
+
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
|
40
|
+
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
|
29
41
|
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
|
30
42
|
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
43
|
+
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
44
|
+
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
31
45
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
32
46
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
33
47
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
@@ -0,0 +1,44 @@
|
|
1
|
+
syntax = "proto3";
|
2
|
+
|
3
|
+
package grpc.gateway.protoc_gen_openapiv2.options;
|
4
|
+
|
5
|
+
import "google/protobuf/descriptor.proto";
|
6
|
+
import "protoc-gen-openapiv2/options/openapiv2.proto";
|
7
|
+
|
8
|
+
option go_package = "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options";
|
9
|
+
|
10
|
+
extend google.protobuf.FileOptions {
|
11
|
+
// ID assigned by protobuf-global-extension-registry@google.com for gRPC-Gateway project.
|
12
|
+
//
|
13
|
+
// All IDs are the same, as assigned. It is okay that they are the same, as they extend
|
14
|
+
// different descriptor messages.
|
15
|
+
Swagger openapiv2_swagger = 1042;
|
16
|
+
}
|
17
|
+
extend google.protobuf.MethodOptions {
|
18
|
+
// ID assigned by protobuf-global-extension-registry@google.com for gRPC-Gateway project.
|
19
|
+
//
|
20
|
+
// All IDs are the same, as assigned. It is okay that they are the same, as they extend
|
21
|
+
// different descriptor messages.
|
22
|
+
Operation openapiv2_operation = 1042;
|
23
|
+
}
|
24
|
+
extend google.protobuf.MessageOptions {
|
25
|
+
// ID assigned by protobuf-global-extension-registry@google.com for gRPC-Gateway project.
|
26
|
+
//
|
27
|
+
// All IDs are the same, as assigned. It is okay that they are the same, as they extend
|
28
|
+
// different descriptor messages.
|
29
|
+
Schema openapiv2_schema = 1042;
|
30
|
+
}
|
31
|
+
extend google.protobuf.ServiceOptions {
|
32
|
+
// ID assigned by protobuf-global-extension-registry@google.com for gRPC-Gateway project.
|
33
|
+
//
|
34
|
+
// All IDs are the same, as assigned. It is okay that they are the same, as they extend
|
35
|
+
// different descriptor messages.
|
36
|
+
Tag openapiv2_tag = 1042;
|
37
|
+
}
|
38
|
+
extend google.protobuf.FieldOptions {
|
39
|
+
// ID assigned by protobuf-global-extension-registry@google.com for gRPC-Gateway project.
|
40
|
+
//
|
41
|
+
// All IDs are the same, as assigned. It is okay that they are the same, as they extend
|
42
|
+
// different descriptor messages.
|
43
|
+
JSONSchema openapiv2_field = 1042;
|
44
|
+
}
|