Skip to content

Commit

Permalink
Merge pull request #38 from palantir/bugfix/optional-headers
Browse files Browse the repository at this point in the history
Absent optional headers become the empty string
  • Loading branch information
markelliot committed Feb 9, 2016
2 parents bb3efa1 + 9bdf870 commit 31d72b4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.remoting.http;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import feign.Param.Expander;
import java.util.Objects;

/**
* Expands Optional by using the empty string for {@link Optional#absent()} and the {@link Object#toString()} of
* the value otherwise.
*/
public final class GuavaEmptyOptionalExpander implements Expander {

@Override
public String expand(Object value) {
Preconditions.checkArgument(value instanceof Optional, "Value must be an Optional. Was: %s", value.getClass());
Optional<?> optional = (Optional<?>) value;
return optional.isPresent() ? Objects.toString(optional.get()) : "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
import java.util.Objects;

/**
* Expands Optional by using null for Optional.absent and the toString() of the value otherwise.
* Expands Optional by using null for {@link Optional#absent()} and the {@link Object#toString()} of the
* value otherwise.
*/
public final class GuavaOptionalExpander implements Expander {
public final class GuavaNullOptionalExpander implements Expander {

@Override
public String expand(Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@

package com.palantir.remoting.http;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import feign.Contract;
import feign.Feign;
import feign.MethodMetadata;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.HeaderParam;

/**
* Decorates a Contract and uses {@link GuavaOptionalExpander} for any HeaderParam, PathParam or QueryParam parameters.
* Decorates a Contract and uses {@link GuavaNullOptionalExpander} for any PathParam or QueryParam parameters
* and {@link GuavaEmptyOptionalExpander} for any {@link HeaderParam} parameters.
*/
public final class GuavaOptionalAwareContract implements Contract {

Expand All @@ -52,15 +57,27 @@ public List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) {
MethodMetadata md = methodMetadataByConfigKey.get(configKey);
if (md != null) {
Class<?>[] parameterTypes = method.getParameterTypes();
Annotation[][] annotations = method.getParameterAnnotations();
for (int i = 0; i < parameterTypes.length; i++) {
Class<?> cls = parameterTypes[i];
if (cls.equals(Optional.class)) {
md.indexToExpanderClass().put(i, GuavaOptionalExpander.class);
if (FluentIterable.of(annotations[i]).transform(EXTRACT_CLASS).contains(HeaderParam.class)) {
md.indexToExpanderClass().put(i, GuavaEmptyOptionalExpander.class);
} else {
md.indexToExpanderClass().put(i, GuavaNullOptionalExpander.class);
}
}
}
}
}
return mdList;
}

private static final Function<Annotation, Class<?>> EXTRACT_CLASS = new Function<Annotation, Class<?>>() {
@Override
public Class<?> apply(Annotation input) {
return input.annotationType();
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void testStringQuery() throws Exception {
public void testAbsentHeader() throws Exception {
proxy.header(Optional.<String>absent(), "str2");
RecordedRequest takeRequest = server.takeRequest();
assertThat(takeRequest.getHeader("opt"), is("{opt}"));
assertThat(takeRequest.getHeader("opt"), is(""));
assertThat(takeRequest.getHeader("req"), is("str2"));
}

Expand Down

0 comments on commit 31d72b4

Please sign in to comment.