Skip to content

Commit

Permalink
[TCGC] fix multipart lint (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
msyyc authored Feb 6, 2024
1 parent 8b072f4 commit d46b6bf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-trains-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@azure-tools/typespec-client-generator-core": none
---

fix linting error for mfd
10 changes: 7 additions & 3 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,13 @@ export function getSdkModel(context: SdkContext, type: Model, operation?: Operat
const httpOperation = operation
? ignoreDiagnostics(getHttpOperation(context.program, operation))
: undefined;
const isFormDataType = httpOperation
? Boolean(httpOperation.parameters.body?.contentTypes.includes("multipart/form-data"))
: false;
const httpBody = httpOperation?.parameters.body;
let isFormDataType = false;
if (httpBody && httpBody.type.kind === "Model") {
const isMultipartOperation = httpBody.contentTypes.some((x) => x.startsWith("multipart/"));
isFormDataType =
isMultipartOperation && getEffectivePayloadType(context, httpBody.type) === type;
}
if (sdkType) {
updateModelsMap(context, type, sdkType, operation);
if (httpOperation && isFormDataType !== sdkType.isFormDataType) {
Expand Down
27 changes: 27 additions & 0 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,33 @@ describe("typespec-client-generator-core: types", () => {
code: "@azure-tools/typespec-client-generator-core/encoding-multipart-bytes",
});
});

it("multipart with reused error model", async function () {
await runner.compileWithBuiltInService(
`
model PictureWrapper {
pictures: bytes[];
}
model ErrorResponse {
errorCode: string;
}
@put op multipartOp(@header contentType: "multipart/form-data", @body body: PictureWrapper): void | ErrorResponse;
@post op normalOp(): void | ErrorResponse;
`
);
const models = getAllModels(runner.context);
strictEqual(models.length, 2);

const pictureWrapper = models.find((x) => x.name === "PictureWrapper")!;
strictEqual(pictureWrapper.kind, "model");
strictEqual(pictureWrapper.isFormDataType, true);

const errorResponse = models.find((x) => x.name === "ErrorResponse")!;
strictEqual(errorResponse.kind, "model");
strictEqual(errorResponse.isFormDataType, false);
});
});
describe("SdkTupleType", () => {
it("model with tupled properties", async function () {
Expand Down

0 comments on commit d46b6bf

Please sign in to comment.