Skip to content
Open
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
Expand Up @@ -804,7 +804,17 @@ private IReadOnlyList<ValueExpression> GetProtocolMethodArguments(Dictionary<str
if (ScmCodeModelGenerator.Instance.TypeFactory.CSharpTypeMap.TryGetValue(convenienceParam.Type, out var typeProvider) &&
typeProvider is ModelProvider paramModel)
{
AddArgument(protocolParam, paramModel.GetPropertyExpression(convenienceParam, propertySegments));
var propertyExpression = paramModel.GetPropertyExpression(convenienceParam, propertySegments);
// When the protocol parameter expects BinaryContent (i.e., it's a content parameter),
// wrap the resolved BinaryData property expression with BinaryContent.Create().
if (protocolParam.IsContentParameter)
{
AddArgument(protocolParam, RequestContentApiSnippets.Create(propertyExpression));
}
else
{
AddArgument(protocolParam, propertyExpression);
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,51 @@ public async Task MethodParameterSegments_UpdateMethod_SetsSegments()
Assert.AreEqual("param2", param.MethodParameterSegments[1].Name);
}

[Test]
public async Task MethodParameterSegments_BodyBinaryDataProperty_WrapsWithBinaryContentCreate()
{
// Test scenario: When a body parameter navigates to a BinaryData property via MethodParameterSegments,
// the convenience method should wrap the expression with BinaryContent.Create().
var streamModel = InputFactory.Model(
"StreamModel",
properties:
[
InputFactory.Property("body", InputPrimitiveType.Base64, isRequired: true),
]);

var bodyParam = InputFactory.BodyParameter("body", InputPrimitiveType.Base64, isRequired: true,
contentTypes: ["application/jsonl"], defaultContentType: "application/jsonl");
bodyParam.Update(methodParameterSegments: [
InputFactory.MethodParameter("stream", streamModel, isRequired: true, location: InputRequestLocation.None),
InputFactory.MethodParameter("body", InputPrimitiveType.Base64, isRequired: true)
]);

var serviceMethod = InputFactory.BasicServiceMethod(
"send",
InputFactory.Operation(
"send",
parameters: [bodyParam],
requestMediaTypes: ["application/jsonl"],
responses: [InputFactory.OperationResponse([204])]),
parameters: [InputFactory.MethodParameter("stream", streamModel, isRequired: true, location: InputRequestLocation.None)]);

var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]);
await MockHelpers.LoadMockGeneratorAsync(clients: () => [inputClient], inputModels: () => [streamModel]);

var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient);
Assert.IsNotNull(client);

var methodCollection = new ScmMethodProviderCollection(serviceMethod, client!);
var convenienceMethod = methodCollection.FirstOrDefault(
m => m.Signature.Parameters.Any(p => p.Name == "stream") && m.Signature.Name == "Send");
Assert.IsNotNull(convenienceMethod);

var methodBody = convenienceMethod!.BodyStatements!.ToDisplayString();
// Verify that BinaryContent.Create() wraps the property access
Assert.IsTrue(methodBody.Contains("BinaryContent.Create(stream.Body)"),
$"Expected 'BinaryContent.Create(stream.Body)' in method body but got: {methodBody}");
}

[Test]
public async Task MissingOperationParamsResultInNamedArgsForSubsequent()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public virtual ClientResult<RoundTripModel> NoContentType(Wrapper info, Cancella
{
Argument.AssertNotNull(info, nameof(info));

ClientResult result = NoContentType(info.P2, info.P1, info.Action, cancellationToken.ToRequestOptions());
ClientResult result = NoContentType(info.P2, info.P1, BinaryContent.Create(info.Action), cancellationToken.ToRequestOptions());
return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse());
}

Expand All @@ -347,7 +347,7 @@ public virtual async Task<ClientResult<RoundTripModel>> NoContentTypeAsync(Wrapp
{
Argument.AssertNotNull(info, nameof(info));

ClientResult result = await NoContentTypeAsync(info.P2, info.P1, info.Action, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
ClientResult result = await NoContentTypeAsync(info.P2, info.P1, BinaryContent.Create(info.Action), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse());
}

Expand Down
Loading