Skip to content

Commit

Permalink
refine logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh committed Sep 19, 2024
1 parent 51a1b18 commit dbb5c29
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
37 changes: 18 additions & 19 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
`
Expand All @@ -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(
`
Expand Down Expand Up @@ -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(
`
Expand Down

0 comments on commit dbb5c29

Please sign in to comment.