Skip to content
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

[tcgc] .name -> .nameInClient on SdkModelPropertyTypes #445

Merged
merged 6 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/name_in_client-2024-2-19-15-2-6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: deprecation
packages:
- "@azure-tools/typespec-client-generator-core"
---

Users should call `.name` instead of `.nameInClient` on `SdkModelPropertyType`s
12 changes: 6 additions & 6 deletions packages/typespec-client-generator-core/doc/types.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ model SdkModelType extends SdkType {
* @property kind: The kind of property
* @property __raw: The original TSP property
* @property type: The type of the property
* @property nameInClient: The name of the property in our client SDKs
* @property name: The name of the property in our client SDKs
* @property description: Description for the property
* @property details: Optional details of the property
* @property apiVersions: Api versions the property is available for
Expand All @@ -260,7 +260,7 @@ model SdkModelType extends SdkType {
model SdkModelPropertyType {
__raw?: ModelProperty;
type: SdkType;
nameInClient: string;
name: string;
description?: string;
details?: string;
apiVersions: string[];
Expand Down Expand Up @@ -291,13 +291,13 @@ model SdkModelPropertyType {
* kind: "model",
* properties: [
* {
* nameInClient: "path_param",
* name: "path_param",
* serializedName: "pathParam"
* type: "string",
* kind: "path"
* },
* {
* nameInClient: "query_param",
* name: "query_param",
* name: "queryParam",
* type: "string",
* kind: "query"
Expand All @@ -307,7 +307,7 @@ model SdkModelPropertyType {
* kind: "method"
* }
* PathServiceParameter: {
* nameInClient: "pathParam",
* name: "pathParam",
* serializedName: "pathParam",
* type: "string",
* kind: "path",
Expand All @@ -317,7 +317,7 @@ model SdkModelPropertyType {
* }
* }
* QueryServiceParameter: {
* nameInClient: "queryParam",
* name: "queryParam",
* serializedName: "queryParam",
* type: "string",
* kind: "query",
Expand Down
5 changes: 5 additions & 0 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ export interface SdkCredentialType extends SdkTypeBase {
export interface SdkModelPropertyTypeBase {
__raw?: ModelProperty;
type: SdkType;
/**
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated This property is deprecated. Use `.name` instead.
* https://github.com/Azure/typespec-azure/issues/446
*/
nameInClient: string;
name: string;
description?: string;
details?: string;
apiVersions: string[];
Expand Down
29 changes: 16 additions & 13 deletions packages/typespec-client-generator-core/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ function getSdkHttpBodyParameters<TOptions extends object>(
const bodyType = diagnostics.pipe(
getClientTypeWithDiagnostics(context, tspBody.type, httpOperation.operation)
);
const name = "body";
return diagnostics.wrap([
{
kind: "body",
nameInClient: "body",
nameInClient: name,
name,
description: getDocHelper(context, tspBody.type).description,
details: getDocHelper(context, tspBody.type).details,
onClient: false,
Expand All @@ -115,7 +117,7 @@ function getSdkHttpBodyParameters<TOptions extends object>(
})
);
const methodBodyParameter = methodParameters.find(
(x) => x.nameInClient === getPropertyNames(context, tspBody.parameter!)[0]
(x) => x.name === getPropertyNames(context, tspBody.parameter!)[0]
);
if (body.kind !== "body") throw new Error("blah");
if (methodBodyParameter) {
Expand Down Expand Up @@ -149,7 +151,7 @@ function getSdkHttpBodyParameters<TOptions extends object>(
function createContentTypeOrAcceptHeader(
bodyObject: SdkBodyParameter | SdkHttpResponse
): Omit<SdkMethodParameter, "kind"> {
const nameInClient = bodyObject.kind === "body" ? "contentType" : "accept";
const name = bodyObject.kind === "body" ? "contentType" : "accept";
let type: SdkType = {
kind: "string",
encode: "string",
Expand All @@ -165,7 +167,7 @@ function createContentTypeOrAcceptHeader(
if (
bodyObject.contentTypes &&
bodyObject.contentTypes.length === 1 &&
(/json/.test(bodyObject.contentTypes[0]) || nameInClient === "accept")
(/json/.test(bodyObject.contentTypes[0]) || name === "accept")
) {
// in this case, we just want a content type of application/json
type = {
Expand All @@ -178,7 +180,8 @@ function createContentTypeOrAcceptHeader(
// No need for clientDefaultValue because it's a constant, it only has one value
return {
type,
nameInClient,
nameInClient: name,
name,
apiVersions: bodyObject.apiVersions,
isApiVersionParam: false,
onClient: false,
Expand Down Expand Up @@ -244,7 +247,7 @@ function getSdkHttpOperation<TOptions extends object>(
kind: "header",
serializedName: "Accept",
});
if (!methodParameters.some((m) => m.nameInClient === "accept")) {
if (!methodParameters.some((m) => m.name === "accept")) {
methodParameters.push({
...acceptBase,
kind: "method",
Expand Down Expand Up @@ -490,18 +493,16 @@ function getParameterMappingHelper<
if (!context.__api_version_parameter) throw new Error("No api version on the client");
return [context.__api_version_parameter];
}
const correspondingMethodParameter = method.parameters.find(
(x) => x.nameInClient === serviceParam.nameInClient
);
const correspondingMethodParameter = method.parameters.find((x) => x.name === serviceParam.name);
if (correspondingMethodParameter) {
return [correspondingMethodParameter];
}
function paramInProperties(param: SdkModelPropertyType, type: SdkType): boolean {
if (type.kind !== "model") return false;
return Array.from(type.properties.values())
.filter((x) => x.kind === "property")
.map((x) => x.nameInClient)
.includes(param.nameInClient);
.map((x) => x.name)
.includes(param.name);
}
const serviceParamType = serviceParam.type;
if (serviceParam.kind === "body" && serviceParamType.kind === "model") {
Expand All @@ -520,7 +521,7 @@ function getParameterMappingHelper<
for (const methodParam of method.parameters) {
if (methodParam.type.kind === "model") {
for (const prop of methodParam.type.properties) {
if (prop.nameInClient === serviceParam.nameInClient) {
if (prop.name === serviceParam.name) {
return [prop];
}
}
Expand Down Expand Up @@ -625,10 +626,12 @@ function getDefaultSdkEndpointParameter<
encode: "string",
};
}
const name = "endpoint";
return [
{
kind: "endpoint",
nameInClient: "endpoint",
nameInClient: name,
name,
description: "Service host",
onClient: true,
urlEncode: false,
Expand Down
16 changes: 11 additions & 5 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ function addDiscriminatorToModelType(
createDiagnostic({
code: "discriminator-not-constant",
target: type,
format: { discriminator: property.nameInClient },
format: { discriminator: property.name },
})
);
} else if (typeof property.type.value !== "string") {
Expand All @@ -429,7 +429,7 @@ function addDiscriminatorToModelType(
code: "discriminator-not-string",
target: type,
format: {
discriminator: property.nameInClient,
discriminator: property.name,
discriminatorValue: String(property.type.value),
},
})
Expand Down Expand Up @@ -469,13 +469,15 @@ function addDiscriminatorToModelType(
encode: "string",
};
}
const name = discriminator.propertyName;
model.properties.push({
kind: "property",
optional: false,
discriminator: true,
serializedName: discriminator.propertyName,
type: discriminatorType!,
nameInClient: discriminator.propertyName,
nameInClient: name,
name,
onClient: false,
apiVersions: getAvailableApiVersions(context, type),
isApiVersionParam: false,
Expand Down Expand Up @@ -933,10 +935,12 @@ export function getSdkCredentialParameter(
): SdkCredentialParameter | undefined {
const auth = getAuthentication(context.program, client.service);
if (!auth) return undefined;
const name = "credential";
return {
type: getSdkCredentialType(client, auth),
kind: "credential",
nameInClient: "credential",
nameInClient: name,
name,
description: "Credential used to authenticate requests to the service.",
apiVersions: getAvailableApiVersions(context, client.service),
onClient: true,
Expand Down Expand Up @@ -968,13 +972,15 @@ export function getSdkModelPropertyType(
propertyType = getSdkEnum(context, knownValues, options.operation);
}
const docWrapper = getDocHelper(context, type);
const name = getPropertyNames(context, type)[0];
const base = {
__raw: type,
description: docWrapper.description,
details: docWrapper.details,
apiVersions: getAvailableApiVersions(context, type),
type: propertyType,
nameInClient: getPropertyNames(context, type)[0],
nameInClient: name,
name,
onClient: false,
optional: type.optional,
};
Expand Down
Loading
Loading