Skip to content

Commit 55237d3

Browse files
committed
binary request
1 parent 08263ea commit 55237d3

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

src/typespec-aaz/src/convertor.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import {
9999
CMDIdentityObjectSchemaBase,
100100
CMDBooleanSchemaBase,
101101
CMDAnyTypeSchemaBase,
102+
CMDBinarySchema,
102103
} from "./model/schema.js";
103104
import { reportDiagnostic } from "./lib.js";
104105
import { getExtensions, getOpenAPITypeName, isReadonlyProperty } from "@typespec/openapi";
@@ -372,9 +373,6 @@ function extractHttpRequest(
372373
if (body.bodyKind === "multipart") {
373374
throw new Error("NotImplementedError: Multipart form data payloads are not supported.");
374375
}
375-
if (isBinaryPayload(body.type, consumes)) {
376-
throw new Error("NotImplementedError: Binary payloads are not supported.");
377-
}
378376
if (consumes.includes("multipart/form-data")) {
379377
throw new Error("NotImplementedError: Multipart form data payloads are not supported.");
380378
}
@@ -412,6 +410,12 @@ function extractHttpRequest(
412410
clientFlatten: true,
413411
} as CMDObjectSchema;
414412
}
413+
if (isBinaryPayload(body.type, consumes)) {
414+
schema = {
415+
...schema,
416+
type: "binary",
417+
} as CMDBinarySchema;
418+
}
415419
request.body = {
416420
json: {
417421
schema,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { TestHost, BasicTestRunner } from "@typespec/compiler/testing";
2+
import { describe, expect, it, beforeEach } from "vitest";
3+
import { createTypespecAazTestHost, createTypespecAazTestRunner, compileTypespecAAZOperations } from "./test-aaz.js";
4+
import { findObjectsWithKey } from "./util.js";
5+
6+
describe("http request parsing", () => {
7+
let host: TestHost;
8+
let runner: BasicTestRunner;
9+
10+
beforeEach(async () => {
11+
host = await createTypespecAazTestHost();
12+
runner = await createTypespecAazTestRunner(host);
13+
});
14+
15+
it("validate http request body with bytes", async () => {
16+
const code: string = `
17+
@versioned(Versions)
18+
@service(#{ title: "My service" })
19+
namespace Service;
20+
enum Versions {A, B, C}
21+
model P {
22+
@bodyRoot
23+
body: bytes;
24+
}
25+
model Q {
26+
q: string;
27+
}
28+
29+
#suppress "@azure-tools/typespec-azure-core/use-standard-operations" "This is a test."
30+
@route("/test1")
31+
@get
32+
op test1(p: P): Q;
33+
`;
34+
const result = await compileTypespecAAZOperations(
35+
code,
36+
{
37+
"operation": "get-resources-operations",
38+
"api-version": "A",
39+
"resources": ["/test1"],
40+
},
41+
runner,
42+
);
43+
const resultObj = JSON.parse(result!);
44+
expect(Array.isArray(resultObj)).toBe(true);
45+
expect(resultObj.length).toBe(1);
46+
const targetObj = findObjectsWithKey(resultObj[0].pathItem.get.read.http.request, "body");
47+
await expect(JSON.stringify(targetObj, null, 2)).toMatchFileSnapshot("./snapshots/http-request-bytes-body.json");
48+
});
49+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "binary",
3+
"name": "body",
4+
"required": true
5+
}

0 commit comments

Comments
 (0)