|
17 | 17 | import io.clientcore.core.models.binarydata.BinaryData;
|
18 | 18 | import io.clientcore.core.serialization.SerializationFormat;
|
19 | 19 | import io.clientcore.core.utils.CoreUtils;
|
20 |
| - |
| 20 | +import java.io.InputStream; |
| 21 | +import java.lang.reflect.ParameterizedType; |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
21 | 24 | import javax.lang.model.element.TypeElement;
|
22 | 25 | import javax.lang.model.type.ArrayType;
|
23 | 26 | import javax.lang.model.type.DeclaredType;
|
24 | 27 | import javax.lang.model.type.TypeKind;
|
25 | 28 | import javax.lang.model.type.TypeMirror;
|
26 |
| -import java.io.InputStream; |
27 |
| -import java.lang.reflect.ParameterizedType; |
28 |
| -import java.util.List; |
29 |
| -import java.util.stream.Collectors; |
30 | 29 |
|
31 | 30 | /**
|
32 | 31 | * Utility class to generate response body mode assignment and response handling based on the response body mode.
|
@@ -148,24 +147,40 @@ private static void handleRequestReturn(BlockStmt body, TypeMirror returnType, j
|
148 | 147 | addReturnStatement(body, returnIsResponse, "networkResponse.getValue().toStream()");
|
149 | 148 | } else if (TypeUtil.isTypeOrSubTypeOf(entityType, BinaryData.class)) {
|
150 | 149 | // Return type is a BinaryData. Return the network response body.
|
151 |
| - // DO NOT close the network response for this return as it will result in the BinaryData either being |
152 |
| - // closed or invalid when it is returned. |
| 150 | + // DO NOT close the network response for this return as it will result in the BinaryData either being closed or invalid when it is returned. |
153 | 151 | if (returnIsResponse) {
|
| 152 | + if (returnType instanceof DeclaredType) { |
| 153 | + DeclaredType declaredType = (DeclaredType) returnType; |
| 154 | + if (!declaredType.getTypeArguments().isEmpty() |
| 155 | + && ((TypeElement) ((DeclaredType) declaredType.getTypeArguments().get(0)).asElement()) |
| 156 | + .getQualifiedName() |
| 157 | + .contentEquals(List.class.getCanonicalName())) { |
| 158 | + // Response<List<BinaryData>> or other generics |
| 159 | + handleDeclaredTypes(body, returnType, serializationFormatSet, true); |
| 160 | + return; |
| 161 | + } |
| 162 | + } |
| 163 | + // Raw Response or not a DeclaredType |
154 | 164 | body.addStatement(StaticJavaParser.parseStatement("return networkResponse;"));
|
155 | 165 | } else {
|
156 | 166 | body.addStatement(StaticJavaParser.parseStatement("return networkResponse.getValue();"));
|
157 | 167 | }
|
158 | 168 | } else {
|
159 | 169 | // Fallback to a generalized code path that handles declared types as the entity, which uses deserialization
|
160 | 170 | // to create the return.
|
161 |
| - String typeCast = determineTypeCast(returnType, body); |
| 171 | + handleDeclaredTypes(body, returnType, serializationFormatSet, returnIsResponse); |
| 172 | + } |
| 173 | + } |
162 | 174 |
|
163 |
| - // Initialize the variable that will be used in the return statement. |
164 |
| - body.addStatement(StaticJavaParser.parseStatement(typeCast + " deserializedResult;")); |
165 |
| - handleDeclaredTypeResponse(body, (DeclaredType) returnType, serializationFormatSet, typeCast); |
| 175 | + private static void handleDeclaredTypes(BlockStmt body, TypeMirror returnType, boolean serializationFormatSet, |
| 176 | + boolean returnIsResponse) { |
| 177 | + String typeCast = determineTypeCast(returnType, body); |
166 | 178 |
|
167 |
| - addReturnStatement(body, returnIsResponse, "deserializedResult"); |
168 |
| - } |
| 179 | + // Initialize the variable that will be used in the return statement. |
| 180 | + body.addStatement(StaticJavaParser.parseStatement(typeCast + " deserializedResult;")); |
| 181 | + handleDeclaredTypeResponse(body, (DeclaredType) returnType, serializationFormatSet, typeCast); |
| 182 | + |
| 183 | + addReturnStatement(body, returnIsResponse, "deserializedResult"); |
169 | 184 | }
|
170 | 185 |
|
171 | 186 | // Helper method that creates the return statement as either Response<T> or T.
|
|
0 commit comments