From dbb5c292465c010384fe15772298d0b98c099276 Mon Sep 17 00:00:00 2001 From: tadelesh Date: Thu, 19 Sep 2024 16:02:34 +0800 Subject: [PATCH] refine logic --- .../src/types.ts | 37 +++++++++---------- .../test/types/multipart-types.test.ts | 3 ++ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index 0b2b7b149..2253551e2 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -1331,8 +1331,8 @@ function updateMultiPartInfo( : undefined, contentType: httpOperationPart.body.contentTypeProperty ? diagnostics.pipe( - getSdkModelPropertyType(context, httpOperationPart.body.contentTypeProperty, operation), - ) + getSdkModelPropertyType(context, httpOperationPart.body.contentTypeProperty, operation), + ) : undefined, defaultContentTypes: httpOperationPart.body.contentTypes, }; @@ -1660,25 +1660,24 @@ function updateTypesFromOperation( // after completion of usage calculation for httpBody, check whether it has // conflicting usage between multipart and regular body - if ( - sdkType.kind === "model" && - ((!multipartOperation && (sdkType.usage & UsageFlags.MultipartFormData) > 0) || - (multipartOperation && - (sdkType.usage & UsageFlags.MultipartFormData) > 0 && - ((sdkType.usage & UsageFlags.Json) | (sdkType.usage & UsageFlags.Xml)) > 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 (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)) { diff --git a/packages/typespec-client-generator-core/test/types/multipart-types.test.ts b/packages/typespec-client-generator-core/test/types/multipart-types.test.ts index 0add03313..a224bd05e 100644 --- a/packages/typespec-client-generator-core/test/types/multipart-types.test.ts +++ b/packages/typespec-client-generator-core/test/types/multipart-types.test.ts @@ -44,6 +44,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( ` @@ -61,6 +62,7 @@ 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( ` @@ -90,6 +92,7 @@ describe("typespec-client-generator-core: multipart types", () => { ok(multiPartRequest); deepEqual(multiPartRequest.usage, UsageFlags.MultipartFormData | UsageFlags.Input); }); + it("multipart conflicting model usage for mixed operations", async function () { await runner.compile( `