Skip to content

Commit

Permalink
http-client-java, refactor crossLanguageDefinitionId (#5429)
Browse files Browse the repository at this point in the history
code-model, move it to `Language`

Visible effect is that sync/async client should now have a
crossLanguageDefinitionId.
e.g.
```
    "todo.TodoItemsClient": "Todo.TodoItems",
```

Previous, it is null, e.g.

https://github.com/allenjzhang/typespec-e2e-demo/blob/main/todoApp/clients/java/src/main/resources/META-INF/todo_apiview_properties.json#L25

PS: should it be `null` instead of `"null"`?
  • Loading branch information
weidongxu-microsoft authored Dec 23, 2024
1 parent f70cf37 commit cfb11a1
Show file tree
Hide file tree
Showing 73 changed files with 537 additions and 577 deletions.
31 changes: 15 additions & 16 deletions packages/http-client-java/emitter/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ import { getSegment } from "@typespec/rest";
import { getAddedOnVersions } from "@typespec/versioning";
import { fail } from "assert";
import pkg from "lodash";
import {
Client as CodeModelClient,
CrossLanguageDefinition,
EncodedSchema,
} from "./common/client.js";
import { Client as CodeModelClient, EncodedSchema } from "./common/client.js";
import { CodeModel } from "./common/code-model.js";
import { LongRunningMetadata } from "./common/long-running-metadata.js";
import { Operation as CodeModelOperation, ConvenienceApi, Request } from "./common/operation.js";
Expand Down Expand Up @@ -549,7 +545,7 @@ export class CodeModelBuilder {
// at present, use global security definition
security: this.codeModel.security,
});
codeModelClient.crossLanguageDefinitionId = client.crossLanguageDefinitionId;
codeModelClient.language.default.crossLanguageDefinitionId = client.crossLanguageDefinitionId;

// versioning
const versions = client.apiVersions;
Expand Down Expand Up @@ -616,6 +612,7 @@ export class CodeModelBuilder {
// operations without operation group
const serviceMethodsWithoutSubClient = this.listServiceMethodsUnderClient(client);
let codeModelGroup = new OperationGroup("");
codeModelGroup.language.default.crossLanguageDefinitionId = client.crossLanguageDefinitionId;
for (const serviceMethod of serviceMethodsWithoutSubClient) {
if (!this.needToSkipProcessingOperation(serviceMethod.__raw, clientContext)) {
codeModelGroup.addOperation(this.processOperation(serviceMethod, clientContext, ""));
Expand All @@ -639,6 +636,8 @@ export class CodeModelBuilder {
// operation group with no operation is skipped
if (serviceMethods.length > 0) {
codeModelGroup = new OperationGroup(subClient.name);
codeModelGroup.language.default.crossLanguageDefinitionId =
subClient.crossLanguageDefinitionId;
for (const serviceMethod of serviceMethods) {
if (!this.needToSkipProcessingOperation(serviceMethod.__raw, clientContext)) {
codeModelGroup.addOperation(
Expand Down Expand Up @@ -817,7 +816,7 @@ export class CodeModelBuilder {
},
});

(codeModelOperation as CrossLanguageDefinition).crossLanguageDefinitionId =
codeModelOperation.language.default.crossLanguageDefinitionId =
sdkMethod.crossLanguageDefintionId;
codeModelOperation.internalApi = sdkMethod.access === "internal";

Expand Down Expand Up @@ -2017,7 +2016,7 @@ export class CodeModelBuilder {
},
},
});
schema.crossLanguageDefinitionId = type.crossLanguageDefinitionId;
schema.language.default.crossLanguageDefinitionId = type.crossLanguageDefinitionId;
return this.codeModel.schemas.add(schema);
}

Expand Down Expand Up @@ -2117,8 +2116,7 @@ export class CodeModelBuilder {
},
},
});
(objectSchema as CrossLanguageDefinition).crossLanguageDefinitionId =
type.crossLanguageDefinitionId;
objectSchema.language.default.crossLanguageDefinitionId = type.crossLanguageDefinitionId;
this.codeModel.schemas.add(objectSchema);

// cache this now before we accidentally recurse on this type.
Expand Down Expand Up @@ -2541,17 +2539,18 @@ export class CodeModelBuilder {
const clientNamespace: string | undefined = type?.clientNamespace;

if (type) {
const crossLanguageDefinitionId = type.crossLanguageDefinitionId;
if (this.isBranded()) {
// special handling for namespace of model that cannot be mapped to azure-core
if (type.crossLanguageDefinitionId === "TypeSpec.Http.File") {
if (crossLanguageDefinitionId === "TypeSpec.Http.File") {
// TypeSpec.Http.File
return this.baseJavaNamespace;
} else if (type.crossLanguageDefinitionId === "Azure.Core.Foundations.OperationState") {
} else if (crossLanguageDefinitionId === "Azure.Core.Foundations.OperationState") {
// Azure.Core.OperationState
return this.baseJavaNamespace;
} else if (
type.crossLanguageDefinitionId === "Azure.Core.ResourceOperationStatus" ||
type.crossLanguageDefinitionId === "Azure.Core.Foundations.OperationStatus"
crossLanguageDefinitionId === "Azure.Core.ResourceOperationStatus" ||
crossLanguageDefinitionId === "Azure.Core.Foundations.OperationStatus"
) {
// Azure.Core.ResourceOperationStatus<>
// Azure.Core.Foundations.OperationStatus<>
Expand All @@ -2563,10 +2562,10 @@ export class CodeModelBuilder {
}
} else {
// special handling for namespace of model in TypeSpec
if (type.crossLanguageDefinitionId === "TypeSpec.Http.File") {
if (crossLanguageDefinitionId === "TypeSpec.Http.File") {
// TypeSpec.Http.File
return this.baseJavaNamespace;
} else if (type.crossLanguageDefinitionId.startsWith("TypeSpec.Rest.Resource.")) {
} else if (crossLanguageDefinitionId.startsWith("TypeSpec.Rest.Resource.")) {
// models in TypeSpec.Rest.Resource
return this.baseJavaNamespace;
}
Expand Down
6 changes: 1 addition & 5 deletions packages/http-client-java/emitter/src/common/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Aspect, Metadata, OperationGroup, Parameter, Security } from "@autorest/codemodel";
import { DeepPartial } from "@azure-tools/codegen";

export interface Client extends Aspect, CrossLanguageDefinition {
export interface Client extends Aspect {
/** All operations */
operationGroups: Array<OperationGroup>;

Expand Down Expand Up @@ -67,10 +67,6 @@ export class ServiceVersion extends Metadata {
}
}

export interface CrossLanguageDefinition {
crossLanguageDefinitionId?: string;
}

export interface EncodedSchema {
encode?: string;
}
3 changes: 1 addition & 2 deletions packages/http-client-java/emitter/src/common/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {
SchemaType,
} from "@autorest/codemodel";
import { DeepPartial } from "@azure-tools/codegen";
import { CrossLanguageDefinition } from "./client.js";
import { LongRunningMetadata } from "./long-running-metadata.js";

/** represents a single callable endpoint with a discrete set of inputs, and any number of output possibilities (responses or exceptions) */
export interface Operation extends Aspect, CrossLanguageDefinition {
export interface Operation extends Aspect {
/**
* Original Operation ID if present.
* This can be used to identify the original id of an operation before it is styled.
Expand Down
8 changes: 3 additions & 5 deletions packages/http-client-java/emitter/src/external-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
SdkModelType,
SdkType,
} from "@azure-tools/typespec-client-generator-core";
import { CrossLanguageDefinition } from "./common/client.js";
import { getNamespace, pascalCase } from "./utils.js";

/*
Expand All @@ -36,8 +35,7 @@ export function createResponseErrorSchema(
},
},
});
(responseErrorSchema as CrossLanguageDefinition).crossLanguageDefinitionId =
"Azure.Core.Foundations.Error";
responseErrorSchema.language.default.crossLanguageDefinitionId = "Azure.Core.Foundations.Error";

schemas.add(responseErrorSchema);
responseErrorSchema.addProperty(
Expand Down Expand Up @@ -117,7 +115,7 @@ export function createResponseInnerErrorSchema(
},
},
);
(responseInnerErrorSchema as CrossLanguageDefinition).crossLanguageDefinitionId =
responseInnerErrorSchema.language.default.crossLanguageDefinitionId =
"Azure.Core.Foundations.InnerError";

schemas.add(responseInnerErrorSchema);
Expand Down Expand Up @@ -341,7 +339,7 @@ export function getFileDetailsSchema(
fileDetailsSchema.language.default.description = fileSdkType.doc;
}
// crossLanguageDefinitionId
(fileDetailsSchema as CrossLanguageDefinition).crossLanguageDefinitionId =
fileDetailsSchema.language.default.crossLanguageDefinitionId =
fileSdkType.crossLanguageDefinitionId;

let contentTypeProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class ChoiceSchema extends ValueSchema {
private Schema choiceType;
private List<ChoiceValue> choices = new ArrayList<>();
private String summary;
private String crossLanguageDefinitionId;

/**
* Creates a new instance of the ChoiceSchema class.
Expand Down Expand Up @@ -73,24 +72,6 @@ public void setSummary(String summary) {
this.summary = summary;
}

/**
* Gets the cross-language definition id.
*
* @return The cross-language definition id.
*/
public String getCrossLanguageDefinitionId() {
return crossLanguageDefinitionId;
}

/**
* Sets the cross-language definition id.
*
* @param crossLanguageDefinitionId The cross-language definition id.
*/
public void setCrossLanguageDefinitionId(String crossLanguageDefinitionId) {
this.crossLanguageDefinitionId = crossLanguageDefinitionId;
}

@Override
public String toString() {
return sharedToString(this, ChoiceSchema.class.getName());
Expand Down Expand Up @@ -137,8 +118,7 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
JsonWriter writeParentProperties(JsonWriter jsonWriter) throws IOException {
return super.writeParentProperties(jsonWriter).writeJsonField("choiceType", choiceType)
.writeArrayField("choices", choices, JsonWriter::writeJson)
.writeStringField("summary", summary)
.writeStringField("crossLanguageDefinitionId", crossLanguageDefinitionId);
.writeStringField("summary", summary);
}

/**
Expand Down Expand Up @@ -168,9 +148,6 @@ boolean tryConsumeParentProperties(ChoiceSchema schema, String fieldName, JsonRe
} else if ("summary".equals(fieldName)) {
schema.summary = reader.getString();
return true;
} else if ("crossLanguageDefinitionId".equals(fieldName)) {
schema.crossLanguageDefinitionId = reader.getString();
return true;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class Client extends Metadata {
private Security security;
private List<ApiVersion> apiVersions = new ArrayList<>();
private ServiceVersion serviceVersion;
private String crossLanguageDefinitionId;
private Client parent;
private List<Client> subClients = Collections.emptyList();
private boolean buildMethodPublic = true;
Expand Down Expand Up @@ -142,24 +141,6 @@ public void setServiceVersion(ServiceVersion serviceVersion) {
this.serviceVersion = serviceVersion;
}

/**
* Gets the cross-language definition id of the client.
*
* @return The cross-language definition id of the client.
*/
public String getCrossLanguageDefinitionId() {
return crossLanguageDefinitionId;
}

/**
* Sets the cross-language definition id of the client.
*
* @param crossLanguageDefinitionId The cross-language definition id of the client.
*/
public void setCrossLanguageDefinitionId(String crossLanguageDefinitionId) {
this.crossLanguageDefinitionId = crossLanguageDefinitionId;
}

public Client getParent() {
return parent;
}
Expand Down Expand Up @@ -204,7 +185,6 @@ JsonWriter writeParentProperties(JsonWriter jsonWriter) throws IOException {
.writeJsonField("security", security)
.writeArrayField("apiVersions", apiVersions, JsonWriter::writeJson)
.writeJsonField("serviceVersion", serviceVersion)
.writeStringField("crossLanguageDefinitionId", crossLanguageDefinitionId)
.writeJsonField("parent", parent)
.writeArrayField("subClients", subClients, JsonWriter::writeJson)
.writeBooleanField("buildMethodPublic", buildMethodPublic)
Expand Down Expand Up @@ -247,9 +227,6 @@ boolean tryConsumeParentProperties(Client client, String fieldName, JsonReader r
} else if ("serviceVersion".equals(fieldName)) {
client.serviceVersion = ServiceVersion.fromJson(reader);
return true;
} else if ("crossLanguageDefinitionId".equals(fieldName)) {
client.crossLanguageDefinitionId = reader.getString();
return true;
} else if ("parent".equals(fieldName)) {
client.parent = Client.fromJson(reader);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Language implements JsonSerializable<Language> {
private String description;
private String summary;
private String namespace;
private String crossLanguageDefinitionId;
private String comment;

/**
Expand Down Expand Up @@ -135,6 +136,24 @@ public void setComment(String comment) {
this.comment = comment;
}

/**
* Gets the crossLanguageDefinitionId.
*
* @return The crossLanguageDefinitionId.
*/
public String getCrossLanguageDefinitionId() {
return crossLanguageDefinitionId;
}

/**
* Sets the crossLanguageDefinitionId.
*
* @param crossLanguageDefinitionId The crossLanguageDefinitionId.
*/
public void setCrossLanguageDefinitionId(String crossLanguageDefinitionId) {
this.crossLanguageDefinitionId = crossLanguageDefinitionId;
}

@Override
public String toString() {
return "Language{name='" + name + "', serializedName='" + serializedName + "'}";
Expand All @@ -148,6 +167,7 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
.writeStringField("description", description)
.writeStringField("summary", summary)
.writeStringField("namespace", namespace)
.writeStringField("crossLanguageDefinitionId", crossLanguageDefinitionId)
.writeStringField("comment", comment)
.writeEndObject();
}
Expand All @@ -171,6 +191,8 @@ public static Language fromJson(JsonReader jsonReader) throws IOException {
language.summary = reader.getString();
} else if ("namespace".equals(fieldName)) {
language.namespace = reader.getString();
} else if ("crossLanguageDefinitionId".equals(fieldName)) {
language.crossLanguageDefinitionId = reader.getString();
} else if ("comment".equals(fieldName)) {
language.comment = reader.getString();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class ObjectSchema extends ComplexSchema {
private boolean flattenedSchema;
// internal use, not from modelerfour
private boolean stronglyTypedHeader;
private String crossLanguageDefinitionId;

/**
* Creates a new instance of the ObjectSchema class.
Expand Down Expand Up @@ -195,24 +194,6 @@ public void setStronglyTypedHeader(boolean stronglyTypedHeader) {
this.stronglyTypedHeader = stronglyTypedHeader;
}

/**
* Gets the cross-language definition ID for this object.
*
* @return The cross-language definition ID for this object.
*/
public String getCrossLanguageDefinitionId() {
return crossLanguageDefinitionId;
}

/**
* Sets the cross-language definition ID for this object.
*
* @param crossLanguageDefinitionId The cross-language definition ID for this object.
*/
public void setCrossLanguageDefinitionId(String crossLanguageDefinitionId) {
this.crossLanguageDefinitionId = crossLanguageDefinitionId;
}

@Override
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
return super.writeParentProperties(jsonWriter.writeStartObject()).writeJsonField("discriminator", discriminator)
Expand Down Expand Up @@ -256,8 +237,6 @@ public static ObjectSchema fromJson(JsonReader jsonReader) throws IOException {
schema.flattenedSchema = reader.getBoolean();
} else if ("stronglyTypedHeader".equals(fieldName)) {
schema.stronglyTypedHeader = reader.getBoolean();
} else if ("crossLanguageDefinitionId".equals(fieldName)) {
schema.crossLanguageDefinitionId = reader.getString();
} else {
reader.skipChildren();
}
Expand Down
Loading

0 comments on commit cfb11a1

Please sign in to comment.