You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The HttpHeaders class won't let me change the "accept" header with either the #put() or #set() methods. This seems to be because it's a special field with its own setter. I'm in a situation where I'm overriding a bunch of headers defined for me in a Map, so it's not at all convenient to be forced to call #setAccept() since I don't know for sure which of the things in that Map have their own special setters, and even if I did I'd have no easy way to convert a map key into a particular method name.
The problem lives in GenericData#put() and GenericData#set(). They both call .getClassInfo().getFieldInfo(keyName) to figure out if the thing being set is a special field on the object, and if so it tries to change the value of the field via reflection instead of putting it into the internal unknownFields map. This is all well and good, but I want to set just one accept header, and that field is a list of them. The result is that my attempt to set the header results in an exception:
java.lang.IllegalArgumentException: Can not set java.util.List field com.google.api.client.http.HttpHeaders.accept to java.lang.String
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:245)
at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:206)
at com.google.api.client.util.GenericData.set(GenericData.java:125)
at com.google.api.client.http.HttpHeaders.set(HttpHeaders.java:175)
<snip>
This is easily worked around for the purpose of HttpHeaders with this if statement:
// This FieldInfo nonsense is to deal with a bug in HttpHeaders.
FieldInfo fieldInfo = httpHeaders.getClassInfo().getFieldInfo(headerName);
if (fieldInfo != null) {
httpHeaders.set(headerName, Lists.newArrayList(headerValue));
} else {
httpHeaders.set(headerName, headerValue);
}
That won't work more generically however, because in the case of HttpHeadersall the fields are guaranteed to be lists (RFC 2616). I don't know what the more generic way to fix this is, but it's was pretty frustrating to me to have to deal with this.
The text was updated successfully, but these errors were encountered:
The
HttpHeaders
class won't let me change the "accept" header with either the#put()
or#set()
methods. This seems to be because it's a special field with its own setter. I'm in a situation where I'm overriding a bunch of headers defined for me in aMap
, so it's not at all convenient to be forced to call#setAccept()
since I don't know for sure which of the things in thatMap
have their own special setters, and even if I did I'd have no easy way to convert a map key into a particular method name.The problem lives in
GenericData#put()
andGenericData#set()
. They both call.getClassInfo().getFieldInfo(keyName)
to figure out if the thing being set is a special field on the object, and if so it tries to change the value of the field via reflection instead of putting it into the internalunknownFields
map. This is all well and good, but I want to set just one accept header, and that field is a list of them. The result is that my attempt to set the header results in an exception:This is easily worked around for the purpose of
HttpHeaders
with this if statement:That won't work more generically however, because in the case of
HttpHeaders
all the fields are guaranteed to be lists (RFC 2616). I don't know what the more generic way to fix this is, but it's was pretty frustrating to me to have to deal with this.The text was updated successfully, but these errors were encountered: