Skip to content

Commit

Permalink
Merge pull request lukeautry#1249 from anton-iskryzhytskyi/fix/allow_…
Browse files Browse the repository at this point in the history
…describe_multipart_formdata_params

fix(cli): add description for multipart/form-data params
  • Loading branch information
WoH authored May 7, 2022
2 parents 06a1591 + 17813fb commit 487db7d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/swagger/specGenerator3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ export class SpecGenerator3 extends SpecGenerator {
schema: {
...this.getSwaggerType(parameter.type),
...validators,
...(parameter.description && { description: parameter.description }),
},
};

Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/controllers/postController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> {
return aFile;
}

@Post('Location')
public async postModelAtLocation(): Promise<TestModel> {
return new ModelService().getModel();
Expand Down
1 change: 1 addition & 0 deletions tests/unit/swagger/parameterDetails3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`,
);
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/swagger/schemaDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/swagger/schemaDetails3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 487db7d

Please sign in to comment.