Skip to content

Commit

Permalink
http-client-java, support client parameter to client (#4415)
Browse files Browse the repository at this point in the history
This is only part of the support for
Azure/autorest.java#2428

cadl-ranch Azure/cadl-ranch#727

user code
```java
        InitializationClient client = new InitializationClientBuilder()
            .name("client1")
            .buildClient();
        client.action();
```
  • Loading branch information
weidongxu-microsoft authored Sep 12, 2024
1 parent f9ae0a1 commit deae32e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
15 changes: 14 additions & 1 deletion packages/http-client-java/emitter/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1148,10 +1148,19 @@ export class CodeModelBuilder {
}
}

// TODO: use param.onClient after TCGC fix
const parameterOnClient =
!isApiVersion(this.sdkContext, param) &&
param.correspondingMethodParams &&
param.correspondingMethodParams.length > 0 &&
param.correspondingMethodParams[0].onClient;

const nullable = param.type.kind === "nullable";
const parameter = new Parameter(param.name, param.details ?? "", schema, {
summary: param.description,
implementation: ImplementationLocation.Method,
implementation: parameterOnClient
? ImplementationLocation.Client
: ImplementationLocation.Method,
required: !param.optional,
nullable: nullable,
protocol: {
Expand All @@ -1169,6 +1178,10 @@ export class CodeModelBuilder {
});
op.addParameter(parameter);

if (parameterOnClient) {
clientContext.addGlobalParameter(parameter);
}

this.trackSchemaUsage(schema, { usage: [SchemaContext.Input] });

if (op.convenienceApi) {
Expand Down
7 changes: 6 additions & 1 deletion packages/http-client-java/emitter/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export class ClientContext {
}

addGlobalParameter(parameter: Parameter) {
if (!this.globalParameters.includes(parameter)) {
if (
!this.globalParameters.includes(parameter) &&
!this.globalParameters.some(
(it) => it.language.default.name === parameter.language.default.name
)
) {
this.globalParameters.push(parameter);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,10 @@ protected List<ServiceClientProperty> processClientProperties(Client client, Str
continue;
}

String serviceClientPropertyDescription =
String description =
p.getDescription() != null ? p.getDescription() : p.getLanguage().getJava().getDescription();
String summary = p.getSummary();
description = SchemaUtil.mergeSummaryWithDescription(summary, description);

String serviceClientPropertyName = CodeNamer.getPropertyName(p.getLanguage().getJava().getName());

Expand All @@ -233,7 +235,7 @@ protected List<ServiceClientProperty> processClientProperties(Client client, Str
if (serviceClientPropertyClientType != ClassType.TOKEN_CREDENTIAL) {
ServiceClientProperty serviceClientProperty =
new ServiceClientProperty.Builder()
.description(serviceClientPropertyDescription)
.description(description)
.type(serviceClientPropertyClientType)
.name(serviceClientPropertyName)
.readOnly(serviceClientPropertyIsReadOnly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"type": "module",
"scripts": {
"clean": "rimraf ./node_modules/@typespec/http-client-java ./package-lock.json",
"clean": "rimraf ./node_modules/@typespec/http-client-java ./package-lock.json ./tsp-output",
"format": "npm run -s prettier -- --write",
"check-format": "npm run prettier -- --check",
"prettier": "prettier --config ./.prettierrc.yaml **/*.tsp",
Expand Down

0 comments on commit deae32e

Please sign in to comment.