Skip to content

Commit

Permalink
http-client-java, corner case of client without op or group (#4358)
Browse files Browse the repository at this point in the history
Found when trying to write subclass as 
```ts
@client({
  name: "ContosoClient",
  service: Cadl.ContosoServer,
})
namespace Cadl.ContosoServer {
  @client({
    name: "ContosoSubClient",
    service: Cadl.ContosoServer,
  })
  namespace SubClient {
    @client({
      name: "ContosoSubSubClient",
      service: Cadl.ContosoServer,
    })
    @route("/contoso/")
    interface ServerOp {
      get(@path(#{ allowReserved: true }) group: string): OkResponse | NoContentResponse;
    }
  }
}
```

<-- this likely not the correct way to write subclient (as Client be
very different from subclient from nested namespace), but at least
emitter should not throw

After this PR, above tsp would result in `ContosoSubSubClient` client,
as this is the only one containing operation.
  • Loading branch information
weidongxu-microsoft authored Sep 10, 2024
1 parent 5a55338 commit 27c263e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
15 changes: 7 additions & 8 deletions packages/http-client-java/emitter/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,12 @@ export class CodeModelBuilder {
}

const service = listServices(this.program)[0];
const serviceNamespace = service.type;
if (serviceNamespace === undefined) {
throw Error("Cannot emit yaml for a namespace that doesn't exist.");
if (!service) {
throw Error("TypeSpec for HTTP must define a service.");
}
this.serviceNamespace = serviceNamespace;
this.serviceNamespace = service.type;

this.namespace = getNamespaceFullName(serviceNamespace) || "Azure.Client";
this.namespace = getNamespaceFullName(this.serviceNamespace) || "Azure.Client";
// java namespace
const javaNamespace = this.getJavaNamespace(this.namespace);

Expand All @@ -219,9 +218,9 @@ export class CodeModelBuilder {
};

// init code model
const title = this.options["service-name"] ?? serviceNamespace.name;
const title = this.options["service-name"] ?? this.serviceNamespace.name;

const description = this.getDoc(serviceNamespace);
const description = this.getDoc(this.serviceNamespace);
this.codeModel = new CodeModel(title, false, {
info: {
description: description,
Expand All @@ -230,7 +229,7 @@ export class CodeModelBuilder {
default: {
name: title,
description: description,
summary: this.getSummary(serviceNamespace),
summary: this.getSummary(this.serviceNamespace),
namespace: this.namespace,
},
java: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,11 @@ public Client map(CodeModel codeModel) {
} else {
// service client
ServiceClient serviceClient = Mappers.getServiceClientMapper().map(codeModel);
builder.serviceClient(serviceClient);
if (serviceClient != null) {
builder.serviceClient(serviceClient);

serviceClientsMap.put(serviceClient, codeModel);
serviceClientsMap.put(serviceClient, codeModel);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ public CodeModel transform(CodeModel codeModel) {
markFlattenedSchemas(codeModel);
}
transformOperationGroups(codeModel.getOperationGroups(), codeModel);
if (codeModel.getGlobalParameters() != null) {
for (Parameter parameter : codeModel.getGlobalParameters()) {
if (parameter.getLanguage().getJava() == null) {
renameVariable(parameter);
}
}
}
// multi-clients for TypeSpec
if (codeModel.getClients() != null) {
transformClients(codeModel.getClients(), codeModel);
transformClients(codeModel.getClients());
}
return codeModel;
}
Expand Down Expand Up @@ -108,7 +115,7 @@ private void transformSchemas(Schemas schemas) {
}
}

private void transformClients(List<Client> clients, CodeModel codeModel) {
private void transformClients(List<Client> clients) {
for (Client client : clients) {
renameClient(client);

Expand Down Expand Up @@ -138,6 +145,14 @@ private void transformClients(List<Client> clients, CodeModel codeModel) {
}
}
}

if (client.getGlobalParameters() != null) {
for (Parameter parameter : client.getGlobalParameters()) {
if (parameter.getLanguage().getJava() == null) {
renameVariable(parameter);
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ protected Map<ServiceClient, Client> processClients(List<Client> clients, CodeMo
Map<ServiceClient, Client> serviceClientsMap = new LinkedHashMap<>();
TypeSpecServiceClientMapper mapper = new TypeSpecServiceClientMapper();
for (Client client : clients) {
serviceClientsMap.put(mapper.map(client, codeModel), client);
ServiceClient serviceClient = mapper.map(client, codeModel);
if (serviceClient != null) {
serviceClientsMap.put(serviceClient, client);
}
}
return serviceClientsMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public ServiceClient map(Client client, CodeModel codeModel) {
.forEach(og -> methodGroupClients.add(Mappers.getMethodGroupMapper().map(og, properties)));
builder.methodGroupClients(methodGroupClients);

if (proxy == null && CoreUtils.isNullOrEmpty(methodGroupClients)) {
// No operation in this client, and no operation group as well. Abort the processing.
return null;
}

if (proxy == null) {
proxy = methodGroupClients.iterator().next().getProxy();
}
Expand Down

0 comments on commit 27c263e

Please sign in to comment.