Skip to content

Commit

Permalink
fix #1714: use header name param also when not compiled with `-parame…
Browse files Browse the repository at this point in the history
…ters`
  • Loading branch information
t1 committed Feb 4, 2023
1 parent b1181bd commit 3fc4759
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.smallrye.graphql.client.GraphQLClientException;
import io.smallrye.graphql.client.impl.typesafe.reflection.MethodInvocation;
import io.smallrye.graphql.client.impl.typesafe.reflection.MethodResolver;
import io.smallrye.graphql.client.impl.typesafe.reflection.NamedElement;
import io.smallrye.graphql.client.impl.typesafe.reflection.ParameterInfo;
import io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo;
import io.smallrye.graphql.client.typesafe.api.AuthorizationHeader;
Expand Down Expand Up @@ -86,7 +87,7 @@ private HeaderDescriptor resolve(Header header) {
throw new RuntimeException("Header must have either 'method' XOR 'constant': " + header);
if (header.name().isEmpty())
throw new RuntimeException("Missing header name for constant '" + header.constant() + "'");
return new HeaderDescriptor(header.constant(), header.name(), null);
return new HeaderDescriptor(header.constant(), header.name());
}

private HeaderDescriptor resolveHeaderMethod(Header header) {
Expand All @@ -96,7 +97,7 @@ private HeaderDescriptor resolveHeaderMethod(Header header) {
throw new RuntimeException("referenced header method '" + header.method() + "'" +
" in " + declaringType.getTypeName() + " is not static");
String value = callMethod(method);
return new HeaderDescriptor(value, header.name(), toHeaderName(method));
return new HeaderDescriptor(value, toHeaderName(header, method));
}

private String callMethod(MethodInvocation method) {
Expand All @@ -110,19 +111,16 @@ private String callMethod(MethodInvocation method) {
}
}

private String toHeaderName(MethodInvocation method) {
String name = method.getName();
return method.isRenamed() ? name : camelToKebab(name);
private String toHeaderName(Header header, NamedElement namedElement) {
if (!header.name().isEmpty())
return header.name();
String name = namedElement.getName();
return namedElement.isRenamed() ? name : camelToKebab(name);
}

private HeaderDescriptor resolve(ParameterInfo parameter) {
Header header = parameter.getAnnotations(Header.class)[0];
return new HeaderDescriptor(parameter.getValue(), header.name(), toHeaderName(parameter));
}

private String toHeaderName(ParameterInfo parameter) {
String name = parameter.getName();
return parameter.isRenamed() ? name : camelToKebab(name);
return new HeaderDescriptor(parameter.getValue(), toHeaderName(header, parameter));
}

private static String camelToKebab(String input) {
Expand All @@ -133,9 +131,9 @@ private static class HeaderDescriptor {
private final String name;
private final String value;

public HeaderDescriptor(Object value, String name, String fallbackName) {
public HeaderDescriptor(Object value, String name) {
this.value = (value == null) ? null : value.toString();
this.name = (name.isEmpty()) ? fallbackName : name;
this.name = name;
}

public void apply(Map<String, String> headers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import io.smallrye.graphql.client.core.OperationType;
import io.smallrye.graphql.client.typesafe.api.Multiple;

public class MethodInvocation {
public class MethodInvocation implements NamedElement {
public static MethodInvocation of(Method method, Object... args) {
return new MethodInvocation(new TypeInfo(null, method.getDeclaringClass()), method, args);
}
Expand Down Expand Up @@ -93,7 +93,7 @@ private Optional<String> subscriptionName() {
return Optional.empty();
}

private String getRawName() {
public String getRawName() {
String name = method.getName();
if (name.startsWith("get") && name.length() > 3 && Character.isUpperCase(name.charAt(3)))
return Character.toLowerCase(name.charAt(3)) + name.substring(4);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.smallrye.graphql.client.impl.typesafe.reflection;

public interface NamedElement {
String getName();

String getRawName();

boolean isRenamed();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import io.smallrye.graphql.client.typesafe.api.Header;
import io.smallrye.graphql.client.typesafe.api.NestedParameter;

public class ParameterInfo {
public class ParameterInfo implements NamedElement {
private final MethodInvocation method;
private final Parameter parameter;
private final TypeInfo type;
Expand Down Expand Up @@ -124,6 +124,7 @@ public Object getValue() {
return this.value;
}

@Override
public String getName() {
if (parameter.isAnnotationPresent(Name.class))
return parameter.getAnnotation(Name.class).value();
Expand All @@ -135,10 +136,12 @@ public String getName() {
return getRawName();
}

@Override
public String getRawName() {
return parameter.getName();
}

@Override
public boolean isRenamed() {
return !getName().equals(getRawName());
}
Expand Down

0 comments on commit 3fc4759

Please sign in to comment.