From 0ad95ba4efae886c81cf0f709d325991788f18d7 Mon Sep 17 00:00:00 2001 From: anton-iskryzhytskyi Date: Tue, 3 May 2022 15:40:47 +0300 Subject: [PATCH 1/2] fix(cli): add description for multipart/form-data params --- packages/cli/src/swagger/specGenerator3.ts | 1 + tests/fixtures/controllers/postController.ts | 11 +++++++ tests/unit/swagger/parameterDetails3.spec.ts | 1 + tests/unit/swagger/schemaDetails3.spec.ts | 34 ++++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/packages/cli/src/swagger/specGenerator3.ts b/packages/cli/src/swagger/specGenerator3.ts index a94f38d73..ab8f4916c 100644 --- a/packages/cli/src/swagger/specGenerator3.ts +++ b/packages/cli/src/swagger/specGenerator3.ts @@ -435,6 +435,7 @@ export class SpecGenerator3 extends SpecGenerator { schema: { ...this.getSwaggerType(parameter.type), ...validators, + ...(parameter.description && { description: parameter.description }), }, }; diff --git a/tests/fixtures/controllers/postController.ts b/tests/fixtures/controllers/postController.ts index abaeb32f8..391765c9c 100644 --- a/tests/fixtures/controllers/postController.ts +++ b/tests/fixtures/controllers/postController.ts @@ -67,6 +67,17 @@ export class PostTestController { return files; } + /** + * + * @param aFile File description of multipart + * @param a FormField description of multipart + * @param c + */ + @Post('DescriptionOfFileAndFormFields') + public async postWithFileAndParams(@UploadedFile('file') aFile: File, @FormField('a') a: string, @FormField('c') c: string): Promise { + return aFile; + } + @Post('Location') public async postModelAtLocation(): Promise { return new ModelService().getModel(); diff --git a/tests/unit/swagger/parameterDetails3.spec.ts b/tests/unit/swagger/parameterDetails3.spec.ts index 267349b15..7ea8e7d89 100644 --- a/tests/unit/swagger/parameterDetails3.spec.ts +++ b/tests/unit/swagger/parameterDetails3.spec.ts @@ -52,6 +52,7 @@ describe('Parameter generation for OpenAPI 3.0.0', () => { $ref: '#/components/schemas/ParameterTestModel', }, type: 'array', + description: 'Body description', }, `for spec ${forSpec(currentSpec)}`, ); diff --git a/tests/unit/swagger/schemaDetails3.spec.ts b/tests/unit/swagger/schemaDetails3.spec.ts index 04da693e3..8f1dd42a4 100644 --- a/tests/unit/swagger/schemaDetails3.spec.ts +++ b/tests/unit/swagger/schemaDetails3.spec.ts @@ -527,6 +527,40 @@ describe('Definition generation for OpenAPI 3.0.0', () => { }, }); }); + it('should consume multipart/form-data and have multiple formData parameter with optional descriptions', () => { + // Act + const specPost = new SpecGenerator3(metadataPost, getDefaultExtendedOptions()).GetSpec(); + const pathPost = specPost.paths['/PostTest/DescriptionOfFileAndFormFields'].post; + if (!pathPost) { + throw new Error('PostTest file method not defined'); + } + if (!pathPost.requestBody) { + throw new Error('PostTest file method has no requestBody'); + } + + // Assert + expect(pathPost.parameters).to.have.length(0); + expect(pathPost.requestBody).to.deep.equal({ + required: true, + content: { + 'multipart/form-data': { + schema: { + type: 'object', + properties: { + file: { + type: 'string', + format: 'binary', + description: 'File description of multipart', + }, + a: { type: 'string', description: 'FormField description of multipart' }, + c: { type: 'string' }, + }, + required: ['file', 'a', 'c'], + }, + }, + }, + }); + }); }); describe('requestBody', () => { it('should replace the body parameter with a requestBody', () => { From 17813fb747a97d4ea9842bc246156cf06761c601 Mon Sep 17 00:00:00 2001 From: anton-iskryzhytskyi Date: Fri, 6 May 2022 15:59:51 +0300 Subject: [PATCH 2/2] test(cli): add tests of description for multipart/form-data params of specV2 --- tests/unit/swagger/schemaDetails.spec.ts | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/unit/swagger/schemaDetails.spec.ts b/tests/unit/swagger/schemaDetails.spec.ts index 51c834c45..4a8547e28 100644 --- a/tests/unit/swagger/schemaDetails.spec.ts +++ b/tests/unit/swagger/schemaDetails.spec.ts @@ -243,6 +243,45 @@ describe('Schema details generation', () => { type: 'string', }); }); + it('should consume multipart/form-data and have multiple formData parameter with optional descriptions', () => { + // Act + const specPost = new SpecGenerator2(metadataPost, getDefaultExtendedOptions()).GetSpec(); + const pathPost = specPost.paths['/PostTest/DescriptionOfFileAndFormFields'].post; + if (!pathPost) { + throw new Error('PostTest file method not defined'); + } + if (!pathPost.parameters?.length) { + throw new Error('PostTest file method has no parameters'); + } + + // Assert + expect(pathPost.consumes).to.include('multipart/form-data'); + const baseParameter = { + default: undefined, + description: undefined, + enum: undefined, + items: undefined, + required: true, + in: 'formData', + }; + expect(pathPost.parameters[0]).to.deep.equal({ + ...baseParameter, + description: 'File description of multipart', + name: 'file', + type: 'file', + }); + expect(pathPost.parameters[1]).to.deep.equal({ + ...baseParameter, + description: 'FormField description of multipart', + name: 'a', + type: 'string', + }); + expect(pathPost.parameters[2]).to.deep.equal({ + ...baseParameter, + name: 'c', + type: 'string', + }); + }); }); describe('hidden paths', () => { it('should not contain hidden paths', () => {