Skip to content

Commit

Permalink
- fixes error mapping declaration for inheritance
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
  • Loading branch information
baywet committed Feb 4, 2022
1 parent 32087dd commit ab84fa5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public class NativeResponseHandler implements ResponseHandler {

/** The error mappings for the response to use when deserializing failed responses bodies. Where an error code like 401 applies specifically to that status code, a class code like 4XX applies to all status codes within the range if an the specific error code is not present. */
@Nullable
public HashMap<String, Class<Parsable>> errorMappings;
public HashMap<String, Class<? extends Parsable>> errorMappings;

/** {@inheritdoc} */
@Nonnull
@Override
public <NativeResponseType, ModelType> CompletableFuture<ModelType> handleResponseAsync(
NativeResponseType response,
HashMap<String, Class<Parsable>> errorMappings) {
HashMap<String, Class<? extends Parsable>> errorMappings) {
this.value = response;
this.errorMappings = errorMappings;
return CompletableFuture.completedFuture(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface RequestAdapter {
* @param <ModelType> the type of the response model to deserialize the response into.
* @return a {@link CompletableFuture} with the deserialized response model.
*/
<ModelType extends Parsable> CompletableFuture<ModelType> sendAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<Parsable>> errorMappings);
<ModelType extends Parsable> CompletableFuture<ModelType> sendAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<? extends Parsable>> errorMappings);
/**
* Executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection.
* @param requestInfo the request info to execute.
Expand All @@ -42,7 +42,7 @@ public interface RequestAdapter {
* @param <ModelType> the type of the response model to deserialize the response into.
* @return a {@link CompletableFuture} with the deserialized response model collection.
*/
<ModelType extends Parsable> CompletableFuture<Iterable<ModelType>> sendCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<Parsable>> errorMappings);
<ModelType extends Parsable> CompletableFuture<Iterable<ModelType>> sendCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<? extends Parsable>> errorMappings);
/**
* Executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model.
* @param requestInfo the request info to execute.
Expand All @@ -52,7 +52,7 @@ public interface RequestAdapter {
* @param <ModelType> the type of the response model to deserialize the response into.
* @return a {@link CompletableFuture} with the deserialized primitive response model.
*/
<ModelType> CompletableFuture<ModelType> sendPrimitiveAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<Parsable>> errorMappings);
<ModelType> CompletableFuture<ModelType> sendPrimitiveAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<? extends Parsable>> errorMappings);
/**
* Executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive collection response model.
* @param requestInfo the request info to execute.
Expand All @@ -62,7 +62,7 @@ public interface RequestAdapter {
* @param <ModelType> the type of the response model to deserialize the response into.
* @return a {@link CompletableFuture} with the deserialized primitive collection response model.
*/
<ModelType> CompletableFuture<Iterable<ModelType>> sendPrimitiveCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<Parsable>> errorMappings);
<ModelType> CompletableFuture<Iterable<ModelType>> sendPrimitiveCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<? extends Parsable>> errorMappings);
/**
* Sets The base url for every request.
* @param baseUrl The base url for every request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public interface ResponseHandler {
* @return A CompletableFuture that represents the asynchronous operation and contains the deserialized response.
*/
@Nonnull
<NativeResponseType, ModelType> CompletableFuture<ModelType> handleResponseAsync(@Nonnull final NativeResponseType response, @Nullable final HashMap<String, Class<Parsable>> errorMappings);
<NativeResponseType, ModelType> CompletableFuture<ModelType> handleResponseAsync(@Nonnull final NativeResponseType response, @Nullable final HashMap<String, Class<? extends Parsable>> errorMappings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void enableBackingStore(@Nullable final BackingStoreFactory backingStoreF
}
}
@Nonnull
public <ModelType extends Parsable> CompletableFuture<Iterable<ModelType>> sendCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<Parsable>> errorMappings) {
public <ModelType extends Parsable> CompletableFuture<Iterable<ModelType>> sendCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<? extends Parsable>> errorMappings) {
Objects.requireNonNull(requestInfo, "parameter requestInfo cannot be null");

return this.getHttpResponseMessage(requestInfo)
Expand All @@ -115,7 +115,7 @@ public <ModelType extends Parsable> CompletableFuture<Iterable<ModelType>> sendC
});
}
@Nonnull
public <ModelType extends Parsable> CompletableFuture<ModelType> sendAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<Parsable>> errorMappings) {
public <ModelType extends Parsable> CompletableFuture<ModelType> sendAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<? extends Parsable>> errorMappings) {
Objects.requireNonNull(requestInfo, "parameter requestInfo cannot be null");

return this.getHttpResponseMessage(requestInfo)
Expand All @@ -140,7 +140,7 @@ private String getMediaTypeAndSubType(final MediaType mediaType) {
return mediaType.type() + "/" + mediaType.subtype();
}
@Nonnull
public <ModelType> CompletableFuture<ModelType> sendPrimitiveAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<Parsable>> errorMappings) {
public <ModelType> CompletableFuture<ModelType> sendPrimitiveAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<? extends Parsable>> errorMappings) {
return this.getHttpResponseMessage(requestInfo)
.thenCompose(r -> this.throwFailedResponse(r, errorMappings))
.thenCompose(response -> {
Expand Down Expand Up @@ -185,7 +185,7 @@ public <ModelType> CompletableFuture<ModelType> sendPrimitiveAsync(@Nonnull fina
}
});
}
public <ModelType> CompletableFuture<Iterable<ModelType>> sendPrimitiveCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<Parsable>> errorMappings) {
public <ModelType> CompletableFuture<Iterable<ModelType>> sendPrimitiveCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap<String, Class<? extends Parsable>> errorMappings) {
Objects.requireNonNull(requestInfo, "parameter requestInfo cannot be null");

return this.getHttpResponseMessage(requestInfo)
Expand Down Expand Up @@ -213,7 +213,7 @@ private ParseNode getRootParseNode(final Response response) throws IOException {
return rootNode;
}
}
private CompletableFuture<Response> throwFailedResponse(final Response response, final HashMap<String, Class<Parsable>> errorMappings) {
private CompletableFuture<Response> throwFailedResponse(final Response response, final HashMap<String, Class<? extends Parsable>> errorMappings) {
if (response.isSuccessful()) return CompletableFuture.completedFuture(response);

final String statusCodeAsString = Integer.toString(response.code());
Expand All @@ -224,7 +224,7 @@ private CompletableFuture<Response> throwFailedResponse(final Response response,
!(statusCode >= 500 && statusCode < 600 && errorMappings.containsKey("5XX"))) {
return CompletableFuture.failedFuture(new ApiException("the server returned an unexpected status code and no error class is registered for this code " + statusCode));
}
final Class<Parsable> errorClass = errorMappings.containsKey(statusCodeAsString) ?
final Class<? extends Parsable> errorClass = errorMappings.containsKey(statusCodeAsString) ?
errorMappings.get(statusCodeAsString) :
(statusCode >= 400 && statusCode < 500 ?
errorMappings.get("4XX") :
Expand Down
7 changes: 5 additions & 2 deletions src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requ
var errorMappingVarName = "null";
if(codeElement.ErrorMappings.Any()) {
errorMappingVarName = "errorMapping";
writer.WriteLine($"final HashMap<String, Class<Parsable>> {errorMappingVarName} = new HashMap<String, Class<Parsable>>({codeElement.ErrorMappings.Count}) {{{{");
writer.WriteLine($"final HashMap<String, Class<? extends Parsable>> {errorMappingVarName} = new HashMap<String, Class<? extends Parsable>>({codeElement.ErrorMappings.Count}) {{{{");
writer.IncreaseIndent();
foreach(var errorMapping in codeElement.ErrorMappings) {
writer.WriteLine($"put(\"{errorMapping.Key.ToUpperInvariant()}\", {errorMapping.Value.Name.ToFirstCharacterUpperCase()}.class);");
Expand Down Expand Up @@ -344,7 +344,10 @@ CodeMethodKind.Setter when (code.AccessedProperty?.IsNameEscaped ?? false) && !s
_ => code.Name.ToFirstCharacterLowerCase()
};
var parameters = string.Join(", ", code.Parameters.OrderBy(x => x, parameterOrderComparer).Select(p=> conventions.GetParameterSignature(p, code)).ToList());
var throwableDeclarations = code.IsOfKind(CodeMethodKind.RequestGenerator) ? "throws URISyntaxException ": string.Empty;
var throwableDeclarations = code.MethodKind switch {
CodeMethodKind.RequestGenerator => "throws URISyntaxException ",
_ => string.Empty
};
var collectionCorrectedReturnType = code.ReturnType.IsArray && code.IsOfKind(CodeMethodKind.RequestExecutor) ?
$"Iterable<{returnType.StripArraySuffix()}>" :
returnType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void WritesRequestExecutorBody() {
writer.Write(method);
var result = tw.ToString();
Assert.Contains("final RequestInformation requestInfo", result);
Assert.Contains("final HashMap<String, Class<Parsable>> errorMapping = new HashMap<String, Class<Parsable>>", result);
Assert.Contains("final HashMap<String, Class<? extends Parsable>> errorMapping = new HashMap<String, Class<? extends Parsable>>", result);
Assert.Contains("put(\"4XX\", Error4XX.class);", result);
Assert.Contains("put(\"5XX\", Error5XX.class);", result);
Assert.Contains("put(\"403\", Error403.class);", result);
Expand All @@ -197,7 +197,7 @@ public void DoesntCreateDictionaryOnEmptyErrorMapping() {
AddRequestBodyParameters();
writer.Write(method);
var result = tw.ToString();
Assert.DoesNotContain("final HashMap<String, Class<Parsable>> errorMapping = new HashMap<String, Class<Parsable>>", result);
Assert.DoesNotContain("final HashMap<String, Class<? extends Parsable>> errorMapping = new HashMap<String, Class<? extends Parsable>>", result);
Assert.Contains("null", result);
AssertExtensions.CurlyBracesAreClosed(result);
}
Expand Down

0 comments on commit ab84fa5

Please sign in to comment.