-
Notifications
You must be signed in to change notification settings - Fork 354
[http-client-java] make Accept header with constant @clientDefaultValue required
#10179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
XiaofeiCao
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
XiaofeiCao:accept_header_required
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.chronus/changes/accept-constant-clientdefaultvalue-2026-03-27.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/http-client-java" | ||
| --- | ||
|
|
||
| Emitter support for constant optional Accept header with `@clientDefaultValue`. When an Accept header is optional with a constant type and has `@Legacy.clientDefaultValue`, the emitter now treats it as a required constant, ensuring the value is always sent and hidden from the public API. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...ava/tsptest/specialheaders/ConstantAcceptHeaderWithDefaultValueAsRequiredAsyncClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| // Code generated by Microsoft (R) TypeSpec Code Generator. | ||
|
|
||
| package tsptest.specialheaders; | ||
|
|
||
| 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.FluxUtil; | ||
| import reactor.core.publisher.Mono; | ||
| import tsptest.specialheaders.implementation.ConstantAcceptHeaderWithDefaultValueAsRequiredsImpl; | ||
|
|
||
| /** | ||
| * Initializes a new instance of the asynchronous SpecialHeadersClient type. | ||
| */ | ||
| @ServiceClient(builder = SpecialHeadersClientBuilder.class, isAsync = true) | ||
| public final class ConstantAcceptHeaderWithDefaultValueAsRequiredAsyncClient { | ||
| @Generated | ||
| private final ConstantAcceptHeaderWithDefaultValueAsRequiredsImpl serviceClient; | ||
|
|
||
| /** | ||
| * Initializes an instance of ConstantAcceptHeaderWithDefaultValueAsRequiredAsyncClient class. | ||
| * | ||
| * @param serviceClient the service client implementation. | ||
| */ | ||
| @Generated | ||
| ConstantAcceptHeaderWithDefaultValueAsRequiredAsyncClient( | ||
| ConstantAcceptHeaderWithDefaultValueAsRequiredsImpl serviceClient) { | ||
| this.serviceClient = serviceClient; | ||
| } | ||
|
|
||
| /** | ||
| * Accept header with constant type and default value should be marked as required, before we support | ||
| * clientDefaultValue. Issue: https://github.com/microsoft/typespec/issues/10178. | ||
| * | ||
| * @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<Response<Void>> retrieveWithResponse(RequestOptions requestOptions) { | ||
| return this.serviceClient.retrieveWithResponseAsync(requestOptions); | ||
| } | ||
|
|
||
| /** | ||
| * Accept header with constant type and default value should be marked as required, before we support | ||
| * clientDefaultValue. Issue: https://github.com/microsoft/typespec/issues/10178. | ||
| * | ||
| * @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<Void> retrieve() { | ||
| // Generated convenience method for retrieveWithResponse | ||
| RequestOptions requestOptions = new RequestOptions(); | ||
| return retrieveWithResponse(requestOptions).flatMap(FluxUtil::toMono); | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
...ain/java/tsptest/specialheaders/ConstantAcceptHeaderWithDefaultValueAsRequiredClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| // Code generated by Microsoft (R) TypeSpec Code Generator. | ||
|
|
||
| package tsptest.specialheaders; | ||
|
|
||
| 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 tsptest.specialheaders.implementation.ConstantAcceptHeaderWithDefaultValueAsRequiredsImpl; | ||
|
|
||
| /** | ||
| * Initializes a new instance of the synchronous SpecialHeadersClient type. | ||
| */ | ||
| @ServiceClient(builder = SpecialHeadersClientBuilder.class) | ||
| public final class ConstantAcceptHeaderWithDefaultValueAsRequiredClient { | ||
| @Generated | ||
| private final ConstantAcceptHeaderWithDefaultValueAsRequiredsImpl serviceClient; | ||
|
|
||
| /** | ||
| * Initializes an instance of ConstantAcceptHeaderWithDefaultValueAsRequiredClient class. | ||
| * | ||
| * @param serviceClient the service client implementation. | ||
| */ | ||
| @Generated | ||
| ConstantAcceptHeaderWithDefaultValueAsRequiredClient( | ||
| ConstantAcceptHeaderWithDefaultValueAsRequiredsImpl serviceClient) { | ||
| this.serviceClient = serviceClient; | ||
| } | ||
|
|
||
| /** | ||
| * Accept header with constant type and default value should be marked as required, before we support | ||
| * clientDefaultValue. Issue: https://github.com/microsoft/typespec/issues/10178. | ||
| * | ||
| * @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<Void> retrieveWithResponse(RequestOptions requestOptions) { | ||
| return this.serviceClient.retrieveWithResponse(requestOptions); | ||
| } | ||
|
|
||
| /** | ||
| * Accept header with constant type and default value should be marked as required, before we support | ||
| * clientDefaultValue. Issue: https://github.com/microsoft/typespec/issues/10178. | ||
| * | ||
| * @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 retrieve() { | ||
| // Generated convenience method for retrieveWithResponse | ||
| RequestOptions requestOptions = new RequestOptions(); | ||
| retrieveWithResponse(requestOptions).getValue(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...st/specialheaders/implementation/ConstantAcceptHeaderWithDefaultValueAsRequiredsImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| // Code generated by Microsoft (R) TypeSpec Code Generator. | ||
|
|
||
| package tsptest.specialheaders.implementation; | ||
|
|
||
| import com.azure.core.annotation.ExpectedResponses; | ||
| import com.azure.core.annotation.Get; | ||
| import com.azure.core.annotation.HeaderParam; | ||
| import com.azure.core.annotation.Host; | ||
| import com.azure.core.annotation.HostParam; | ||
| 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.Context; | ||
| import com.azure.core.util.FluxUtil; | ||
| import reactor.core.publisher.Mono; | ||
| import tsptest.specialheaders.SpecialHeadersServiceVersion; | ||
|
|
||
| /** | ||
| * An instance of this class provides access to all the operations defined in | ||
| * ConstantAcceptHeaderWithDefaultValueAsRequireds. | ||
| */ | ||
| public final class ConstantAcceptHeaderWithDefaultValueAsRequiredsImpl { | ||
| /** | ||
| * The proxy service used to perform REST calls. | ||
| */ | ||
| private final ConstantAcceptHeaderWithDefaultValueAsRequiredsService service; | ||
|
|
||
| /** | ||
| * The service client containing this operation class. | ||
| */ | ||
| private final SpecialHeadersClientImpl client; | ||
|
|
||
| /** | ||
| * Initializes an instance of ConstantAcceptHeaderWithDefaultValueAsRequiredsImpl. | ||
| * | ||
| * @param client the instance of the service client containing this operation class. | ||
| */ | ||
| ConstantAcceptHeaderWithDefaultValueAsRequiredsImpl(SpecialHeadersClientImpl client) { | ||
| this.service = RestProxy.create(ConstantAcceptHeaderWithDefaultValueAsRequiredsService.class, | ||
| client.getHttpPipeline(), client.getSerializerAdapter()); | ||
| this.client = client; | ||
| } | ||
|
|
||
| /** | ||
| * Gets Service version. | ||
| * | ||
| * @return the serviceVersion value. | ||
| */ | ||
| public SpecialHeadersServiceVersion getServiceVersion() { | ||
| return client.getServiceVersion(); | ||
| } | ||
|
|
||
| /** | ||
| * The interface defining all the services for SpecialHeadersClientConstantAcceptHeaderWithDefaultValueAsRequireds | ||
| * to be used by the proxy service to perform REST calls. | ||
| */ | ||
| @Host("{endpoint}") | ||
| @ServiceInterface(name = "SpecialHeadersClientConstantAcceptHeaderWithDefaultValueAsRequireds") | ||
| public interface ConstantAcceptHeaderWithDefaultValueAsRequiredsService { | ||
| @Get("/constant-optional-accept-header-with-default-value") | ||
| @ExpectedResponses({ 200 }) | ||
| @UnexpectedResponseExceptionType(value = ClientAuthenticationException.class, code = { 401 }) | ||
| @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) | ||
| @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) | ||
| @UnexpectedResponseExceptionType(HttpResponseException.class) | ||
| Mono<Response<Void>> retrieve(@HostParam("endpoint") String endpoint, @HeaderParam("accept") String accept, | ||
| RequestOptions requestOptions, Context context); | ||
|
|
||
| @Get("/constant-optional-accept-header-with-default-value") | ||
| @ExpectedResponses({ 200 }) | ||
| @UnexpectedResponseExceptionType(value = ClientAuthenticationException.class, code = { 401 }) | ||
| @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) | ||
| @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) | ||
| @UnexpectedResponseExceptionType(HttpResponseException.class) | ||
| Response<Void> retrieveSync(@HostParam("endpoint") String endpoint, @HeaderParam("accept") String accept, | ||
| RequestOptions requestOptions, Context context); | ||
| } | ||
|
|
||
| /** | ||
| * Accept header with constant type and default value should be marked as required, before we support | ||
| * clientDefaultValue. Issue: https://github.com/microsoft/typespec/issues/10178. | ||
| * | ||
| * @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<Response<Void>> retrieveWithResponseAsync(RequestOptions requestOptions) { | ||
| final String accept = "application/json;odata.metadata=minimal"; | ||
| return FluxUtil | ||
| .withContext(context -> service.retrieve(this.client.getEndpoint(), accept, requestOptions, context)); | ||
| } | ||
|
|
||
| /** | ||
| * Accept header with constant type and default value should be marked as required, before we support | ||
| * clientDefaultValue. Issue: https://github.com/microsoft/typespec/issues/10178. | ||
| * | ||
| * @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<Void> retrieveWithResponse(RequestOptions requestOptions) { | ||
| final String accept = "application/json;odata.metadata=minimal"; | ||
| return service.retrieveSync(this.client.getEndpoint(), accept, requestOptions, Context.NONE); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this specific to "accept" header only? We should make this generic and
clientDefaultValueshould work the same way everywhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kind like a "hack" to support Search case before we support
@clientDefaultValuein general.A bit concern from me is that we are expanding our support for
@clientDefaultValuea bit too far.Or do you think we should support
@clientDefaultValuein general?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a normal e.g. query parameter
I think emitter would generate API like
So if user calls
list((Expand) null), they can send the request withoutexpandparameter.Hence in this
Acceptcase, we are doing special process here. Given that TCGC would always give us a generatedAcceptparameter (if op has response body and TypeSpec does not define the header), it seems OK for us to use the only non-nullAcceptvalue.But this seems not applicable to other header/query param.