Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-client-java"
---

Fix null LRO error response body handling in generated management clients.
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ protected void writeAdditionalClassBlock(JavaClass classBlock) {
code.line("super(null);");
code.line("this.statusCode = statusCode;");
code.line("this.httpHeaders = httpHeaders;");
// Use an empty array so body accessors still work when the poller surfaces an error without a
// response body.
code.line(
"this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);");
"this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);");
});

block.publicMethod("int getStatusCode()", code -> code.methodReturn("statusCode"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.typespec.http.client.generator.mgmt.template;

import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaFile;
import com.microsoft.typespec.http.client.generator.mgmt.TestUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class FluentServiceClientTemplateTests {

@BeforeAll
public static void ensurePlugin() {
new TestUtils.MockFluentGen();
}

@Test
public void writesEmptyByteArrayForNullLroErrorBody() {
JavaFile javaFile = new JavaFile("ProgramEnrollmentManagementClientImpl.java");

javaFile.publicFinalClass("ProgramEnrollmentManagementClientImpl",
classBlock -> FluentServiceClientTemplate.getInstance().writeAdditionalClassBlock(classBlock));

String output = javaFile.getContents().toString();

Assertions.assertTrue(output.contains(
"this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);"));
Assertions.assertFalse(output.contains(
"this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private static final class HttpResponseImpl extends HttpResponse {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody == null ? null : responseBody.getBytes(StandardCharsets.UTF_8);
this.responseBody = responseBody == null ? new byte[0] : responseBody.getBytes(StandardCharsets.UTF_8);
}

public int getStatusCode() {
Expand Down
Loading