Skip to content

Commit b804754

Browse files
tsp, use TCGC's name for model property schema name (#2787)
* use TCGC's name for model property schema name and union * regen * remove unused functions * change the union schema's name back * use tcgc name for constant schema * fix tests * remove the logic of getting name from raw model and scalar * regen * clean up unused function
1 parent 400d3cc commit b804754

File tree

58 files changed

+379
-446
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+379
-446
lines changed

typespec-extension/src/code-model-builder.ts

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ import {
9595
getOverloadedOperation,
9696
getProjectedName,
9797
getSummary,
98-
getTypeName,
9998
getVisibility,
10099
ignoreDiagnostics,
101100
isArrayModelType,
@@ -156,8 +155,6 @@ import {
156155
ProcessingCache,
157156
getAccess,
158157
getDurationFormatFromSdkType,
159-
getNameForTemplate,
160-
getNamePrefixForProperty,
161158
getUnionDescription,
162159
getUsage,
163160
hasScalarAsBase,
@@ -1350,7 +1347,8 @@ export class CodeModelBuilder {
13501347
}
13511348

13521349
const isAnonymousModel = sdkType.kind === "model" && sdkType.isGeneratedName === true;
1353-
const parameter = new Parameter(this.getName(body), this.getDoc(body), schema, {
1350+
const parameterName = body.kind === "Model" ? (sdkType.kind === "model" ? sdkType.name : "") : this.getName(body);
1351+
const parameter = new Parameter(parameterName, this.getDoc(body), schema, {
13541352
summary: this.getSummary(body),
13551353
implementation: ImplementationLocation.Method,
13561354
required: body.kind === "Model" || !body.optional,
@@ -1598,10 +1596,10 @@ export class CodeModelBuilder {
15981596
}
15991597
if (match) {
16001598
schema = candidateResponseSchema;
1599+
const sdkType = getClientType(this.sdkContext, bodyType);
1600+
const modelName = sdkType.kind === "model" ? sdkType.name : undefined;
16011601
this.trace(
1602-
`Replace TypeSpec model '${this.getName(bodyType)}' with '${
1603-
candidateResponseSchema.language.default.name
1604-
}'`,
1602+
`Replace TypeSpec model '${modelName}' with '${candidateResponseSchema.language.default.name}'`,
16051603
);
16061604
}
16071605
}
@@ -1890,7 +1888,7 @@ export class CodeModelBuilder {
18901888

18911889
const schemaType = type.isFixed ? SealedChoiceSchema : ChoiceSchema;
18921890

1893-
const schema = new schemaType(type.name ? type.name : name, type.details ?? "", {
1891+
const schema = new schemaType(type.name ?? name, type.details ?? "", {
18941892
summary: type.description,
18951893
choiceType: valueType as any,
18961894
choices: choices,
@@ -1911,7 +1909,7 @@ export class CodeModelBuilder {
19111909
const valueType = this.processSchemaFromSdkType(type.valueType, type.valueType.kind);
19121910

19131911
return this.codeModel.schemas.add(
1914-
new ConstantSchema(name, type.details ?? "", {
1912+
new ConstantSchema(type.name ?? name, type.details ?? "", {
19151913
summary: type.description,
19161914
valueType: valueType,
19171915
value: new ConstantValue(type.value),
@@ -1923,7 +1921,7 @@ export class CodeModelBuilder {
19231921
const valueType = this.processSchemaFromSdkType(type.enumType, type.enumType.name);
19241922

19251923
return this.codeModel.schemas.add(
1926-
new ConstantSchema(name, type.details ?? "", {
1924+
new ConstantSchema(type.name ?? name, type.details ?? "", {
19271925
summary: type.description,
19281926
valueType: valueType,
19291927
value: new ConstantValue(type.value ?? type.name),
@@ -2091,10 +2089,7 @@ export class CodeModelBuilder {
20912089
}
20922090

20932091
private processModelPropertyFromSdkType(prop: SdkModelPropertyType): Property {
2094-
const rawModelPropertyType = prop.__raw as ModelProperty | undefined;
2095-
// TODO: This case is related with literal.tsp, once TCGC supports giving a name, we can use TCGC generatedName
2096-
const schemaNameHint = pascalCase(getNamePrefixForProperty(rawModelPropertyType)) + pascalCase(prop.name);
2097-
let schema = this.processSchemaFromSdkType(prop.type, schemaNameHint);
2092+
let schema = this.processSchemaFromSdkType(prop.type, "");
20982093
let nullable = prop.nullable;
20992094

21002095
let extensions: Record<string, any> | undefined = undefined;
@@ -2133,7 +2128,6 @@ export class CodeModelBuilder {
21332128
throw new Error(`Invalid type for union: '${type.kind}'.`);
21342129
}
21352130
const rawUnionType: Union = type.__raw as Union;
2136-
// TODO: name from typespec-client-generator-core
21372131
const namespace = getNamespace(rawUnionType);
21382132
const baseName = type.name ?? pascalCase(name) + "Model";
21392133
this.logWarning(
@@ -2265,13 +2259,9 @@ export class CodeModelBuilder {
22652259
}
22662260

22672261
private getName(
2268-
target: Model | Union | UnionVariant | Enum | EnumMember | ModelProperty | Scalar | Operation | undefined,
2262+
target: Union | UnionVariant | Enum | EnumMember | ModelProperty | Operation,
22692263
nameHint: string | undefined = undefined,
22702264
): string {
2271-
if (!target) {
2272-
return nameHint || "";
2273-
}
2274-
22752265
// TODO: once getLibraryName API in typespec-client-generator-core can get projected name from language and client, as well as can handle template case, use getLibraryName API
22762266
const emitterClientName = getClientNameOverride(this.sdkContext, target);
22772267
if (emitterClientName && typeof emitterClientName === "string") {
@@ -2293,24 +2283,6 @@ export class CodeModelBuilder {
22932283
return friendlyName;
22942284
}
22952285

2296-
// if no projectedName and friendlyName found, return the name of the target (including special handling for template)
2297-
if (
2298-
target.kind === "Model" &&
2299-
target.templateMapper &&
2300-
target.templateMapper.args &&
2301-
target.templateMapper.args.length > 0
2302-
) {
2303-
const tspName = getTypeName(target, this.typeNameOptions);
2304-
const newName = getNameForTemplate(target);
2305-
this.logWarning(`Rename TypeSpec Model '${tspName}' to '${newName}'`);
2306-
return newName;
2307-
}
2308-
2309-
if (!target.name && nameHint) {
2310-
const newName = nameHint;
2311-
this.logWarning(`Rename anonymous TypeSpec ${target.kind} to '${newName}'`);
2312-
return newName;
2313-
}
23142286
if (typeof target.name === "symbol") {
23152287
return "";
23162288
}

typespec-extension/src/type-utils.ts

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
EnumMember,
66
IntrinsicScalarName,
77
Model,
8-
ModelProperty,
98
Program,
109
Scalar,
1110
StringLiteral,
@@ -21,7 +20,7 @@ import {
2120
} from "@typespec/compiler";
2221
import { SchemaContext } from "@autorest/codemodel";
2322
import { DurationSchema } from "./common/schemas/time.js";
24-
import { getNamespace, pascalCase } from "./utils.js";
23+
import { getNamespace } from "./utils.js";
2524
import { getUnionAsEnum } from "@azure-tools/typespec-azure-core";
2625
import { SdkDurationType, isSdkFloatKind, isSdkIntKind } from "@azure-tools/typespec-client-generator-core";
2726
import { Version } from "@typespec/versioning";
@@ -83,24 +82,6 @@ export function isModelReferredInTemplate(template: TemplatedTypeBase, target: M
8382
);
8483
}
8584

86-
export function getNameForTemplate(target: Type): string {
87-
switch (target.kind) {
88-
case "Model": {
89-
let name = target.name;
90-
if (target.templateMapper && target.templateMapper.args) {
91-
name = name + target.templateMapper.args.map((it) => ("kind" in it ? getNameForTemplate(it) : "")).join("");
92-
}
93-
return name;
94-
}
95-
96-
case "String":
97-
return target.value;
98-
99-
default:
100-
return "";
101-
}
102-
}
103-
10485
export function isNullableType(type: Type): boolean {
10586
if (type.kind === "Union") {
10687
const nullVariants = Array.from(type.variants.values()).filter((it) => isNullType(it.type));
@@ -222,33 +203,6 @@ export function getUnionDescription(union: Union, typeNameOptions: TypeNameOptio
222203
return name;
223204
}
224205

225-
export function getNamePrefixForProperty(property: ModelProperty | undefined): string {
226-
if (property && property.model) {
227-
if (property.model.name) {
228-
return property.model.name;
229-
} else if (property.model.namespace) {
230-
for (const model of property.model.namespace.models.values()) {
231-
for (const prop of model.properties.values()) {
232-
const candidateModel = prop.type;
233-
// find the property that refers to the unnamed property.model
234-
if (
235-
candidateModel.kind === "Model" &&
236-
(candidateModel === property.model || candidateModel.indexer?.value === property.model)
237-
) {
238-
return getNamePrefixForProperty(prop) + pascalCase(prop.name);
239-
} else if (
240-
candidateModel.kind === "Union" &&
241-
Array.from(candidateModel.variants.values()).find((it) => it.type === property.model)
242-
) {
243-
return getNamePrefixForProperty(prop) + pascalCase(prop.name);
244-
}
245-
}
246-
}
247-
}
248-
}
249-
return "";
250-
}
251-
252206
export function modelIs(model: Model, name: string, namespace: string): boolean {
253207
let currentModel: Model | undefined = model;
254208
while (currentModel) {

typespec-tests/src/main/java/com/cadl/literalservice/LiteralServiceAsyncClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.azure.core.util.FluxUtil;
1919
import com.cadl.literalservice.implementation.LiteralOpsImpl;
2020
import com.cadl.literalservice.models.Model;
21-
import com.cadl.literalservice.models.OptionalLiteralParam;
21+
import com.cadl.literalservice.models.PutRequestOptionalLiteralParam;
2222
import reactor.core.publisher.Mono;
2323

2424
/**
@@ -96,7 +96,7 @@ public Mono<Response<BinaryData>> putWithResponse(BinaryData model, RequestOptio
9696
*/
9797
@Generated
9898
@ServiceMethod(returns = ReturnType.SINGLE)
99-
public Mono<Model> put(Model model, OptionalLiteralParam optionalLiteralParam) {
99+
public Mono<Model> put(Model model, PutRequestOptionalLiteralParam optionalLiteralParam) {
100100
// Generated convenience method for putWithResponse
101101
RequestOptions requestOptions = new RequestOptions();
102102
if (optionalLiteralParam != null) {

typespec-tests/src/main/java/com/cadl/literalservice/LiteralServiceClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import com.azure.core.util.BinaryData;
1818
import com.cadl.literalservice.implementation.LiteralOpsImpl;
1919
import com.cadl.literalservice.models.Model;
20-
import com.cadl.literalservice.models.OptionalLiteralParam;
20+
import com.cadl.literalservice.models.PutRequestOptionalLiteralParam;
2121

2222
/**
2323
* Initializes a new instance of the synchronous LiteralServiceClient type.
@@ -94,7 +94,7 @@ public Response<BinaryData> putWithResponse(BinaryData model, RequestOptions req
9494
*/
9595
@Generated
9696
@ServiceMethod(returns = ReturnType.SINGLE)
97-
public Model put(Model model, OptionalLiteralParam optionalLiteralParam) {
97+
public Model put(Model model, PutRequestOptionalLiteralParam optionalLiteralParam) {
9898
// Generated convenience method for putWithResponse
9999
RequestOptions requestOptions = new RequestOptions();
100100
if (optionalLiteralParam != null) {

typespec-tests/src/main/java/com/cadl/literalservice/models/Model.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public final class Model implements JsonSerializable<Model> {
2727
* The optionalLiteral property.
2828
*/
2929
@Generated
30-
private ModelOptionalLiteral optionalLiteral;
30+
private ModelOptionalLiteralOptionalLiteral optionalLiteral;
3131

3232
/**
3333
* Creates an instance of Model class.
@@ -52,7 +52,7 @@ public String getLiteral() {
5252
* @return the optionalLiteral value.
5353
*/
5454
@Generated
55-
public ModelOptionalLiteral getOptionalLiteral() {
55+
public ModelOptionalLiteralOptionalLiteral getOptionalLiteral() {
5656
return this.optionalLiteral;
5757
}
5858

@@ -63,7 +63,7 @@ public ModelOptionalLiteral getOptionalLiteral() {
6363
* @return the Model object itself.
6464
*/
6565
@Generated
66-
public Model setOptionalLiteral(ModelOptionalLiteral optionalLiteral) {
66+
public Model setOptionalLiteral(ModelOptionalLiteralOptionalLiteral optionalLiteral) {
6767
this.optionalLiteral = optionalLiteral;
6868
return this;
6969
}
@@ -99,7 +99,8 @@ public static Model fromJson(JsonReader jsonReader) throws IOException {
9999
reader.nextToken();
100100

101101
if ("optionalLiteral".equals(fieldName)) {
102-
deserializedModel.optionalLiteral = ModelOptionalLiteral.fromString(reader.getString());
102+
deserializedModel.optionalLiteral
103+
= ModelOptionalLiteralOptionalLiteral.fromString(reader.getString());
103104
} else {
104105
reader.skipChildren();
105106
}

typespec-tests/src/main/java/com/cadl/literalservice/models/ModelOptionalLiteral.java renamed to typespec-tests/src/main/java/com/cadl/literalservice/models/ModelOptionalLiteralOptionalLiteral.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,35 @@
55
package com.cadl.literalservice.models;
66

77
/**
8-
* Defines values for ModelOptionalLiteral.
8+
* Defines values for ModelOptionalLiteralOptionalLiteral.
99
*/
10-
public enum ModelOptionalLiteral {
10+
public enum ModelOptionalLiteralOptionalLiteral {
1111
/**
1212
* Enum value optionalLiteral.
1313
*/
1414
OPTIONAL_LITERAL("optionalLiteral");
1515

1616
/**
17-
* The actual serialized value for a ModelOptionalLiteral instance.
17+
* The actual serialized value for a ModelOptionalLiteralOptionalLiteral instance.
1818
*/
1919
private final String value;
2020

21-
ModelOptionalLiteral(String value) {
21+
ModelOptionalLiteralOptionalLiteral(String value) {
2222
this.value = value;
2323
}
2424

2525
/**
26-
* Parses a serialized value to a ModelOptionalLiteral instance.
26+
* Parses a serialized value to a ModelOptionalLiteralOptionalLiteral instance.
2727
*
2828
* @param value the serialized value to parse.
29-
* @return the parsed ModelOptionalLiteral object, or null if unable to parse.
29+
* @return the parsed ModelOptionalLiteralOptionalLiteral object, or null if unable to parse.
3030
*/
31-
public static ModelOptionalLiteral fromString(String value) {
31+
public static ModelOptionalLiteralOptionalLiteral fromString(String value) {
3232
if (value == null) {
3333
return null;
3434
}
35-
ModelOptionalLiteral[] items = ModelOptionalLiteral.values();
36-
for (ModelOptionalLiteral item : items) {
35+
ModelOptionalLiteralOptionalLiteral[] items = ModelOptionalLiteralOptionalLiteral.values();
36+
for (ModelOptionalLiteralOptionalLiteral item : items) {
3737
if (item.toString().equalsIgnoreCase(value)) {
3838
return item;
3939
}

typespec-tests/src/main/java/com/cadl/literalservice/models/OptionalLiteralParam.java renamed to typespec-tests/src/main/java/com/cadl/literalservice/models/PutRequestOptionalLiteralParam.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,35 @@
55
package com.cadl.literalservice.models;
66

77
/**
8-
* Defines values for OptionalLiteralParam.
8+
* Defines values for PutRequestOptionalLiteralParam.
99
*/
10-
public enum OptionalLiteralParam {
10+
public enum PutRequestOptionalLiteralParam {
1111
/**
1212
* Enum value optionalLiteralParam.
1313
*/
1414
OPTIONAL_LITERAL_PARAM("optionalLiteralParam");
1515

1616
/**
17-
* The actual serialized value for a OptionalLiteralParam instance.
17+
* The actual serialized value for a PutRequestOptionalLiteralParam instance.
1818
*/
1919
private final String value;
2020

21-
OptionalLiteralParam(String value) {
21+
PutRequestOptionalLiteralParam(String value) {
2222
this.value = value;
2323
}
2424

2525
/**
26-
* Parses a serialized value to a OptionalLiteralParam instance.
26+
* Parses a serialized value to a PutRequestOptionalLiteralParam instance.
2727
*
2828
* @param value the serialized value to parse.
29-
* @return the parsed OptionalLiteralParam object, or null if unable to parse.
29+
* @return the parsed PutRequestOptionalLiteralParam object, or null if unable to parse.
3030
*/
31-
public static OptionalLiteralParam fromString(String value) {
31+
public static PutRequestOptionalLiteralParam fromString(String value) {
3232
if (value == null) {
3333
return null;
3434
}
35-
OptionalLiteralParam[] items = OptionalLiteralParam.values();
36-
for (OptionalLiteralParam item : items) {
35+
PutRequestOptionalLiteralParam[] items = PutRequestOptionalLiteralParam.values();
36+
for (PutRequestOptionalLiteralParam item : items) {
3737
if (item.toString().equalsIgnoreCase(value)) {
3838
return item;
3939
}

typespec-tests/src/main/java/com/cadl/specialchars/SpecialCharsAsyncClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public final class SpecialCharsAsyncClient {
6161
* }
6262
* }</pre>
6363
*
64-
* @param request The request parameter.
64+
* @param readRequest The readRequest parameter.
6565
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
6666
* @throws HttpResponseException thrown if the request is rejected by server.
6767
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
@@ -71,8 +71,8 @@ public final class SpecialCharsAsyncClient {
7171
*/
7272
@Generated
7373
@ServiceMethod(returns = ReturnType.SINGLE)
74-
public Mono<Response<BinaryData>> readWithResponse(BinaryData request, RequestOptions requestOptions) {
75-
return this.serviceClient.readWithResponseAsync(request, requestOptions);
74+
public Mono<Response<BinaryData>> readWithResponse(BinaryData readRequest, RequestOptions requestOptions) {
75+
return this.serviceClient.readWithResponseAsync(readRequest, requestOptions);
7676
}
7777

7878
/**
@@ -92,9 +92,9 @@ public Mono<Response<BinaryData>> readWithResponse(BinaryData request, RequestOp
9292
public Mono<Resource> read(String id) {
9393
// Generated convenience method for readWithResponse
9494
RequestOptions requestOptions = new RequestOptions();
95-
ReadRequest requestObj = new ReadRequest(id);
96-
BinaryData request = BinaryData.fromObject(requestObj);
97-
return readWithResponse(request, requestOptions).flatMap(FluxUtil::toMono)
95+
ReadRequest readRequestObj = new ReadRequest(id);
96+
BinaryData readRequest = BinaryData.fromObject(readRequestObj);
97+
return readWithResponse(readRequest, requestOptions).flatMap(FluxUtil::toMono)
9898
.map(protocolMethodData -> protocolMethodData.toObject(Resource.class));
9999
}
100100
}

0 commit comments

Comments
 (0)