Skip to content

Commit

Permalink
[TCGC] Fix judge logic how to distinguish model is referred both in c…
Browse files Browse the repository at this point in the history
…ommon content-type and multipart content-type (#1511)

fix #1510

---------

Co-authored-by: tadelesh <[email protected]>
  • Loading branch information
msyyc and tadelesh committed Sep 20, 2024
1 parent 271d4c5 commit 3becc74
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

Fix logic to check conflicting usage for model of multipart body and regular body
41 changes: 21 additions & 20 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1602,26 +1602,6 @@ function updateTypesFromOperation(
}

const multipartOperation = isMultipartOperation(context, operation);
// this part should be put before setting current body's usage because it is based on the previous usage
if (
sdkType.kind === "model" &&
((!multipartOperation && (sdkType.usage & UsageFlags.MultipartFormData) > 0) ||
(multipartOperation &&
(sdkType.usage & UsageFlags.Input) > 0 &&
(sdkType.usage & UsageFlags.Input & UsageFlags.MultipartFormData) === 0))
) {
// This means we have a model that is used both for formdata input and for regular body input
diagnostics.add(
createDiagnostic({
code: "conflicting-multipart-model-usage",
target: httpBody.type,
format: {
modelName: sdkType.name,
},
}),
);
}

if (generateConvenient) {
if (spread) {
updateUsageOrAccessOfModel(context, UsageFlags.Spread, sdkType, { propagation: false });
Expand Down Expand Up @@ -1649,7 +1629,28 @@ function updateTypesFromOperation(
}
const access = getAccessOverride(context, operation) ?? "public";
diagnostics.pipe(updateUsageOrAccessOfModel(context, access, sdkType));

// after completion of usage calculation for httpBody, check whether it has
// conflicting usage between multipart and regular body
if (sdkType.kind === "model") {
const isUsedInMultipart = (sdkType.usage & UsageFlags.MultipartFormData) > 0;
const isUsedInOthers =
((sdkType.usage & UsageFlags.Json) | (sdkType.usage & UsageFlags.Xml)) > 0;
if ((!multipartOperation && isUsedInMultipart) || (multipartOperation && isUsedInOthers)) {
// This means we have a model that is used both for formdata input and for regular body input
diagnostics.add(
createDiagnostic({
code: "conflicting-multipart-model-usage",
target: httpBody.type,
format: {
modelName: sdkType.name,
},
}),
);
}
}
}

for (const response of httpOperation.responses) {
for (const innerResponse of response.responses) {
if (innerResponse.body?.type && !isNeverOrVoidType(innerResponse.body.type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe("typespec-client-generator-core: multipart types", () => {
ok(profileImage.multipartOptions);
strictEqual(profileImage.multipartOptions.isFilePart, true);
});

it("multipart conflicting model usage", async function () {
await runner.compile(
`
Expand All @@ -72,6 +73,70 @@ describe("typespec-client-generator-core: multipart types", () => {
code: "@azure-tools/typespec-client-generator-core/conflicting-multipart-model-usage",
});
});

it("multipart conflicting model usage for only multipart operations", async function () {
await runner.compile(
`
@service({title: "Test Service"}) namespace TestService;
model Address {city: string;}
model MultiPartRequest {
address: Address;
id: string;
profileImage: bytes;
}
@post
@route("/basic1")
op basic1(@header contentType: "multipart/form-data", @body body: MultiPartRequest): NoContentResponse;
@post
@route("/basic2")
op basic2(@header contentType: "multipart/form-data", @body body: MultiPartRequest): NoContentResponse;
`,
);
deepEqual(runner.context.diagnostics.length, 0);
const address = runner.context.sdkPackage.models.find((x) => x.name === "Address");
ok(address);
deepEqual(address.usage, UsageFlags.Input);
const multiPartRequest = runner.context.sdkPackage.models.find(
(x) => x.name === "MultiPartRequest",
);
ok(multiPartRequest);
deepEqual(multiPartRequest.usage, UsageFlags.MultipartFormData | UsageFlags.Input);
});

it("multipart conflicting model usage for mixed operations", async function () {
await runner.compile(
`
@service({title: "Test Service"}) namespace TestService;
model Address {city: string;}
model RegularRequest {
address: Address;
}
model MultiPartRequest {
address: Address;
id: string;
profileImage: bytes;
}
@post
@route("/basic1")
op basic1(@body body: RegularRequest): NoContentResponse;
@post
@route("/basic2")
op basic2(@header contentType: "multipart/form-data", @body body: MultiPartRequest): NoContentResponse;
`,
);
deepEqual(runner.context.diagnostics.length, 0);
const address = runner.context.sdkPackage.models.find((x) => x.name === "Address");
ok(address);
deepEqual(address.usage, UsageFlags.Input | UsageFlags.Json);
const multiPartRequest = runner.context.sdkPackage.models.find(
(x) => x.name === "MultiPartRequest",
);
ok(multiPartRequest);
deepEqual(multiPartRequest.usage, UsageFlags.MultipartFormData | UsageFlags.Input);
});

it("multipart resolving conflicting model usage with spread", async function () {
await runner.compileWithBuiltInService(
`
Expand Down

0 comments on commit 3becc74

Please sign in to comment.