Skip to content

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

@weidongxu-microsoft

Description

@weidongxu-microsoft

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\"}".

    @Override
    public Response<Void> 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

  • Description Added
  • Expected solution specified

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.StorageStorage Service (Queues, Blobs, Files)needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions