Azure / Azure/azure-sdk-for-java

[FEATURE REQ] clientcore annotation-processor, handle BinaryData on file upload

Open
#47,993 3 comments 0 reactions 1 assignee Claimed by @browndav-msft View on GitHub
Client needs-team-attention Storage
Dominant language
Java
Stars
2.6k
Forks
2.2k
Avg merge
2d 8h
Merged PRs (30d)
178

Description

**Is your feature request related to a problem? Please describe.**
annotation-processor 1.0.0-beta.4

TypeSpec test on upload JSON as file
https://github.com/microsoft/typespec/blob/main/packages/http-specs/specs/type/file/main.tsp#L27-L36

TypeSpec doc on `File` https://typespec.io/docs/libraries/http/files
See "contentType" part
Image
also https://typespec.io/docs/libraries/http/files/#creating-custom-file-models

So, I think the REST API would have content-type header as `application/json` in the request, and the body is JSON encoded as UTF-8 (e.g. `{"key", "value"}`).

Generated code be
https://github.com/microsoft/typespec/blob/main/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/file/FileClient.java#L85-L107

However, the ServiceImpl generated by annotation-processor would do another `BinaryData.fromObject()`, which make the payload body `"{\"key\", \"value\"}"`.
```java
@Override
public Response uploadFileJsonContentType(String endpoint, String contentType, BinaryData file, long contentLength, RequestContext requestContext) {
// Create the HttpRequest.
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/file/body/request/json-content-type");
httpRequest.getHeaders().add(new HttpHeader(HttpHeaderName.CONTENT_LENGTH, String.valueOf(contentLength)));
if (file != null) {
httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType);
SerializationFormat serializationFormat = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders());
if (xmlSerializer.supportsFormat(serializationFormat)) {
httpRequest.setBody(BinaryData.fromObject(file, xmlSerializer));
} else {
httpRequest.setBody(BinaryData.fromObject(file, jsonSerializer));
}
}
httpRequest.setContext(requestContext);
httpRequest.getContext().getRequestCallback().accept(httpRequest);
```

**Describe the solution you'd like**
The current behavior of annotation-processor seems wrong. If the request body is already a `BinaryData`, it should not process it again.

PS: code in azure-core DPG passes. There the distinction is the type. If it is `byte[]`, the payload would be processed as a JSON string of base64 encode of the `byte[]`. If it is `BinaryData`, it is the exact payload.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.

**Information Checklist**
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report
- [x] Description Added
- [x] Expected solution specified

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.