Skip to content

[http-client-java] make Accept header with constant @clientDefaultValue required#10179

Draft
XiaofeiCao wants to merge 5 commits intomicrosoft:mainfrom
XiaofeiCao:accept_header_required
Draft

[http-client-java] make Accept header with constant @clientDefaultValue required#10179
XiaofeiCao wants to merge 5 commits intomicrosoft:mainfrom
XiaofeiCao:accept_header_required

Conversation

@XiaofeiCao
Copy link
Copy Markdown
Member

XiaofeiCao and others added 4 commits March 27, 2026 18:38
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:java Issue for the Java client emitter: @typespec/http-client-java label Mar 27, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 27, 2026

All changed packages have been documented.

  • @typespec/http-client-java
Show changes

@typespec/http-client-java - feature ✏️

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.

@azure-sdk
Copy link
Copy Markdown
Collaborator

azure-sdk commented Mar 27, 2026

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

// treat as required constant — always sent, hidden from public API.
const isAcceptConstantWithDefault =
param.kind === "header" &&
param.serializedName.toLowerCase() === "accept" &&
Copy link
Copy Markdown
Member

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 clientDefaultValue should work the same way everywhere.

Copy link
Copy Markdown
Member Author

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 @clientDefaultValue in general.
A bit concern from me is that we are expanding our support for @clientDefaultValue a bit too far.
Or do you think we should support @clientDefaultValue in general?

Copy link
Copy Markdown
Contributor

@weidongxu-microsoft weidongxu-microsoft Apr 2, 2026

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

@query
@clientDefaultValue("instanceView")
expand?: "instanceView";

I think emitter would generate API like

Paged<> list() {
  Expand expand = Expand.INSTANCE_VIEW;
  RequestOptions requestOptions = new RequestOptions();
  if (expand != null) {
    requestOptions.addQuery("expand", expand);
  }
  return list(requestOptions);
}

Paged<> list(Expand expand) {
  RequestOptions requestOptions = new RequestOptions();
  if (expand != null) {
    requestOptions.addQuery("expand", expand);
  }
  return list(requestOptions);
}

So if user calls list((Expand) null), they can send the request without expand parameter.


Hence in this Accept case, we are doing special process here. Given that TCGC would always give us a generated Accept parameter (if op has response body and TypeSpec does not define the header), it seems OK for us to use the only non-null Accept value.

But this seems not applicable to other header/query param.

retrieve(
@header
@Azure.ClientGenerator.Core.Legacy.clientDefaultValue("application/json;odata.metadata=minimal")
accept?: "application/json;odata.metadata=minimal",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if this is an enum with multiple values? How do we handle clientDefaultValue in such cases or is that going to be an error?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR only handles constant optional case, not including enum case. If it's regular enum, we will ignore @clientDefaultValue and treat it as regular property.
Another thought, do we want to also make it required in such case? Is there a real use case for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:java Issue for the Java client emitter: @typespec/http-client-java

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[http-client-java] emitter support for constant optional parameter with @clientDefaultValue

4 participants