Skip to content

Commit

Permalink
do not add Public, if going to add Spread
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft committed Aug 26, 2024
1 parent 7d91812 commit 0ba777e
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions typespec-extension/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}

Expand Down

0 comments on commit 0ba777e

Please sign in to comment.