Skip to content

Commit c6a6519

Browse files
http-client-java, support clientRequired as Java clientOption (#10337)
add `clientRequired` client option for java it can also be used to set required property to optional.
1 parent df2da9f commit c6a6519

16 files changed

Lines changed: 1000 additions & 4 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: feature
3+
packages:
4+
- "@typespec/http-client-java"
5+
---
6+
7+
Add clientRequired clientOption for Java emitter

packages/http-client-java/emitter/src/code-model-builder.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ import {
159159
DiagnosticError,
160160
escapeJavaKeywords,
161161
getNamespace,
162+
isPropertyRequired,
162163
optionBoolean,
163164
pascalCase,
164165
removeClientSuffix,
@@ -1527,7 +1528,7 @@ export class CodeModelBuilder {
15271528
implementation: parameterOnClient
15281529
? ImplementationLocation.Client
15291530
: ImplementationLocation.Method,
1530-
required: !param.optional,
1531+
required: isPropertyRequired(param),
15311532
nullable: nullable,
15321533
protocol: {
15331534
http: new HttpParameter(param.kind, {
@@ -1698,7 +1699,7 @@ export class CodeModelBuilder {
16981699
{
16991700
summary: sdkMethodParameter.summary,
17001701
implementation: ImplementationLocation.Method,
1701-
required: !sdkMethodParameter.optional,
1702+
required: isPropertyRequired(sdkMethodParameter),
17021703
nullable: false,
17031704
},
17041705
);
@@ -2046,7 +2047,7 @@ export class CodeModelBuilder {
20462047
const parameter = new Parameter(parameterName, sdkBody.doc ?? "", schema, {
20472048
summary: sdkBody.summary,
20482049
implementation: ImplementationLocation.Method,
2049-
required: !sdkBody.optional,
2050+
required: isPropertyRequired(sdkBody),
20502051
protocol: {
20512052
http: new HttpParameter(ParameterLocation.Body),
20522053
},
@@ -2975,7 +2976,7 @@ export class CodeModelBuilder {
29752976

29762977
const codeModelProperty = new Property(modelProperty.name, modelProperty.doc ?? "", schema, {
29772978
summary: modelProperty.summary,
2978-
required: !modelProperty.optional,
2979+
required: isPropertyRequired(modelProperty),
29792980
nullable: nullable,
29802981
readOnly: this.isReadOnly(modelProperty),
29812982
serializedName: getPropertySerializedName(modelProperty),

packages/http-client-java/emitter/src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DecoratedType, getClientOptions } from "@azure-tools/typespec-client-generator-core";
12
import { Diagnostic, Program, Type } from "@typespec/compiler";
23
import { spawn, SpawnOptions } from "child_process";
34

@@ -206,3 +207,8 @@ const JAVA_KEYWORDS: Set<string> = new Set<string>([
206207
"volatile",
207208
"while",
208209
]);
210+
211+
export function isPropertyRequired(property: { optional: boolean } & DecoratedType): boolean {
212+
const clientRequired = getClientOptions(property, "clientRequired") as boolean;
213+
return clientRequired ?? !property.optional;
214+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
// Code generated by Microsoft (R) TypeSpec Code Generator.
4+
5+
package tsptest.clientoption;
6+
7+
import com.azure.core.annotation.Generated;
8+
import com.azure.core.annotation.ReturnType;
9+
import com.azure.core.annotation.ServiceClient;
10+
import com.azure.core.annotation.ServiceMethod;
11+
import com.azure.core.exception.ClientAuthenticationException;
12+
import com.azure.core.exception.HttpResponseException;
13+
import com.azure.core.exception.ResourceModifiedException;
14+
import com.azure.core.exception.ResourceNotFoundException;
15+
import com.azure.core.http.rest.RequestOptions;
16+
import com.azure.core.http.rest.Response;
17+
import com.azure.core.util.BinaryData;
18+
import com.azure.core.util.FluxUtil;
19+
import reactor.core.publisher.Mono;
20+
import tsptest.clientoption.implementation.ClientRequiredsImpl;
21+
import tsptest.clientoption.models.ClientRequiredRequest;
22+
23+
/**
24+
* Initializes a new instance of the asynchronous ClientOptionClient type.
25+
*/
26+
@ServiceClient(builder = ClientOptionClientBuilder.class, isAsync = true)
27+
public final class ClientOptionAsyncClient {
28+
@Generated
29+
private final ClientRequiredsImpl serviceClient;
30+
31+
/**
32+
* Initializes an instance of ClientOptionAsyncClient class.
33+
*
34+
* @param serviceClient the service client implementation.
35+
*/
36+
@Generated
37+
ClientOptionAsyncClient(ClientRequiredsImpl serviceClient) {
38+
this.serviceClient = serviceClient;
39+
}
40+
41+
/**
42+
* The post operation.
43+
* <p><strong>Query Parameters</strong></p>
44+
* <table border="1">
45+
* <caption>Query Parameters</caption>
46+
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
47+
* <tr><td>filter</td><td>String</td><td>No</td><td>The filter parameter</td></tr>
48+
* </table>
49+
* You can add these to a request with {@link RequestOptions#addQueryParam}
50+
* <p><strong>Request Body Schema</strong></p>
51+
*
52+
* <pre>
53+
* {@code
54+
* {
55+
* name: String (Required)
56+
* timespan: Duration (Required)
57+
* }
58+
* }
59+
* </pre>
60+
*
61+
* @param body The body parameter.
62+
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
63+
* @throws HttpResponseException thrown if the request is rejected by server.
64+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
65+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
66+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
67+
* @return the {@link Response} on successful completion of {@link Mono}.
68+
*/
69+
@Generated
70+
@ServiceMethod(returns = ReturnType.SINGLE)
71+
public Mono<Response<Void>> postWithResponse(BinaryData body, RequestOptions requestOptions) {
72+
return this.serviceClient.postWithResponseAsync(body, requestOptions);
73+
}
74+
75+
/**
76+
* The post operation.
77+
*
78+
* @param body The body parameter.
79+
* @param filter The filter parameter.
80+
* @throws IllegalArgumentException thrown if parameters fail the validation.
81+
* @throws HttpResponseException thrown if the request is rejected by server.
82+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
83+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
84+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
85+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
86+
* @return A {@link Mono} that completes when a successful response is received.
87+
*/
88+
@Generated
89+
@ServiceMethod(returns = ReturnType.SINGLE)
90+
public Mono<Void> post(ClientRequiredRequest body, String filter) {
91+
// Generated convenience method for postWithResponse
92+
RequestOptions requestOptions = new RequestOptions();
93+
if (filter != null) {
94+
requestOptions.addQueryParam("filter", filter, false);
95+
}
96+
return postWithResponse(BinaryData.fromObject(body), requestOptions).flatMap(FluxUtil::toMono);
97+
}
98+
99+
/**
100+
* The post operation.
101+
*
102+
* @param body The body parameter.
103+
* @throws IllegalArgumentException thrown if parameters fail the validation.
104+
* @throws HttpResponseException thrown if the request is rejected by server.
105+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
106+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
107+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
108+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
109+
* @return A {@link Mono} that completes when a successful response is received.
110+
*/
111+
@Generated
112+
@ServiceMethod(returns = ReturnType.SINGLE)
113+
public Mono<Void> post(ClientRequiredRequest body) {
114+
// Generated convenience method for postWithResponse
115+
RequestOptions requestOptions = new RequestOptions();
116+
return postWithResponse(BinaryData.fromObject(body), requestOptions).flatMap(FluxUtil::toMono);
117+
}
118+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
// Code generated by Microsoft (R) TypeSpec Code Generator.
4+
5+
package tsptest.clientoption;
6+
7+
import com.azure.core.annotation.Generated;
8+
import com.azure.core.annotation.ReturnType;
9+
import com.azure.core.annotation.ServiceClient;
10+
import com.azure.core.annotation.ServiceMethod;
11+
import com.azure.core.exception.ClientAuthenticationException;
12+
import com.azure.core.exception.HttpResponseException;
13+
import com.azure.core.exception.ResourceModifiedException;
14+
import com.azure.core.exception.ResourceNotFoundException;
15+
import com.azure.core.http.rest.RequestOptions;
16+
import com.azure.core.http.rest.Response;
17+
import com.azure.core.util.BinaryData;
18+
import tsptest.clientoption.implementation.ClientRequiredsImpl;
19+
import tsptest.clientoption.models.ClientRequiredRequest;
20+
21+
/**
22+
* Initializes a new instance of the synchronous ClientOptionClient type.
23+
*/
24+
@ServiceClient(builder = ClientOptionClientBuilder.class)
25+
public final class ClientOptionClient {
26+
@Generated
27+
private final ClientRequiredsImpl serviceClient;
28+
29+
/**
30+
* Initializes an instance of ClientOptionClient class.
31+
*
32+
* @param serviceClient the service client implementation.
33+
*/
34+
@Generated
35+
ClientOptionClient(ClientRequiredsImpl serviceClient) {
36+
this.serviceClient = serviceClient;
37+
}
38+
39+
/**
40+
* The post operation.
41+
* <p><strong>Query Parameters</strong></p>
42+
* <table border="1">
43+
* <caption>Query Parameters</caption>
44+
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
45+
* <tr><td>filter</td><td>String</td><td>No</td><td>The filter parameter</td></tr>
46+
* </table>
47+
* You can add these to a request with {@link RequestOptions#addQueryParam}
48+
* <p><strong>Request Body Schema</strong></p>
49+
*
50+
* <pre>
51+
* {@code
52+
* {
53+
* name: String (Required)
54+
* timespan: Duration (Required)
55+
* }
56+
* }
57+
* </pre>
58+
*
59+
* @param body The body parameter.
60+
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
61+
* @throws HttpResponseException thrown if the request is rejected by server.
62+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
63+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
64+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
65+
* @return the {@link Response}.
66+
*/
67+
@Generated
68+
@ServiceMethod(returns = ReturnType.SINGLE)
69+
public Response<Void> postWithResponse(BinaryData body, RequestOptions requestOptions) {
70+
return this.serviceClient.postWithResponse(body, requestOptions);
71+
}
72+
73+
/**
74+
* The post operation.
75+
*
76+
* @param body The body parameter.
77+
* @param filter The filter parameter.
78+
* @throws IllegalArgumentException thrown if parameters fail the validation.
79+
* @throws HttpResponseException thrown if the request is rejected by server.
80+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
81+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
82+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
83+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
84+
*/
85+
@Generated
86+
@ServiceMethod(returns = ReturnType.SINGLE)
87+
public void post(ClientRequiredRequest body, String filter) {
88+
// Generated convenience method for postWithResponse
89+
RequestOptions requestOptions = new RequestOptions();
90+
if (filter != null) {
91+
requestOptions.addQueryParam("filter", filter, false);
92+
}
93+
postWithResponse(BinaryData.fromObject(body), requestOptions).getValue();
94+
}
95+
96+
/**
97+
* The post operation.
98+
*
99+
* @param body The body parameter.
100+
* @throws IllegalArgumentException thrown if parameters fail the validation.
101+
* @throws HttpResponseException thrown if the request is rejected by server.
102+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
103+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
104+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
105+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
106+
*/
107+
@Generated
108+
@ServiceMethod(returns = ReturnType.SINGLE)
109+
public void post(ClientRequiredRequest body) {
110+
// Generated convenience method for postWithResponse
111+
RequestOptions requestOptions = new RequestOptions();
112+
postWithResponse(BinaryData.fromObject(body), requestOptions).getValue();
113+
}
114+
}

0 commit comments

Comments
 (0)