Skip to content

✨support file upload decorator #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions __tests__/fixtures/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
Put,
QueryParam,
QueryParams,
UploadedFile,
UploadedFiles,
} from 'routing-controllers'

import { OpenAPI, ResponseSchema } from '../../src'
Expand All @@ -39,6 +41,11 @@ export class CreatePostBody {
content: string[]
}

export class CreateUserPostImagesBody {
@IsString()
description: string
}

export class ListUsersQueryParams {
@IsOptional()
@IsEmail()
Expand Down Expand Up @@ -154,6 +161,11 @@ export class UsersController {
) {
return
}

@Put('/:userId/avatar')
putUserAvatar(@UploadedFile('image') _image: any) {
return
}
}

@Controller('/users/:userId/posts')
Expand All @@ -170,6 +182,15 @@ export class UserPostsController {
patchUserPost(@BodyParam('token') _token: string) {
return
}

@Post('/:postId/images')
createUserPostImages(
@Body({ required: true }) _body: CreateUserPostImagesBody,
@BodyParam('token') _token: string,
@UploadedFiles('images') _images: any[]
) {
return
}
}

@Controller()
Expand Down
122 changes: 121 additions & 1 deletion __tests__/fixtures/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@
],
"type": "object"
},
"CreateUserPostImagesBody": {
"properties": {
"description": {
"type": "string"
}
},
"required": [
"description"
],
"type": "object"
},
"ListUsersHeaderParams": {
"properties": {
"Authorization": {
Expand Down Expand Up @@ -432,6 +443,72 @@
]
}
},
"/api/users/{userId}/posts/{postId}/images": {
"post": {
"operationId": "UserPostsController.createUserPostImages",
"parameters": [
{
"in": "path",
"name": "userId",
"required": true,
"schema": {
"type": "string"
}
},
{
"in": "path",
"name": "postId",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/CreateUserPostImagesBody"
},
{
"properties": {
"images": {
"items": {
"format": "binary",
"type": "string"
},
"type": "array"
},
"token": {
"type": "string"
}
},
"required": [],
"type": "object"
}
]
}
}
},
"description": "CreateUserPostImagesBody",
"required": true
},
"responses": {
"200": {
"content": {
"text/html; charset=utf-8": {}
},
"description": "Successful response"
}
},
"summary": "Create user post images",
"tags": [
"User Posts"
]
}
},
"/api/users/{version}": {
"delete": {
"operationId": "UsersController.deleteUsersByVersion",
Expand Down Expand Up @@ -503,6 +580,49 @@
]
}
},
"/api/users/{userId}/avatar": {
"put": {
"operationId": "UsersController.putUserAvatar",
"parameters": [
{
"in": "path",
"name": "userId",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"properties": {
"image": {
"format": "binary",
"type": "string"
}
},
"required": [],
"type": "object"
}
}
}
},
"responses": {
"200": {
"content": {
"application/json": {}
},
"description": "Successful response"
}
},
"summary": "Put user avatar",
"tags": [
"Users"
]
}
},
"/api/users/{userId}/posts": {
"post": {
"operationId": "UsersController.createUserPost",
Expand Down Expand Up @@ -638,4 +758,4 @@
}
}
}
}
}
70 changes: 70 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ describe('index', () => {
target: UsersController,
type: 'put',
},
{
method: 'putUserAvatar',
route: '/:userId/avatar',
target: UsersController,
type: 'put',
},
{
method: 'getUserPost',
route: '/:postId',
Expand All @@ -128,6 +134,12 @@ describe('index', () => {
target: UserPostsController,
type: 'patch',
},
{
method: 'createUserPostImages',
route: '/:postId/images',
target: UserPostsController,
type: 'post',
},
{
method: 'getDefaultPath',
route: undefined,
Expand Down Expand Up @@ -297,4 +309,62 @@ describe('getRequestBody', () => {
required: true,
})
})

it('parse a single `UploadedFile` metadata into a single `object` schema under content-type `multipart/form-data`', () => {
const route = routes.find((d) => d.action.method === 'putUserAvatar')!
expect(route).toBeDefined()
expect(getRequestBody(route)).toEqual({
content: {
'multipart/form-data': {
schema: {
properties: {
image: {
format: 'binary',
type: 'string',
},
},
required: [],
type: 'object',
},
},
},
})
})
it('wrap `body` and others metadata containing `UploadedFiles` items under a single `allOf` schema under content-type `multipart/form-data`', () => {
const route = routes.find(
(d) => d.action.method === 'createUserPostImages'
)!
expect(route).toBeDefined()
expect(getRequestBody(route)).toEqual({
content: {
'multipart/form-data': {
schema: {
allOf: [
{
$ref: '#/components/schemas/CreateUserPostImagesBody',
},
{
properties: {
images: {
items: {
format: 'binary',
type: 'string',
},
type: 'array',
},
token: {
type: 'string',
},
},
required: [],
type: 'object',
},
],
},
},
},
description: 'CreateUserPostImagesBody',
required: true,
})
})
})
46 changes: 36 additions & 10 deletions src/generateSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,43 @@ export function getQueryParams(
return queries
}

function getNamedParamSchema(
param: ParamMetadataArgs
): oa.SchemaObject | oa.ReferenceObject {
const { type } = param
if (type === 'file') {
return { type: 'string', format: 'binary' }
}
if (type === 'files') {
return {
type: 'array',
items: {
type: 'string',
format: 'binary',
},
}
}
return getParamSchema(param)
}

/**
* Return OpenAPI requestBody of given route, if it has one.
*/
export function getRequestBody(route: IRoute): oa.RequestBodyObject | void {
const bodyParamMetas = route.params.filter((d) => d.type === 'body-param')
const bodyParamsSchema: oa.SchemaObject | null =
bodyParamMetas.length > 0
? bodyParamMetas.reduce(
const uploadFileMetas = route.params.filter((d) =>
['file', 'files'].includes(d.type)
)
const namedParamMetas = [...bodyParamMetas, ...uploadFileMetas]

const namedParamsSchema: oa.SchemaObject | null =
namedParamMetas.length > 0
? namedParamMetas.reduce(
(acc: oa.SchemaObject, d) => ({
...acc,
properties: {
...acc.properties,
[d.name!]: getParamSchema(d),
[d.name!]: getNamedParamSchema(d),
},
required: isRequired(d, route)
? [...(acc.required || []), d.name!]
Expand All @@ -213,27 +237,29 @@ export function getRequestBody(route: IRoute): oa.RequestBodyObject | void {
)
: null

const bodyMeta = route.params.find((d) => d.type === 'body')
const contentType =
uploadFileMetas.length > 0 ? 'multipart/form-data' : 'application/json'

const bodyMeta = route.params.find((d) => d.type === 'body')
if (bodyMeta) {
const bodySchema = getParamSchema(bodyMeta)
const { $ref } =
'items' in bodySchema && bodySchema.items ? bodySchema.items : bodySchema

return {
content: {
'application/json': {
schema: bodyParamsSchema
? { allOf: [bodySchema, bodyParamsSchema] }
[contentType]: {
schema: namedParamsSchema
? { allOf: [bodySchema, namedParamsSchema] }
: bodySchema,
},
},
description: ($ref || '').split('/').pop(),
required: isRequired(bodyMeta, route),
}
} else if (bodyParamsSchema) {
} else if (namedParamsSchema) {
return {
content: { 'application/json': { schema: bodyParamsSchema } },
content: { [contentType]: { schema: namedParamsSchema } },
}
}
}
Expand Down