This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
fix(httpjson): handle message derived query params #1784
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e7a9caa
fix(httpjson): handle message derived query params
diegomarquezp a61065b
fix(protoparser): decompose messages in query prms
diegomarquezp 63e0118
test(serializer): add test for complex msg obj
diegomarquezp 16689a9
fix(format): format ProtoRestSerializer files
diegomarquezp 2edd120
fix(queryparam): use json approach to process msgs
diegomarquezp 08b655b
fix(queryparam): use numeric value for root enums
diegomarquezp 46b8bd7
chore: Refactoring the fix.
blakeli0 471a134
test(queryparam): atomized tests
diegomarquezp f7443b9
test(queryparam): test objects w/ well-known types
diegomarquezp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,9 +31,17 @@ | |
package com.google.api.gax.httpjson; | ||
|
||
import com.google.common.truth.Truth; | ||
import com.google.protobuf.Duration; | ||
import com.google.protobuf.Field; | ||
import com.google.protobuf.Field.Cardinality; | ||
import com.google.protobuf.FieldMask; | ||
import com.google.protobuf.FloatValue; | ||
import com.google.protobuf.Int32Value; | ||
import com.google.protobuf.Option; | ||
import com.google.protobuf.Timestamp; | ||
import com.google.protobuf.TypeRegistry; | ||
import com.google.rpc.RetryInfo; | ||
import com.google.type.Interval; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.Arrays; | ||
|
@@ -53,7 +61,14 @@ public class ProtoRestSerializerTest { | |
|
||
@Before | ||
public void setUp() { | ||
requestSerializer = ProtoRestSerializer.create(); | ||
// tests with Any type messages require corresponding descriptors in the type registry | ||
requestSerializer = | ||
ProtoRestSerializer.create( | ||
TypeRegistry.newBuilder() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check if these descriptors are added to ProtoRestSerializer on generating client libraries? If not, we should added the well knowntypes to the registry as well. |
||
.add(FieldMask.getDescriptor()) | ||
.add(Duration.getDescriptor()) | ||
.build()); | ||
|
||
field = | ||
Field.newBuilder() | ||
.setNumber(2) | ||
|
@@ -163,7 +178,7 @@ public void putPathParam() { | |
} | ||
|
||
@Test | ||
public void putQueryParam() { | ||
public void putQueryParamPrimitive() { | ||
Map<String, List<String>> fields = new HashMap<>(); | ||
requestSerializer.putQueryParam(fields, "optName1", 1); | ||
requestSerializer.putQueryParam(fields, "optName2", 0); | ||
|
@@ -181,6 +196,83 @@ public void putQueryParam() { | |
Truth.assertThat(fields).isEqualTo(expectedFields); | ||
} | ||
|
||
@Test | ||
public void putQueryParamComplexObject() { | ||
Map<String, List<String>> fields = new HashMap<>(); | ||
requestSerializer.putQueryParam(fields, "object", field); | ||
|
||
Map<String, List<String>> expectedFields = new HashMap<>(); | ||
expectedFields.put("object.cardinality", Arrays.asList("1")); | ||
expectedFields.put("object.name", Arrays.asList("field_name1")); | ||
expectedFields.put("object.number", Arrays.asList("2")); | ||
expectedFields.put("object.options.name", Arrays.asList("opt_name1", "opt_name2")); | ||
|
||
Truth.assertThat(fields).isEqualTo(expectedFields); | ||
} | ||
|
||
@Test | ||
public void putQueryParamComplexObjectDuration() { | ||
Map<String, List<String>> fields = new HashMap<>(); | ||
Duration duration = Duration.newBuilder().setSeconds(1).setNanos(1).build(); | ||
RetryInfo input = RetryInfo.newBuilder().setRetryDelay(duration).build(); | ||
requestSerializer.putQueryParam(fields, "retry_info", input); | ||
|
||
Map<String, List<String>> expectedFields = new HashMap<>(); | ||
expectedFields.put("retry_info.retryDelay", Arrays.asList("1.000000001s")); | ||
|
||
Truth.assertThat(fields).isEqualTo(expectedFields); | ||
} | ||
|
||
@Test | ||
public void putQueryParamComplexObjectTimestamp() { | ||
Map<String, List<String>> fields = new HashMap<>(); | ||
Timestamp start = Timestamp.newBuilder().setSeconds(1).setNanos(1).build(); | ||
Timestamp end = Timestamp.newBuilder().setSeconds(2).setNanos(2).build(); | ||
Interval input = Interval.newBuilder().setStartTime(start).setEndTime(end).build(); | ||
|
||
requestSerializer.putQueryParam(fields, "object", input); | ||
|
||
Map<String, List<String>> expectedFields = new HashMap<>(); | ||
expectedFields.put("object.startTime", Arrays.asList("1970-01-01T00:00:01.000000001Z")); | ||
expectedFields.put("object.endTime", Arrays.asList("1970-01-01T00:00:02.000000002Z")); | ||
|
||
Truth.assertThat(fields).isEqualTo(expectedFields); | ||
} | ||
|
||
@Test | ||
public void putQueryParamDuration() { | ||
queryParamHelper(Duration.newBuilder().setSeconds(1).setNanos(1).build(), "1.000000001s"); | ||
} | ||
|
||
@Test | ||
public void putQueryParamTimestamp() { | ||
queryParamHelper( | ||
Timestamp.newBuilder().setSeconds(1).setNanos(1).build(), "1970-01-01T00:00:01.000000001Z"); | ||
} | ||
|
||
@Test | ||
public void putQueryParamFieldMask() { | ||
queryParamHelper(FieldMask.newBuilder().addPaths("a.b").addPaths("c.d").build(), "a.b,c.d"); | ||
} | ||
|
||
@Test | ||
public void putQueryParamInt32Value() { | ||
queryParamHelper(Int32Value.of(1), "1"); | ||
} | ||
|
||
@Test | ||
public void putQueryParamFloatValue() { | ||
queryParamHelper(FloatValue.of(1.1f), "1.1"); | ||
} | ||
|
||
private void queryParamHelper(Object value, String expected) { | ||
Map<String, List<String>> fields = new HashMap<>(); | ||
requestSerializer.putQueryParam(fields, "value", value); | ||
Map<String, List<String>> expectedFields = new HashMap<>(); | ||
expectedFields.put("value", Arrays.asList(expected)); | ||
Truth.assertThat(fields).isEqualTo(expectedFields); | ||
} | ||
|
||
@Test | ||
public void toBody() { | ||
String body = requestSerializer.toBody("bodyField1", field, false); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When working with the pattern:
We can often refactor to:
This eliminates the need to study both implementations of A to determine if they're the same or not, with the added benefit of it being easier to test the single code path rather than both. As the complexity of A increases, this simplification becomes more and more beneficial.