diff --git a/typespec-extension/src/code-model-builder.ts b/typespec-extension/src/code-model-builder.ts index aaa384c217..5b7771ef13 100644 --- a/typespec-extension/src/code-model-builder.ts +++ b/typespec-extension/src/code-model-builder.ts @@ -1305,14 +1305,11 @@ export class CodeModelBuilder { }); op.addParameter(parameter); - this.trackSchemaUsage(schema, { usage: [SchemaContext.Input] }); + const jsonMergePatch = operationIsJsonMergePatch(sdkHttpOperation); - if (op.convenienceApi) { - // model/schema does not need to be Public or Internal, if it is not to be used in convenience API - this.trackSchemaUsage(schema, { usage: [op.internalApi ? SchemaContext.Internal : SchemaContext.Public] }); - } + this.trackSchemaUsage(schema, { usage: [SchemaContext.Input] }); - if (operationIsJsonMergePatch(sdkHttpOperation)) { + if (jsonMergePatch) { this.trackSchemaUsage(schema, { usage: [SchemaContext.JsonMergePatch] }); } if (op.convenienceApi && operationIsMultipart(sdkHttpOperation)) { @@ -1323,25 +1320,36 @@ export class CodeModelBuilder { // Implicit body parameter would result to rawHttpOperation.parameters.body.property be undefined // see https://typespec.io/docs/libraries/http/cheat-sheet#data-types const bodyParameterFlatten = - sdkType.kind === "model" && !rawHttpOperation.parameters.body?.property && !this.isArm(); + schema instanceof ObjectSchema && + sdkType.kind === "model" && + !rawHttpOperation.parameters.body?.property && + !this.isArm(); + + if (op.convenienceApi) { + // model/schema does not need to be Public or Internal, if it is not to be used in convenience API + if (op.internalApi) { + this.trackSchemaUsage(schema, { usage: [SchemaContext.Internal] }); + } else if (!(bodyParameterFlatten && !jsonMergePatch)) { + // do not add Public, if going to add Spread + this.trackSchemaUsage(schema, { usage: [SchemaContext.Public] }); + } + } - if (schema instanceof ObjectSchema && bodyParameterFlatten) { + if (bodyParameterFlatten) { // flatten body parameter const parameters = sdkHttpOperation.parameters; const bodyParameter = sdkHttpOperation.bodyParam; - if (!schema.language.default.name) { - // name the schema for documentation - schema.language.default.name = pascalCase(op.language.default.name) + "Request"; - } if (!parameter.language.default.name) { // name the parameter for documentation parameter.language.default.name = "request"; } - if (operationIsJsonMergePatch(sdkHttpOperation)) { + if (jsonMergePatch) { // skip model flatten, if "application/merge-patch+json" - schema.language.default.name = pascalCase(op.language.default.name) + "PatchRequest"; + if (sdkType.isGeneratedName) { + schema.language.default.name = pascalCase(op.language.default.name) + "PatchRequest"; + } return; }