From e49a5796517de1e57ff04c30a8d791f61767f08c Mon Sep 17 00:00:00 2001 From: Ruediger zu Dohna Date: Sat, 4 Feb 2023 06:59:55 +0100 Subject: [PATCH] fix #1714: use header name param also when not compiled with `-parameters` --- .../client/impl/typesafe/HeaderBuilder.java | 24 +++++++++---------- .../typesafe/reflection/MethodInvocation.java | 4 ++-- .../typesafe/reflection/NamedElement.java | 9 +++++++ .../typesafe/reflection/ParameterInfo.java | 5 +++- 4 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/NamedElement.java diff --git a/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/HeaderBuilder.java b/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/HeaderBuilder.java index a05555597..d8955ccb4 100644 --- a/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/HeaderBuilder.java +++ b/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/HeaderBuilder.java @@ -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; @@ -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) { @@ -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) { @@ -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) { @@ -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 headers) { diff --git a/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/MethodInvocation.java b/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/MethodInvocation.java index 7c2294b72..b3602d0c3 100644 --- a/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/MethodInvocation.java +++ b/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/MethodInvocation.java @@ -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); } @@ -93,7 +93,7 @@ private Optional 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); diff --git a/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/NamedElement.java b/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/NamedElement.java new file mode 100644 index 000000000..da32503e3 --- /dev/null +++ b/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/NamedElement.java @@ -0,0 +1,9 @@ +package io.smallrye.graphql.client.impl.typesafe.reflection; + +public interface NamedElement { + String getName(); + + String getRawName(); + + boolean isRenamed(); +} diff --git a/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/ParameterInfo.java b/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/ParameterInfo.java index 9f800e84b..0ed5ecad5 100644 --- a/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/ParameterInfo.java +++ b/client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/reflection/ParameterInfo.java @@ -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; @@ -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(); @@ -135,10 +136,12 @@ public String getName() { return getRawName(); } + @Override public String getRawName() { return parameter.getName(); } + @Override public boolean isRenamed() { return !getName().equals(getRawName()); }