Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix query param serialization for requests with enums #140

Merged
merged 4 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private <I> Request withQuery(Request in, I entity) {
if (entity == null) {
return in;
}
for (GrpcTranscodingQueryParamsSerializer.HeaderEntry e :
for (GrpcTranscodingQueryParamsSerializer.QueryParamPair e :
GrpcTranscodingQueryParamsSerializer.serialize(entity)) {
in.withQueryParam(e.getKey(), e.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
* documentation for gRPC transcoding</a> for more details.
*/
public class GrpcTranscodingQueryParamsSerializer {
public static class HeaderEntry {
public static class QueryParamPair {
private final String key;
private final String value;

public HeaderEntry(String key, String value) {
public QueryParamPair(String key, String value) {
this.key = key;
this.value = value;
}
Expand All @@ -47,25 +47,19 @@ public String getValue() {
* @param o The object to serialize.
* @return A list of query parameter entries compatible with gRPC-transcoding.
*/
public static List<HeaderEntry> serialize(Object o) {
Map<String, Object> flattened = flattenObject(o);
for (Field f : o.getClass().getDeclaredFields()) {
QueryParam queryParam = f.getAnnotation(QueryParam.class);
if (queryParam == null) {
flattened.remove(getFieldName(f));
}
}
public static List<QueryParamPair> serialize(Object o) {
Map<String, Object> flattened = flattenObject(o, true);

List<HeaderEntry> result = new ArrayList<>();
List<QueryParamPair> result = new ArrayList<>();
for (Map.Entry<String, Object> entry : flattened.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof Collection) {
for (Object v : (Collection<Object>) value) {
result.add(new HeaderEntry(key, v.toString()));
result.add(new QueryParamPair(key, v.toString()));
}
} else {
result.add(new HeaderEntry(key, value.toString()));
result.add(new QueryParamPair(key, value.toString()));
}
}
return result;
Expand Down Expand Up @@ -103,24 +97,30 @@ private static String getFieldName(Field f) {
}
}

private static Map<String, Object> flattenObject(Object o) {
private static Map<String, Object> flattenObject(Object o, Boolean onlyAnnotatedFields) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the test is dependent on another PR, should we add a unit tests for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could have... but the other PR should be merged in the next day or two, so I think we can deal with that.

// LinkedHashMap ensures consistent ordering of fields.
Map<String, Object> result = new LinkedHashMap<>();
Field[] fields = o.getClass().getDeclaredFields();
for (Field f : fields) {
if (onlyAnnotatedFields && f.getAnnotation(QueryParam.class) == null) {
continue;
}
f.setAccessible(true);
try {
String name = getFieldName(f);
Object value = f.get(o);
if (value == null) {
continue;
}
// check if object is a primitive type or a collection of some kind
if (primitiveTypes.contains(f.getType()) || Iterable.class.isAssignableFrom(f.getType())) {
// check if object is a primitive type, a collection of some kind, or an enum
Class<?> type = f.getType();
if (primitiveTypes.contains(type)
|| Iterable.class.isAssignableFrom(type)
|| type.isEnum()) {
result.put(name, value);
} else {
// recursively flatten the object
Map<String, Object> flattened = flattenObject(value);
Map<String, Object> flattened = flattenObject(value, false);
for (Map.Entry<String, Object> entry : flattened.entrySet()) {
result.put(name + "." + entry.getKey(), entry.getValue());
}
Expand Down
Loading