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"
---

Cast null arguments to their declared Java types in generated samples and tests so overloaded API calls are unambiguous.
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public ClientMethodExampleWriter(ClientMethod method, String clientVarName, Prox
.filter(methodParameter -> !pageDetails.shouldHideParameter(methodParameter.getClientMethodParameter()))
.collect(Collectors.toList());
}
List<ExampleNode> exampleNodes = methodParameters.stream()
.map(methodParameter -> parseNodeFromParameter(method, proxyMethodExample, methodParameter))
.collect(Collectors.toList());

String parameterInvocations = exampleNodes.stream().map(nodeVisitor::accept).collect(Collectors.joining(", "));
String parameterInvocations = methodParameters.stream()
.map(methodParameter -> ExampleWriterUtil.getParameterExpression(
methodParameter.getClientMethodParameter().getClientType(),
nodeVisitor.accept(parseNodeFromParameter(method, proxyMethodExample, methodParameter))))
.collect(Collectors.joining(", "));

// assertion
this.imports.add("org.junit.jupiter.api.Assertions");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.typespec.http.client.generator.core.template.example;

import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType;

final class ExampleWriterUtil {
private ExampleWriterUtil() {
}

static String getParameterExpression(IType parameterType, String valueExpression) {
return "null".equals(valueExpression) ? String.format("(%s) null", parameterType) : valueExpression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public ProtocolExampleWriter(ProtocolExample protocolExample) {
// parameter values and required invocation on RequestOptions
List<String> params = new ArrayList<>();
for (ClientMethodParameter parameter : method.getParameters()) {
params.add(parameter.getClientType().defaultValueExpression());
params.add(ExampleWriterUtil.getParameterExpression(parameter.getClientType(),
parameter.getClientType().defaultValueExpression()));
}

StringBuilder binaryDataStmt = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.typespec.http.client.generator.core.template.example;

import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ExampleWriterUtilTests {
@Test
public void typesNullParameterExpression() {
Assertions.assertEquals("(String) null", ExampleWriterUtil.getParameterExpression(ClassType.STRING, "null"));
Comment thread
alzimmermsft marked this conversation as resolved.
}

@Test
public void preservesNonNullParameterExpression() {
Assertions.assertEquals("\"value\"", ExampleWriterUtil.getParameterExpression(ClassType.STRING, "\"value\""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package tsptest.builtin.generated;

import com.azure.core.util.Configuration;
import java.time.OffsetDateTime;
import tsptest.builtin.BuiltinClient;
import tsptest.builtin.BuiltinClientBuilder;
import tsptest.builtin.models.Builtin;
Expand All @@ -14,7 +15,8 @@ public static void main(String[] args) {
BuiltinClient builtinClient
= new BuiltinClientBuilder().endpoint(Configuration.getGlobalConfiguration().get("ENDPOINT")).buildClient();
// BEGIN:tsptest.builtin.generated.builtin-op-read.builtin-op-read
Builtin response = builtinClient.read(null, null, null, "myFilter", null, null);
Builtin response = builtinClient.read((String) null, (String) null, (OffsetDateTime) null, "myFilter",
(String) null, (String) null);
// END:tsptest.builtin.generated.builtin-op-read.builtin-op-read
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void main(String[] args) {
FlattenClient flattenClient
= new FlattenClientBuilder().endpoint(Configuration.getGlobalConfiguration().get("ENDPOINT")).buildClient();
// BEGIN:tsptest.flatten.generated.send.flatten-op-send
flattenClient.send("myRequiredId", null, "myRequiredInput", 0, 50, new User("myOptionalUser"));
flattenClient.send("myRequiredId", (String) null, "myRequiredInput", 0, 50, new User("myOptionalUser"));
// END:tsptest.flatten.generated.send.flatten-op-send
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void main(String[] args) {
ModelClient modelClient
= new ModelClientBuilder().endpoint(Configuration.getGlobalConfiguration().get("ENDPOINT")).buildClient();
// BEGIN:tsptest.model.generated.model-op-put-nested.model-op-put-nested
NestedModel response = modelClient.putNested(null);
NestedModel response = modelClient.putNested((NestedModel) null);
// END:tsptest.model.generated.model-op-put-nested.model-op-put-nested
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void main(String[] args) {
= new SpecialCharsClientBuilder().endpoint(Configuration.getGlobalConfiguration().get("ENDPOINT"))
.buildClient();
// BEGIN:tsptest.specialchars.generated.builtin-op-read.builtin-op-read
Resource response = specialCharsClient.read(null);
Resource response = specialCharsClient.read((String) null);
// END:tsptest.specialchars.generated.builtin-op-read.builtin-op-read
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void main(String[] args) {
= new VersioningClientBuilder().endpoint(Configuration.getGlobalConfiguration().get("ENDPOINT"))
.buildClient();
// BEGIN:tsptest.versioning.generated.versioning-op-list.versioning-op-list
PagedIterable<Resource> response = versioningClient.list(Arrays.asList("name=name"), null);
PagedIterable<Resource> response = versioningClient.list(Arrays.asList("name=name"), (String) null);
// END:tsptest.versioning.generated.versioning-op-list.versioning-op-list
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package tsptest.builtin.generated;

import java.time.OffsetDateTime;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
Expand All @@ -17,7 +18,8 @@ public final class BuiltinOpReadTests extends BuiltinClientTestBase {
@Disabled
public void testBuiltinOpReadTests() {
// method invocation
Builtin response = builtinClient.read(null, null, null, "myFilter", null, null);
Builtin response = builtinClient.read((String) null, (String) null, (OffsetDateTime) null, "myFilter",
(String) null, (String) null);

// response assertion
Assertions.assertNotNull(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public final class FlattenOpSendTests extends FlattenClientTestBase {
@Disabled
public void testFlattenOpSendTests() {
// method invocation
flattenClient.send("myRequiredId", null, "myRequiredInput", 0, 50, new User("myOptionalUser"));
flattenClient.send("myRequiredId", (String) null, "myRequiredInput", 0, 50, new User("myOptionalUser"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public final class ModelOpPutNestedTests extends ModelClientTestBase {
@Disabled
public void testModelOpPutNestedTests() {
// method invocation
NestedModel response = modelClient.putNested(null);
NestedModel response = modelClient.putNested((NestedModel) null);

// response assertion
Assertions.assertNotNull(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class BuiltinOpReadTests extends SpecialCharsClientTestBase {
@Disabled
public void testBuiltinOpReadTests() {
// method invocation
Resource response = specialCharsClient.read(null);
Resource response = specialCharsClient.read((String) null);

// response assertion
Assertions.assertNotNull(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public final class VersioningOpListTests extends VersioningClientTestBase {
@Disabled
public void testVersioningOpListTests() {
// method invocation
PagedIterable<Resource> response = versioningClient.list(Arrays.asList("name=name"), null);
PagedIterable<Resource> response = versioningClient.list(Arrays.asList("name=name"), (String) null);

// response assertion
Assertions.assertEquals(200, response.iterableByPage().iterator().next().getStatusCode());
Expand Down