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

HttpHeaders won't let me put/set "accept" #340

Closed
elreydetodo opened this issue Dec 9, 2016 · 1 comment
Closed

HttpHeaders won't let me put/set "accept" #340

elreydetodo opened this issue Dec 9, 2016 · 1 comment
Assignees
Labels
type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@elreydetodo
Copy link

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 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.

@ipince
Copy link

ipince commented Apr 21, 2017

+1

I ran into this as well, with the Authorization header. My use case is the same: I'm copying some headers over from a Map<String, String>.

Nowhere in the documentation does it say that some headers are "reserved" and cannot be overridden with the set() method.

@JustinBeckwith JustinBeckwith added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. priority: p2 Moderately-important priority. Fix may not be included in next release. 🚨 This issue needs some love. and removed status: investigating labels Jun 6, 2018
@chingor13 chingor13 removed the priority: p2 Moderately-important priority. Fix may not be included in next release. label Aug 29, 2018
@JustinBeckwith JustinBeckwith removed the 🚨 This issue needs some love. label Aug 29, 2018
ajaaym added a commit to ajaaym/google-http-java-client that referenced this issue Dec 7, 2018
chingor13 pushed a commit that referenced this issue Dec 10, 2018
* Fix #340 HttpHeaders won't let me put/set "accept"

* fix case of null value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

5 participants