@@ -1138,6 +1138,19 @@ A more full example of generated client (with sub-resource) can is at the bottom
1138
1138
// just store the index of parameter used to create the body, we'll use it later
1139
1139
bodyParameterIdx = paramIdx ;
1140
1140
} else if (param .parameterType == ParameterType .HEADER ) {
1141
+ Type paramType = jandexMethod .parameterType (paramIdx );
1142
+ String effectiveParamTypeStr = paramType .name ().toString ();
1143
+ boolean isOptional = isOptional (paramType , index );
1144
+ if (isOptional ) {
1145
+ effectiveParamTypeStr = DotNames .OBJECT .toString ();
1146
+ if (paramType .kind () == PARAMETERIZED_TYPE ) {
1147
+ Type objectType = paramType .asParameterizedType ().arguments ().get (0 );
1148
+ if ((objectType .kind () == CLASS ) || (objectType .kind () == PARAMETERIZED_TYPE )) {
1149
+ effectiveParamTypeStr = objectType .name ().toString ();
1150
+ }
1151
+ }
1152
+ }
1153
+
1141
1154
// headers are added at the invocation builder level
1142
1155
MethodDescriptor handleHeaderDescriptor = MethodDescriptor .ofMethod (name ,
1143
1156
method .getName () + "$$" + methodIndex + "$$handleHeader$$" + paramIdx ,
@@ -1149,8 +1162,15 @@ A more full example of generated client (with sub-resource) can is at the bottom
1149
1162
AssignableResultHandle invocationBuilderRef = handleHeaderMethod
1150
1163
.createVariable (Invocation .Builder .class );
1151
1164
handleHeaderMethod .assign (invocationBuilderRef , handleHeaderMethod .getMethodParam (0 ));
1165
+ ResultHandle headerValue = handleHeaderMethod .getMethodParam (1 );
1152
1166
addHeaderParam (handleHeaderMethod , invocationBuilderRef , param .name ,
1153
- handleHeaderMethod .getMethodParam (1 ), param .type ,
1167
+ isOptional
1168
+ ? handleHeaderMethod .invokeVirtualMethod (
1169
+ MethodDescriptor .ofMethod (Optional .class , "orElse" , Object .class ,
1170
+ Object .class ),
1171
+ headerValue , handleHeaderMethod .loadNull ())
1172
+ : headerValue ,
1173
+ effectiveParamTypeStr ,
1154
1174
handleHeaderMethod .getThis (),
1155
1175
getGenericTypeFromArray (handleHeaderMethod , methodGenericParametersField , paramIdx ),
1156
1176
getAnnotationsFromArray (handleHeaderMethod , methodParamAnnotationsField , paramIdx ));
@@ -3123,22 +3143,22 @@ private boolean isOptional(Type type, IndexView index) {
3123
3143
}
3124
3144
3125
3145
private void addHeaderParam (BytecodeCreator invoBuilderEnricher , AssignableResultHandle invocationBuilder ,
3126
- String paramName , ResultHandle headerParamHandle , String paramType , ResultHandle client ,
3146
+ String headerName , ResultHandle headerValueHandle , String paramType , ResultHandle client ,
3127
3147
ResultHandle genericType , ResultHandle annotations ) {
3128
3148
3129
- BytecodeCreator notNullValue = invoBuilderEnricher .ifNull (headerParamHandle ).falseBranch ();
3149
+ BytecodeCreator notNullValue = invoBuilderEnricher .ifNull (headerValueHandle ).falseBranch ();
3130
3150
3131
- headerParamHandle = notNullValue .invokeVirtualMethod (
3151
+ headerValueHandle = notNullValue .invokeVirtualMethod (
3132
3152
MethodDescriptor .ofMethod (RestClientBase .class , "convertParam" , Object .class ,
3133
3153
Object .class , Class .class , java .lang .reflect .Type .class , Annotation [].class ),
3134
- client , headerParamHandle ,
3154
+ client , headerValueHandle ,
3135
3155
notNullValue .loadClassFromTCCL (paramType ), genericType , annotations );
3136
3156
3137
3157
notNullValue .assign (invocationBuilder ,
3138
3158
notNullValue .invokeInterfaceMethod (
3139
3159
MethodDescriptor .ofMethod (Invocation .Builder .class , "header" , Invocation .Builder .class ,
3140
3160
String .class , Object .class ),
3141
- invocationBuilder , notNullValue .load (paramName ), headerParamHandle ));
3161
+ invocationBuilder , notNullValue .load (headerName ), headerValueHandle ));
3142
3162
}
3143
3163
3144
3164
private void addPathParam (BytecodeCreator methodCreator , AssignableResultHandle methodTarget ,
0 commit comments