Azure / autorest.java

Extension for AutoRest (https://github.com/Azure/autorest) that generates Java code
MIT License
33 stars 82 forks source link

tsp, content type, support cases when body is optional #2185

Open haolingdong-msft opened 1 year ago

haolingdong-msft commented 1 year ago

Link https://github.com/Azure/autorest.java/issues/1472#issuecomment-1581857338

The current behavior for single content type with optional body in tsp is diffferent from dpg.

For single content type, if body is optional, we will add content-type in header param table section, and the description mention the value required.

Add test cases for optional body cases and make the behavior the same as DPG.

From Weidong: We may use a similar trick (as repeatability) to set it only when there is body, but content-type not set.

requestOptions.addRequestCallback(requestLocal -> {
    if (requestLocal.getBody() != null && requestLocal.getHeaders().get(HttpHeaderName.fromString("content-type")) == null) {
        requestLocal.setHeader(HttpHeaderName.fromString("content-type"), "<single_content_type>");
    }
});
weidongxu-microsoft commented 1 year ago

Another issue (not sure if related) <-- Please check this first. It affect DPG 1.0 and TSP on protocol API method signature

On multiple content-type, testserver is optional body https://github.com/Azure/autorest.testserver/blob/main/swagger/media_types.json#L11-L31

Generated code has contentType as required, but body as optional (hence not in method signature) https://github.com/Azure/autorest.java/blob/4ac4a8e/protocol-tests/src/main/java/fixtures/mediatypes/MediaTypesClient.java#L60

    public Response<BinaryData> analyzeBodyWithResponse(String contentType, RequestOptions requestOptions)

I didn't remember the discussion. But I assume contentType also be optional? Reason is that, when user not use the body, they should not need to use the contentType as well (so it should not be required).

code seems try to do optional contentType if body is optional https://github.com/Azure/autorest.java/blob/main/javagen/src/main/java/com/azure/autorest/util/MethodUtil.java#L178-L183

I say "seems" because I don't understand the instanceof BinarySchema part. What's special about BinarySchema? Should it find body parameter?