@@ -57,15 +57,17 @@ class ResponseToJsonTranslatorTestRun {
57
57
// input - the input to be passed to the MessageReader,
58
58
// expected - the expected translated json chunks as the input is processed,
59
59
ResponseToJsonTranslatorTestRun (pbutil::TypeResolver* type_resolver,
60
- bool streaming, const std::string& type_url,
61
- const std::string& input,
62
- const std::vector<ExpectedAt>& expected)
60
+ bool streaming, const std::string& type_url,
61
+ const pbutil::JsonPrintOptions& json_print_options,
62
+ const std::string& input,
63
+ const std::vector<ExpectedAt>& expected)
63
64
: input_(input),
64
65
expected_ (expected),
65
66
streaming_(streaming),
66
67
input_stream_(new TestZeroCopyInputStream()),
67
68
translator_(new ResponseToJsonTranslator(
68
- type_resolver, type_url, streaming_, input_stream_.get())),
69
+ type_resolver, type_url, streaming_, input_stream_.get(),
70
+ json_print_options)),
69
71
position_(0 ),
70
72
next_expected_(std::begin(expected_)) {}
71
73
@@ -186,19 +188,21 @@ class ResponseToJsonTranslatorTestCase {
186
188
// input - the input to be passed to the MessageReader,
187
189
// expected - the expected translated json chunks as the input is processed,
188
190
ResponseToJsonTranslatorTestCase (pbutil::TypeResolver* type_resolver,
189
- bool streaming, const std::string& type_url,
190
- std::string input ,
191
- std::vector<ExpectedAt> expected)
191
+ bool streaming, const std::string& type_url,
192
+ const pbutil::JsonPrintOptions& json_print_options ,
193
+ std::string input, std::vector<ExpectedAt> expected)
192
194
: type_resolver_(type_resolver),
193
195
streaming_ (streaming),
194
196
type_url_(type_url),
197
+ json_print_options_(json_print_options),
195
198
input_(std::move(input)),
196
199
expected_(std::move(expected)) {}
197
200
198
201
std::unique_ptr<ResponseToJsonTranslatorTestRun> NewRun () {
199
202
return std::unique_ptr<ResponseToJsonTranslatorTestRun>(
200
203
new ResponseToJsonTranslatorTestRun (type_resolver_, streaming_,
201
- type_url_, input_, expected_));
204
+ type_url_, json_print_options_,
205
+ input_, expected_));
202
206
}
203
207
204
208
// Runs the test for different partitions of the input.
@@ -234,6 +238,7 @@ class ResponseToJsonTranslatorTestCase {
234
238
pbutil::TypeResolver* type_resolver_;
235
239
bool streaming_;
236
240
std::string type_url_;
241
+ pbutil::JsonPrintOptions json_print_options_;
237
242
238
243
// The entire input including message delimiters
239
244
std::string input_;
@@ -261,6 +266,21 @@ class ResponseToJsonTranslatorTest : public ::testing::Test {
261
266
type_url_ = " type.googleapis.com/" + type_name;
262
267
}
263
268
269
+ // Sets json print options type for used in this test. Must be used before
270
+ // Build().
271
+ void SetJsonPrintOptions (const pbutil::JsonPrintOptions& json_print_options) {
272
+ json_print_options_ = json_print_options;
273
+ }
274
+
275
+ // Sets whether always print primitive fields for default values. Must be used
276
+ // before Build(). The default is false.
277
+ void SetJsonAlwaysPrintPrimitiveFields (bool always_print_primitive_fields) {
278
+ pbutil::JsonPrintOptions json_print_options;
279
+ json_print_options.always_print_primitive_fields =
280
+ always_print_primitive_fields;
281
+ SetJsonPrintOptions (json_print_options);
282
+ }
283
+
264
284
// Sets whether this is a streaming call or not. Must be used before Build().
265
285
// The default is non-streaming.
266
286
void SetStreaming (bool streaming) { streaming_ = streaming; }
@@ -285,15 +305,16 @@ class ResponseToJsonTranslatorTest : public ::testing::Test {
285
305
286
306
return std::unique_ptr<ResponseToJsonTranslatorTestCase>(
287
307
new ResponseToJsonTranslatorTestCase (
288
- type_helper_->Resolver (), streaming_, type_url_, std::move (input) ,
289
- std::move (expected)));
308
+ type_helper_->Resolver (), streaming_, type_url_, json_print_options_ ,
309
+ std::move (input), std::move ( expected)));
290
310
}
291
311
292
312
private:
293
313
::google::api::Service service_;
294
314
std::unique_ptr<TypeHelper> type_helper_;
295
315
296
316
std::string type_url_;
317
+ pbutil::JsonPrintOptions json_print_options_;
297
318
bool streaming_;
298
319
299
320
// The entire input
@@ -316,6 +337,20 @@ TEST_F(ResponseToJsonTranslatorTest, Simple) {
316
337
EXPECT_TRUE (tc->Test (4 , 0.5 ));
317
338
}
318
339
340
+ TEST_F (ResponseToJsonTranslatorTest, SimpleAlwaysPrintPrimitiveFields) {
341
+ ASSERT_TRUE (LoadService (" bookstore_service.pb.txt" ));
342
+ SetMessageType (" Shelf" );
343
+ SetJsonAlwaysPrintPrimitiveFields (true );
344
+ AddMessage<Shelf>(R"( name : "" theme : "")" ,
345
+ R"( { "name" : "", "theme" : ""})" );
346
+
347
+ auto tc = Build ();
348
+ EXPECT_TRUE (tc->Test (1 , 1.0 ));
349
+ EXPECT_TRUE (tc->Test (2 , 1.0 ));
350
+ EXPECT_TRUE (tc->Test (3 , 1.0 ));
351
+ EXPECT_TRUE (tc->Test (4 , 0.5 ));
352
+ }
353
+
319
354
TEST_F (ResponseToJsonTranslatorTest, Nested) {
320
355
ASSERT_TRUE (LoadService (" bookstore_service.pb.txt" ));
321
356
SetMessageType (" Book" );
@@ -355,6 +390,46 @@ TEST_F(ResponseToJsonTranslatorTest, Nested) {
355
390
EXPECT_TRUE (tc->Test (3 , 0.2 ));
356
391
}
357
392
393
+ TEST_F (ResponseToJsonTranslatorTest, NestedAlwaysPrintPrimitiveFields) {
394
+ ASSERT_TRUE (LoadService (" bookstore_service.pb.txt" ));
395
+ SetMessageType (" Book" );
396
+ SetJsonAlwaysPrintPrimitiveFields (true );
397
+ AddMessage<Book>(
398
+ R"(
399
+ name : ""
400
+ author : ""
401
+ title : ""
402
+ author_info {
403
+ first_name : ""
404
+ last_name : ""
405
+ bio {
406
+ year_born : 0
407
+ year_died : 0
408
+ text : ""
409
+ }
410
+ }
411
+ )" ,
412
+ R"( {
413
+ "author" : "",
414
+ "name" : "",
415
+ "title" : "",
416
+ "authorInfo" : {
417
+ "firstName" : "",
418
+ "lastName" : "",
419
+ "bio" : {
420
+ "yearBorn" : "0",
421
+ "yearDied" : "0",
422
+ "text" : ""
423
+ }
424
+ }
425
+ })" );
426
+
427
+ auto tc = Build ();
428
+ EXPECT_TRUE (tc->Test (1 , 1.0 ));
429
+ EXPECT_TRUE (tc->Test (2 , 1.0 ));
430
+ EXPECT_TRUE (tc->Test (3 , 0.2 ));
431
+ }
432
+
358
433
TEST_F (ResponseToJsonTranslatorTest, Empty) {
359
434
ASSERT_TRUE (LoadService (" bookstore_service.pb.txt" ));
360
435
SetMessageType (" Shelf" );
@@ -365,6 +440,18 @@ TEST_F(ResponseToJsonTranslatorTest, Empty) {
365
440
EXPECT_TRUE (tc->Test (2 , 1.0 ));
366
441
}
367
442
443
+ TEST_F (ResponseToJsonTranslatorTest, EmptyAlwaysPrintPrimitiveFields) {
444
+ ASSERT_TRUE (LoadService (" bookstore_service.pb.txt" ));
445
+ SetMessageType (" Shelf" );
446
+ SetJsonAlwaysPrintPrimitiveFields (true );
447
+ AddMessage<Shelf>(R"( )" ,
448
+ R"( { "name" : "", "theme" : ""})" );
449
+
450
+ auto tc = Build ();
451
+ EXPECT_TRUE (tc->Test (1 , 1.0 ));
452
+ EXPECT_TRUE (tc->Test (2 , 1.0 ));
453
+ }
454
+
368
455
TEST_F (ResponseToJsonTranslatorTest, DifferentSizes) {
369
456
ASSERT_TRUE (LoadService (" bookstore_service.pb.txt" ));
370
457
SetMessageType (" Shelf" );
0 commit comments