Skip to content

Commit

Permalink
[tcgc] linter raise for encoding mfd bytes (#220)
Browse files Browse the repository at this point in the history
Co-authored-by: iscai-msft <isabellavcai@gmail.com>
Co-authored-by: Timothee Guerin <tiguerin@microsoft.com>
  • Loading branch information
3 people authored Feb 5, 2024
1 parent fd85907 commit c07c189
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-ladybugs-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@azure-tools/typespec-client-generator-core": minor
---

error out if user tries to encode bytes in multipart input
7 changes: 7 additions & 0 deletions packages/typespec-client-generator-core/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ export const $lib = createTypeSpecLibrary({
default: "@client or @operationGroup should decorate namespace or interface in client.tsp",
},
},
"encoding-multipart-bytes": {
severity: "error",
messages: {
default:
"Encoding should not be applied to bytes content in a multipart request. This is semi-incompatible with how multipart works in HTTP.",
},
},
},
});

Expand Down
6 changes: 6 additions & 0 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,12 @@ function getSdkBodyModelPropertyType(
const isBytesInput =
base.type.kind === "bytes" ||
(base.type.kind === "array" && base.type.valueType.kind === "bytes");
if (isBytesInput && getEncode(context.program, type)) {
reportDiagnostic(context.program, {
code: "encoding-multipart-bytes",
target: type,
});
}
return {
...base,
kind: "property",
Expand Down
18 changes: 18 additions & 0 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,24 @@ describe("typespec-client-generator-core: types", () => {
strictEqual(pictures.kind, "property");
strictEqual(pictures.isMultipartFileInput, true);
});

it("multipart with encoding bytes raises error", async function () {
const diagnostics = await runner.diagnose(
`
@service({title: "Test Service"}) namespace TestService;
model EncodedBytesMFD {
@encode("base64")
pictures: bytes;
}
@put op multipartOp(@header contentType: "multipart/form-data", @body body: EncodedBytesMFD): void;
`
);
getAllModels(runner.context);
expectDiagnostics(diagnostics, {
code: "@azure-tools/typespec-client-generator-core/encoding-multipart-bytes",
});
});
});
describe("SdkTupleType", () => {
it("model with tupled properties", async function () {
Expand Down

0 comments on commit c07c189

Please sign in to comment.