Skip to content

Commit 850f467

Browse files
committed
fix: configurable max size
1 parent 01e31c6 commit 850f467

File tree

16 files changed

+97
-15
lines changed

16 files changed

+97
-15
lines changed

e2e/openapi.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ paths:
333333
schema:
334334
type: string
335335
format: binary
336+
maxLength: 20971520
336337
responses:
337338
200:
338339
description: ok

e2e/src/generated/server/express/routes/media-types.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/src/generated/server/koa/routes/media-types.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration-tests/typescript-express/src/generated/api.github.com.yaml/generated.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration-tests/typescript-koa/src/generated/api.github.com.yaml/generated.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/documentation/src/app/guides/concepts/content-type/page.mdx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,28 @@ by the server templates. Full support is expected to land soon.
8080

8181
## `application/octet-stream`
8282

83-
🚫 Not yet supported. Coming soon.
83+
| Template | Supported | Notes |
84+
|:-------------------|:---------:|--------------------------------------------------------------------------------:|
85+
| typescript-angular | 🚫 | |
86+
| typescript-axios || Uses `Blob` for request bodies, and response `res.data` |
87+
| typescript-express || Parses request bodies to `Blob`, serializes `Blob` |
88+
| typescript-fetch || Pass a `Blob` for request bodies, access using `res.blob()` for response bodies |
89+
| typescript-koa || Parses request bodies to `Blob`, serializes `Blob` |
90+
91+
### Controlling Maximum Request Body size
92+
By default, a maximum of `10mb` is used as the maximum requset body size for binary data. You can override this for
93+
an individual schema by using the `maxLength` field, which will be interpreted as `bytes`, eg:
94+
95+
```yaml
96+
post:
97+
requestBody:
98+
content:
99+
application/octet-stream:
100+
schema:
101+
type: string
102+
format: binary
103+
maxLength: 20971520 # 20mb in bytes
104+
```
84105
85106
## Selecting between multiple possible `Content-Type`
86107

packages/openapi-code-generator/src/core/utils.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {describe, expect, it} from "@jest/globals"
22
import {
33
camelCase,
44
coalesce,
5+
convertBytesToHuman,
56
deepEqual,
67
hasSingleElement,
78
isDefined,
@@ -358,4 +359,16 @@ describe("core/utils", () => {
358359
expect(mediaTypeToIdentifier(contentType)).toBe(expected)
359360
})
360361
})
362+
363+
describe("#convertBytesToHuman", () => {
364+
it("converts to mb", () => {
365+
expect(convertBytesToHuman(5 * 1024 * 1024)).toBe("5mb")
366+
})
367+
it("converts to kb", () => {
368+
expect(convertBytesToHuman(128 * 1024)).toBe("128kb")
369+
})
370+
it("leaves as b", () => {
371+
expect(convertBytesToHuman(768)).toBe("768b")
372+
})
373+
})
361374
})

packages/openapi-code-generator/src/core/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,23 @@ export function mediaTypeToIdentifier(mediaType: string): string {
207207

208208
return titleCase([type, subType].filter(isDefined).join(" "))
209209
}
210+
211+
export function convertBytesToHuman(
212+
bytes: number,
213+
): number | `${number}${"mb" | "kb" | "b"}` {
214+
const units = ["mb" as const, "kb" as const, "b" as const]
215+
216+
let adjusted = bytes
217+
while (units.length > 1 && adjusted % 1024 === 0) {
218+
adjusted /= 1024
219+
units.pop()
220+
}
221+
222+
const unit = units.pop()
223+
224+
if (!unit) {
225+
return bytes
226+
}
227+
228+
return `${adjusted}${unit}` as const
229+
}

packages/openapi-code-generator/src/typescript/common/type-builder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export class TypeBuilder implements ICompilable {
180180
}
181181
} else if (
182182
schemaObject.format === "binary" ||
183+
// todo: is byte the same as binary
183184
schemaObject.format === "byte"
184185
) {
185186
result.push("Blob")

packages/openapi-code-generator/src/typescript/server/abstract-router-builder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export abstract class AbstractRouterBuilder implements ICompilable {
4747
{
4848
requestBody: {
4949
supportedMediaTypes: this.capabilities.requestBody.mediaTypes,
50+
defaultMaxSize: "10mb",
5051
},
5152
},
5253
)

0 commit comments

Comments
 (0)