diff --git a/core b/core index 334950f4ff..a385cfd03e 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 334950f4ff31c737bb9badf55475ac5c1b19c281 +Subproject commit a385cfd03ea179f55ccaea49537619f7684b9b4a diff --git a/typespec-extension/changelog.md b/typespec-extension/changelog.md index 0da78b4f58..cead46e850 100644 --- a/typespec-extension/changelog.md +++ b/typespec-extension/changelog.md @@ -1,11 +1,16 @@ # Release History +## 0.44.2 (2026-04-10) + +Compatible with compiler 1.11.0. + +- Supported `@clientRequired` client option. + ## 0.44.1 (2026-04-10) Compatible with compiler 1.11.0. - Updated package dependencies to the latest versions. -- Supported `@clientRequired` client option. ## 0.44.0 (2026-04-08) diff --git a/typespec-extension/package-lock.json b/typespec-extension/package-lock.json index a82ee58c12..ff25bbe75b 100644 --- a/typespec-extension/package-lock.json +++ b/typespec-extension/package-lock.json @@ -1,12 +1,12 @@ { "name": "@azure-tools/typespec-java", - "version": "0.44.1", + "version": "0.44.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@azure-tools/typespec-java", - "version": "0.44.1", + "version": "0.44.2", "license": "MIT", "dependencies": { "@autorest/codemodel": "~4.20.1", diff --git a/typespec-extension/package.json b/typespec-extension/package.json index 953cc0a90a..21e0d1104b 100644 --- a/typespec-extension/package.json +++ b/typespec-extension/package.json @@ -1,6 +1,6 @@ { "name": "@azure-tools/typespec-java", - "version": "0.44.1", + "version": "0.44.2", "description": "TypeSpec library for emitting Java client from the TypeSpec REST protocol binding", "keywords": [ "TypeSpec" diff --git a/typespec-tests/package.json b/typespec-tests/package.json index c82d8a7b70..b7138d7c19 100644 --- a/typespec-tests/package.json +++ b/typespec-tests/package.json @@ -13,7 +13,7 @@ "@typespec/spector": "0.1.0-alpha.25", "@typespec/http-specs": "0.1.0-alpha.36", "@azure-tools/azure-http-specs": "0.1.0-alpha.39", - "@azure-tools/typespec-java": "file:/../typespec-extension/azure-tools-typespec-java-0.44.1.tgz" + "@azure-tools/typespec-java": "file:/../typespec-extension/azure-tools-typespec-java-0.44.2.tgz" }, "devDependencies": { "@typespec/prettier-plugin-typespec": "^1.11.0", diff --git a/typespec-tests/src/main/java/tsptest/clientoption/ClientOptionAsyncClient.java b/typespec-tests/src/main/java/tsptest/clientoption/ClientOptionAsyncClient.java new file mode 100644 index 0000000000..ea60c644a8 --- /dev/null +++ b/typespec-tests/src/main/java/tsptest/clientoption/ClientOptionAsyncClient.java @@ -0,0 +1,118 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.clientoption; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.ReturnType; +import com.azure.core.annotation.ServiceClient; +import com.azure.core.annotation.ServiceMethod; +import com.azure.core.exception.ClientAuthenticationException; +import com.azure.core.exception.HttpResponseException; +import com.azure.core.exception.ResourceModifiedException; +import com.azure.core.exception.ResourceNotFoundException; +import com.azure.core.http.rest.RequestOptions; +import com.azure.core.http.rest.Response; +import com.azure.core.util.BinaryData; +import com.azure.core.util.FluxUtil; +import reactor.core.publisher.Mono; +import tsptest.clientoption.implementation.ClientRequiredsImpl; +import tsptest.clientoption.models.ClientRequiredRequest; + +/** + * Initializes a new instance of the asynchronous ClientOptionClient type. + */ +@ServiceClient(builder = ClientOptionClientBuilder.class, isAsync = true) +public final class ClientOptionAsyncClient { + @Generated + private final ClientRequiredsImpl serviceClient; + + /** + * Initializes an instance of ClientOptionAsyncClient class. + * + * @param serviceClient the service client implementation. + */ + @Generated + ClientOptionAsyncClient(ClientRequiredsImpl serviceClient) { + this.serviceClient = serviceClient; + } + + /** + * The post operation. + *

Query Parameters

+ * + * + * + * + *
Query Parameters
NameTypeRequiredDescription
filterStringNoThe filter parameter
+ * You can add these to a request with {@link RequestOptions#addQueryParam} + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     *     timespan: Duration (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the {@link Response} on successful completion of {@link Mono}. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Mono> postWithResponse(BinaryData body, RequestOptions requestOptions) { + return this.serviceClient.postWithResponseAsync(body, requestOptions); + } + + /** + * The post operation. + * + * @param body The body parameter. + * @param filter The filter parameter. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return A {@link Mono} that completes when a successful response is received. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Mono post(ClientRequiredRequest body, String filter) { + // Generated convenience method for postWithResponse + RequestOptions requestOptions = new RequestOptions(); + if (filter != null) { + requestOptions.addQueryParam("filter", filter, false); + } + return postWithResponse(BinaryData.fromObject(body), requestOptions).flatMap(FluxUtil::toMono); + } + + /** + * The post operation. + * + * @param body The body parameter. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return A {@link Mono} that completes when a successful response is received. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Mono post(ClientRequiredRequest body) { + // Generated convenience method for postWithResponse + RequestOptions requestOptions = new RequestOptions(); + return postWithResponse(BinaryData.fromObject(body), requestOptions).flatMap(FluxUtil::toMono); + } +} diff --git a/typespec-tests/src/main/java/tsptest/clientoption/ClientOptionClient.java b/typespec-tests/src/main/java/tsptest/clientoption/ClientOptionClient.java new file mode 100644 index 0000000000..aead7bf086 --- /dev/null +++ b/typespec-tests/src/main/java/tsptest/clientoption/ClientOptionClient.java @@ -0,0 +1,114 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.clientoption; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.ReturnType; +import com.azure.core.annotation.ServiceClient; +import com.azure.core.annotation.ServiceMethod; +import com.azure.core.exception.ClientAuthenticationException; +import com.azure.core.exception.HttpResponseException; +import com.azure.core.exception.ResourceModifiedException; +import com.azure.core.exception.ResourceNotFoundException; +import com.azure.core.http.rest.RequestOptions; +import com.azure.core.http.rest.Response; +import com.azure.core.util.BinaryData; +import tsptest.clientoption.implementation.ClientRequiredsImpl; +import tsptest.clientoption.models.ClientRequiredRequest; + +/** + * Initializes a new instance of the synchronous ClientOptionClient type. + */ +@ServiceClient(builder = ClientOptionClientBuilder.class) +public final class ClientOptionClient { + @Generated + private final ClientRequiredsImpl serviceClient; + + /** + * Initializes an instance of ClientOptionClient class. + * + * @param serviceClient the service client implementation. + */ + @Generated + ClientOptionClient(ClientRequiredsImpl serviceClient) { + this.serviceClient = serviceClient; + } + + /** + * The post operation. + *

Query Parameters

+ * + * + * + * + *
Query Parameters
NameTypeRequiredDescription
filterStringNoThe filter parameter
+ * You can add these to a request with {@link RequestOptions#addQueryParam} + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     *     timespan: Duration (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the {@link Response}. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public Response postWithResponse(BinaryData body, RequestOptions requestOptions) { + return this.serviceClient.postWithResponse(body, requestOptions); + } + + /** + * The post operation. + * + * @param body The body parameter. + * @param filter The filter parameter. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public void post(ClientRequiredRequest body, String filter) { + // Generated convenience method for postWithResponse + RequestOptions requestOptions = new RequestOptions(); + if (filter != null) { + requestOptions.addQueryParam("filter", filter, false); + } + postWithResponse(BinaryData.fromObject(body), requestOptions).getValue(); + } + + /** + * The post operation. + * + * @param body The body parameter. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + */ + @Generated + @ServiceMethod(returns = ReturnType.SINGLE) + public void post(ClientRequiredRequest body) { + // Generated convenience method for postWithResponse + RequestOptions requestOptions = new RequestOptions(); + postWithResponse(BinaryData.fromObject(body), requestOptions).getValue(); + } +} diff --git a/typespec-tests/src/main/java/tsptest/clientoption/ClientOptionClientBuilder.java b/typespec-tests/src/main/java/tsptest/clientoption/ClientOptionClientBuilder.java new file mode 100644 index 0000000000..c67113ece0 --- /dev/null +++ b/typespec-tests/src/main/java/tsptest/clientoption/ClientOptionClientBuilder.java @@ -0,0 +1,287 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.clientoption; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.ServiceClientBuilder; +import com.azure.core.client.traits.ConfigurationTrait; +import com.azure.core.client.traits.EndpointTrait; +import com.azure.core.client.traits.HttpTrait; +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpHeaders; +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.HttpPipelineBuilder; +import com.azure.core.http.HttpPipelinePosition; +import com.azure.core.http.policy.AddDatePolicy; +import com.azure.core.http.policy.AddHeadersFromContextPolicy; +import com.azure.core.http.policy.AddHeadersPolicy; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpLoggingPolicy; +import com.azure.core.http.policy.HttpPipelinePolicy; +import com.azure.core.http.policy.HttpPolicyProviders; +import com.azure.core.http.policy.RequestIdPolicy; +import com.azure.core.http.policy.RetryOptions; +import com.azure.core.http.policy.RetryPolicy; +import com.azure.core.http.policy.UserAgentPolicy; +import com.azure.core.util.ClientOptions; +import com.azure.core.util.Configuration; +import com.azure.core.util.CoreUtils; +import com.azure.core.util.builder.ClientBuilderUtil; +import com.azure.core.util.logging.ClientLogger; +import com.azure.core.util.serializer.JacksonAdapter; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import tsptest.clientoption.implementation.ClientOptionClientImpl; + +/** + * A builder for creating a new instance of the ClientOptionClient type. + */ +@ServiceClientBuilder(serviceClients = { ClientOptionClient.class, ClientOptionAsyncClient.class }) +public final class ClientOptionClientBuilder implements HttpTrait, + ConfigurationTrait, EndpointTrait { + @Generated + private static final String SDK_NAME = "name"; + + @Generated + private static final String SDK_VERSION = "version"; + + @Generated + private static final Map PROPERTIES = CoreUtils.getProperties("tsptest-clientoption.properties"); + + @Generated + private final List pipelinePolicies; + + /** + * Create an instance of the ClientOptionClientBuilder. + */ + @Generated + public ClientOptionClientBuilder() { + this.pipelinePolicies = new ArrayList<>(); + } + + /* + * The HTTP client used to send the request. + */ + @Generated + private HttpClient httpClient; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public ClientOptionClientBuilder httpClient(HttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /* + * The HTTP pipeline to send requests through. + */ + @Generated + private HttpPipeline pipeline; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public ClientOptionClientBuilder pipeline(HttpPipeline pipeline) { + if (this.pipeline != null && pipeline == null) { + LOGGER.atInfo().log("HttpPipeline is being set to 'null' when it was previously configured."); + } + this.pipeline = pipeline; + return this; + } + + /* + * The logging configuration for HTTP requests and responses. + */ + @Generated + private HttpLogOptions httpLogOptions; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public ClientOptionClientBuilder httpLogOptions(HttpLogOptions httpLogOptions) { + this.httpLogOptions = httpLogOptions; + return this; + } + + /* + * The client options such as application ID and custom headers to set on a request. + */ + @Generated + private ClientOptions clientOptions; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public ClientOptionClientBuilder clientOptions(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + return this; + } + + /* + * The retry options to configure retry policy for failed requests. + */ + @Generated + private RetryOptions retryOptions; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public ClientOptionClientBuilder retryOptions(RetryOptions retryOptions) { + this.retryOptions = retryOptions; + return this; + } + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public ClientOptionClientBuilder addPolicy(HttpPipelinePolicy customPolicy) { + Objects.requireNonNull(customPolicy, "'customPolicy' cannot be null."); + pipelinePolicies.add(customPolicy); + return this; + } + + /* + * The configuration store that is used during construction of the service client. + */ + @Generated + private Configuration configuration; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public ClientOptionClientBuilder configuration(Configuration configuration) { + this.configuration = configuration; + return this; + } + + /* + * The service endpoint + */ + @Generated + private String endpoint; + + /** + * {@inheritDoc}. + */ + @Generated + @Override + public ClientOptionClientBuilder endpoint(String endpoint) { + this.endpoint = endpoint; + return this; + } + + /* + * The retry policy that will attempt to retry failed requests, if applicable. + */ + @Generated + private RetryPolicy retryPolicy; + + /** + * Sets The retry policy that will attempt to retry failed requests, if applicable. + * + * @param retryPolicy the retryPolicy value. + * @return the ClientOptionClientBuilder. + */ + @Generated + public ClientOptionClientBuilder retryPolicy(RetryPolicy retryPolicy) { + this.retryPolicy = retryPolicy; + return this; + } + + /** + * Builds an instance of ClientOptionClientImpl with the provided parameters. + * + * @return an instance of ClientOptionClientImpl. + */ + @Generated + private ClientOptionClientImpl buildInnerClient() { + this.validateClient(); + HttpPipeline localPipeline = (pipeline != null) ? pipeline : createHttpPipeline(); + ClientOptionClientImpl client + = new ClientOptionClientImpl(localPipeline, JacksonAdapter.createDefaultSerializerAdapter(), this.endpoint); + return client; + } + + @Generated + private void validateClient() { + // This method is invoked from 'buildInnerClient'/'buildClient' method. + // Developer can customize this method, to validate that the necessary conditions are met for the new client. + Objects.requireNonNull(endpoint, "'endpoint' cannot be null."); + } + + @Generated + private HttpPipeline createHttpPipeline() { + Configuration buildConfiguration + = (configuration == null) ? Configuration.getGlobalConfiguration() : configuration; + HttpLogOptions localHttpLogOptions = this.httpLogOptions == null ? new HttpLogOptions() : this.httpLogOptions; + ClientOptions localClientOptions = this.clientOptions == null ? new ClientOptions() : this.clientOptions; + List policies = new ArrayList<>(); + String clientName = PROPERTIES.getOrDefault(SDK_NAME, "UnknownName"); + String clientVersion = PROPERTIES.getOrDefault(SDK_VERSION, "UnknownVersion"); + String applicationId = CoreUtils.getApplicationId(localClientOptions, localHttpLogOptions); + policies.add(new UserAgentPolicy(applicationId, clientName, clientVersion, buildConfiguration)); + policies.add(new RequestIdPolicy()); + policies.add(new AddHeadersFromContextPolicy()); + HttpHeaders headers = CoreUtils.createHttpHeadersFromClientOptions(localClientOptions); + if (headers != null) { + policies.add(new AddHeadersPolicy(headers)); + } + this.pipelinePolicies.stream() + .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_CALL) + .forEach(p -> policies.add(p)); + HttpPolicyProviders.addBeforeRetryPolicies(policies); + policies.add(ClientBuilderUtil.validateAndGetRetryPolicy(retryPolicy, retryOptions, new RetryPolicy())); + policies.add(new AddDatePolicy()); + this.pipelinePolicies.stream() + .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_RETRY) + .forEach(p -> policies.add(p)); + HttpPolicyProviders.addAfterRetryPolicies(policies); + policies.add(new HttpLoggingPolicy(localHttpLogOptions)); + HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) + .httpClient(httpClient) + .clientOptions(localClientOptions) + .build(); + return httpPipeline; + } + + /** + * Builds an instance of ClientOptionAsyncClient class. + * + * @return an instance of ClientOptionAsyncClient. + */ + @Generated + public ClientOptionAsyncClient buildAsyncClient() { + return new ClientOptionAsyncClient(buildInnerClient().getClientRequireds()); + } + + /** + * Builds an instance of ClientOptionClient class. + * + * @return an instance of ClientOptionClient. + */ + @Generated + public ClientOptionClient buildClient() { + return new ClientOptionClient(buildInnerClient().getClientRequireds()); + } + + private static final ClientLogger LOGGER = new ClientLogger(ClientOptionClientBuilder.class); +} diff --git a/typespec-tests/src/main/java/tsptest/clientoption/implementation/ClientOptionClientImpl.java b/typespec-tests/src/main/java/tsptest/clientoption/implementation/ClientOptionClientImpl.java new file mode 100644 index 0000000000..1feb3aedf5 --- /dev/null +++ b/typespec-tests/src/main/java/tsptest/clientoption/implementation/ClientOptionClientImpl.java @@ -0,0 +1,107 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.clientoption.implementation; + +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.HttpPipelineBuilder; +import com.azure.core.http.policy.RetryPolicy; +import com.azure.core.http.policy.UserAgentPolicy; +import com.azure.core.util.serializer.JacksonAdapter; +import com.azure.core.util.serializer.SerializerAdapter; + +/** + * Initializes a new instance of the ClientOptionClient type. + */ +public final class ClientOptionClientImpl { + /** + * Service host. + */ + private final String endpoint; + + /** + * Gets Service host. + * + * @return the endpoint value. + */ + public String getEndpoint() { + return this.endpoint; + } + + /** + * The HTTP pipeline to send requests through. + */ + private final HttpPipeline httpPipeline; + + /** + * Gets The HTTP pipeline to send requests through. + * + * @return the httpPipeline value. + */ + public HttpPipeline getHttpPipeline() { + return this.httpPipeline; + } + + /** + * The serializer to serialize an object into a string. + */ + private final SerializerAdapter serializerAdapter; + + /** + * Gets The serializer to serialize an object into a string. + * + * @return the serializerAdapter value. + */ + public SerializerAdapter getSerializerAdapter() { + return this.serializerAdapter; + } + + /** + * The ClientRequiredsImpl object to access its operations. + */ + private final ClientRequiredsImpl clientRequireds; + + /** + * Gets the ClientRequiredsImpl object to access its operations. + * + * @return the ClientRequiredsImpl object. + */ + public ClientRequiredsImpl getClientRequireds() { + return this.clientRequireds; + } + + /** + * Initializes an instance of ClientOptionClient client. + * + * @param endpoint Service host. + */ + public ClientOptionClientImpl(String endpoint) { + this(new HttpPipelineBuilder().policies(new UserAgentPolicy(), new RetryPolicy()).build(), + JacksonAdapter.createDefaultSerializerAdapter(), endpoint); + } + + /** + * Initializes an instance of ClientOptionClient client. + * + * @param httpPipeline The HTTP pipeline to send requests through. + * @param endpoint Service host. + */ + public ClientOptionClientImpl(HttpPipeline httpPipeline, String endpoint) { + this(httpPipeline, JacksonAdapter.createDefaultSerializerAdapter(), endpoint); + } + + /** + * Initializes an instance of ClientOptionClient client. + * + * @param httpPipeline The HTTP pipeline to send requests through. + * @param serializerAdapter The serializer to serialize an object into a string. + * @param endpoint Service host. + */ + public ClientOptionClientImpl(HttpPipeline httpPipeline, SerializerAdapter serializerAdapter, String endpoint) { + this.httpPipeline = httpPipeline; + this.serializerAdapter = serializerAdapter; + this.endpoint = endpoint; + this.clientRequireds = new ClientRequiredsImpl(this); + } +} diff --git a/typespec-tests/src/main/java/tsptest/clientoption/implementation/ClientRequiredsImpl.java b/typespec-tests/src/main/java/tsptest/clientoption/implementation/ClientRequiredsImpl.java new file mode 100644 index 0000000000..246bb3c07c --- /dev/null +++ b/typespec-tests/src/main/java/tsptest/clientoption/implementation/ClientRequiredsImpl.java @@ -0,0 +1,152 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.clientoption.implementation; + +import com.azure.core.annotation.BodyParam; +import com.azure.core.annotation.ExpectedResponses; +import com.azure.core.annotation.HeaderParam; +import com.azure.core.annotation.Host; +import com.azure.core.annotation.HostParam; +import com.azure.core.annotation.Post; +import com.azure.core.annotation.ReturnType; +import com.azure.core.annotation.ServiceInterface; +import com.azure.core.annotation.ServiceMethod; +import com.azure.core.annotation.UnexpectedResponseExceptionType; +import com.azure.core.exception.ClientAuthenticationException; +import com.azure.core.exception.HttpResponseException; +import com.azure.core.exception.ResourceModifiedException; +import com.azure.core.exception.ResourceNotFoundException; +import com.azure.core.http.rest.RequestOptions; +import com.azure.core.http.rest.Response; +import com.azure.core.http.rest.RestProxy; +import com.azure.core.util.BinaryData; +import com.azure.core.util.Context; +import com.azure.core.util.FluxUtil; +import reactor.core.publisher.Mono; + +/** + * An instance of this class provides access to all the operations defined in ClientRequireds. + */ +public final class ClientRequiredsImpl { + /** + * The proxy service used to perform REST calls. + */ + private final ClientRequiredsService service; + + /** + * The service client containing this operation class. + */ + private final ClientOptionClientImpl client; + + /** + * Initializes an instance of ClientRequiredsImpl. + * + * @param client the instance of the service client containing this operation class. + */ + ClientRequiredsImpl(ClientOptionClientImpl client) { + this.service + = RestProxy.create(ClientRequiredsService.class, client.getHttpPipeline(), client.getSerializerAdapter()); + this.client = client; + } + + /** + * The interface defining all the services for ClientOptionClientClientRequireds to be used by the proxy service to + * perform REST calls. + */ + @Host("{endpoint}") + @ServiceInterface(name = "ClientOptionClientClientRequireds") + public interface ClientRequiredsService { + @Post("/client-required") + @ExpectedResponses({ 200 }) + @UnexpectedResponseExceptionType(value = ClientAuthenticationException.class, code = { 401 }) + @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) + @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Mono> post(@HostParam("endpoint") String endpoint, @HeaderParam("accept") String accept, + @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") BinaryData body, + RequestOptions requestOptions, Context context); + + @Post("/client-required") + @ExpectedResponses({ 200 }) + @UnexpectedResponseExceptionType(value = ClientAuthenticationException.class, code = { 401 }) + @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) + @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Response postSync(@HostParam("endpoint") String endpoint, @HeaderParam("accept") String accept, + @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") BinaryData body, + RequestOptions requestOptions, Context context); + } + + /** + * The post operation. + *

Query Parameters

+ * + * + * + * + *
Query Parameters
NameTypeRequiredDescription
filterStringNoThe filter parameter
+ * You can add these to a request with {@link RequestOptions#addQueryParam} + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     *     timespan: Duration (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the {@link Response} on successful completion of {@link Mono}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Mono> postWithResponseAsync(BinaryData body, RequestOptions requestOptions) { + final String accept = "application/json;odata.metadata=minimal"; + final String contentType = "application/json"; + return FluxUtil.withContext( + context -> service.post(this.client.getEndpoint(), accept, contentType, body, requestOptions, context)); + } + + /** + * The post operation. + *

Query Parameters

+ * + * + * + * + *
Query Parameters
NameTypeRequiredDescription
filterStringNoThe filter parameter
+ * You can add these to a request with {@link RequestOptions#addQueryParam} + *

Request Body Schema

+ * + *
+     * {@code
+     * {
+     *     name: String (Required)
+     *     timespan: Duration (Required)
+     * }
+     * }
+     * 
+ * + * @param body The body parameter. + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. + * @return the {@link Response}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Response postWithResponse(BinaryData body, RequestOptions requestOptions) { + final String accept = "application/json;odata.metadata=minimal"; + final String contentType = "application/json"; + return service.postSync(this.client.getEndpoint(), accept, contentType, body, requestOptions, Context.NONE); + } +} diff --git a/typespec-tests/src/main/java/tsptest/clientoption/implementation/package-info.java b/typespec-tests/src/main/java/tsptest/clientoption/implementation/package-info.java new file mode 100644 index 0000000000..a26ab84778 --- /dev/null +++ b/typespec-tests/src/main/java/tsptest/clientoption/implementation/package-info.java @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +/** + * + * Package containing the implementations for ClientOption. + * + */ +package tsptest.clientoption.implementation; diff --git a/typespec-tests/src/main/java/tsptest/clientoption/models/ClientRequiredRequest.java b/typespec-tests/src/main/java/tsptest/clientoption/models/ClientRequiredRequest.java new file mode 100644 index 0000000000..59ebea8171 --- /dev/null +++ b/typespec-tests/src/main/java/tsptest/clientoption/models/ClientRequiredRequest.java @@ -0,0 +1,107 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.clientoption.models; + +import com.azure.core.annotation.Generated; +import com.azure.core.annotation.Immutable; +import com.azure.core.util.CoreUtils; +import com.azure.json.JsonReader; +import com.azure.json.JsonSerializable; +import com.azure.json.JsonToken; +import com.azure.json.JsonWriter; +import java.io.IOException; +import java.time.Duration; + +/** + * The ClientRequiredRequest model. + */ +@Immutable +public final class ClientRequiredRequest implements JsonSerializable { + /* + * The name property. + */ + @Generated + private final String name; + + /* + * The timespan property. + */ + @Generated + private final Duration timespan; + + /** + * Creates an instance of ClientRequiredRequest class. + * + * @param name the name value to set. + * @param timespan the timespan value to set. + */ + @Generated + public ClientRequiredRequest(String name, Duration timespan) { + this.name = name; + this.timespan = timespan; + } + + /** + * Get the name property: The name property. + * + * @return the name value. + */ + @Generated + public String getName() { + return this.name; + } + + /** + * Get the timespan property: The timespan property. + * + * @return the timespan value. + */ + @Generated + public Duration getTimespan() { + return this.timespan; + } + + /** + * {@inheritDoc} + */ + @Generated + @Override + public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { + jsonWriter.writeStartObject(); + jsonWriter.writeStringField("name", this.name); + jsonWriter.writeStringField("timespan", CoreUtils.durationToStringWithDays(this.timespan)); + return jsonWriter.writeEndObject(); + } + + /** + * Reads an instance of ClientRequiredRequest from the JsonReader. + * + * @param jsonReader The JsonReader being read. + * @return An instance of ClientRequiredRequest if the JsonReader was pointing to an instance of it, or null if it + * was pointing to JSON null. + * @throws IllegalStateException If the deserialized JSON object was missing any required properties. + * @throws IOException If an error occurs while reading the ClientRequiredRequest. + */ + @Generated + public static ClientRequiredRequest fromJson(JsonReader jsonReader) throws IOException { + return jsonReader.readObject(reader -> { + String name = null; + Duration timespan = null; + while (reader.nextToken() != JsonToken.END_OBJECT) { + String fieldName = reader.getFieldName(); + reader.nextToken(); + + if ("name".equals(fieldName)) { + name = reader.getString(); + } else if ("timespan".equals(fieldName)) { + timespan = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString())); + } else { + reader.skipChildren(); + } + } + return new ClientRequiredRequest(name, timespan); + }); + } +} diff --git a/typespec-tests/src/main/java/tsptest/clientoption/models/package-info.java b/typespec-tests/src/main/java/tsptest/clientoption/models/package-info.java new file mode 100644 index 0000000000..5c11144ff3 --- /dev/null +++ b/typespec-tests/src/main/java/tsptest/clientoption/models/package-info.java @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +/** + * + * Package containing the data models for ClientOption. + * + */ +package tsptest.clientoption.models; diff --git a/typespec-tests/src/main/java/tsptest/clientoption/package-info.java b/typespec-tests/src/main/java/tsptest/clientoption/package-info.java new file mode 100644 index 0000000000..a7a35400a0 --- /dev/null +++ b/typespec-tests/src/main/java/tsptest/clientoption/package-info.java @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +/** + * + * Package containing the classes for ClientOption. + * + */ +package tsptest.clientoption; diff --git a/typespec-tests/src/main/resources/tsptest-clientoption.properties b/typespec-tests/src/main/resources/tsptest-clientoption.properties new file mode 100644 index 0000000000..ca812989b4 --- /dev/null +++ b/typespec-tests/src/main/resources/tsptest-clientoption.properties @@ -0,0 +1,2 @@ +name=${project.artifactId} +version=${project.version} diff --git a/typespec-tests/src/test/java/tsptest/clientoption/generated/ClientOptionClientTestBase.java b/typespec-tests/src/test/java/tsptest/clientoption/generated/ClientOptionClientTestBase.java new file mode 100644 index 0000000000..537b025afb --- /dev/null +++ b/typespec-tests/src/test/java/tsptest/clientoption/generated/ClientOptionClientTestBase.java @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.clientoption.generated; + +// The Java test files under 'generated' package are generated for your reference. +// If you wish to modify these files, please copy them out of the 'generated' package, and modify there. +// See https://aka.ms/azsdk/dpg/java/tests for guide on adding a test. + +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.test.TestMode; +import com.azure.core.test.TestProxyTestBase; +import com.azure.core.util.Configuration; +import tsptest.clientoption.ClientOptionClient; +import tsptest.clientoption.ClientOptionClientBuilder; + +class ClientOptionClientTestBase extends TestProxyTestBase { + protected ClientOptionClient clientOptionClient; + + @Override + protected void beforeTest() { + ClientOptionClientBuilder clientOptionClientbuilder = new ClientOptionClientBuilder() + .endpoint(Configuration.getGlobalConfiguration().get("ENDPOINT", "endpoint")) + .httpClient(getHttpClientOrUsePlayback(getHttpClients().findFirst().orElse(null))) + .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)); + if (getTestMode() == TestMode.RECORD) { + clientOptionClientbuilder.addPolicy(interceptorManager.getRecordPolicy()); + } + clientOptionClient = clientOptionClientbuilder.buildClient(); + + } +} diff --git a/typespec-tests/tsp/client-option.tsp b/typespec-tests/tsp/client-option.tsp new file mode 100644 index 0000000000..18f01f45e3 --- /dev/null +++ b/typespec-tests/tsp/client-option.tsp @@ -0,0 +1,30 @@ +import "@typespec/rest"; +import "@typespec/versioning"; +import "@azure-tools/typespec-azure-core"; +import "@azure-tools/typespec-client-generator-core"; + +using TypeSpec.Http; +using Azure.ClientGenerator.Core; + +@service(#{ title: "ClientOption" }) +namespace TspTest.ClientOption; + +alias ClientRequiredParameters = { + @header accept?: "application/json;odata.metadata=minimal"; + @query filter: string; +}; + +model ClientRequiredRequest { + name: string; + timespan?: duration; +} + +@route("/client-required") +interface ClientRequired { + @post + post(...ClientRequiredParameters, @body body: ClientRequiredRequest): {}; +} + +@@clientOption(ClientRequiredParameters.accept, "clientRequired", true, "java"); +@@clientOption(ClientRequiredParameters.filter, "clientRequired", false, "java"); +@@clientOption(ClientRequiredRequest.timespan, "clientRequired", true, "java");