Skip to content

Commit f31d90a

Browse files
authored
fix: swagger body array type (#4075)
1 parent 10d5761 commit f31d90a

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

packages/swagger/src/swaggerExplorer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export class SwaggerExplorer {
438438
param.metadata.schema['type'] = param.metadata.type;
439439
delete param.metadata.type;
440440
}
441-
if (param.metadata.isArray !== undefined) {
441+
if (param.metadata.isArray) {
442442
param.metadata.schema['items'] = {
443443
type: param.metadata.schema['type'],
444444
};

packages/swagger/test/__snapshots__/parser.test.ts.snap

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,7 @@ exports[`test @ApiBody should test ApiBody 1`] = `
165165
"content": {
166166
"application/json": {
167167
"schema": {
168-
"items": {
169-
"$ref": "#/components/schemas/Cat",
170-
},
171-
"type": "array",
168+
"$ref": "#/components/schemas/Cat",
172169
},
173170
},
174171
},
@@ -269,6 +266,74 @@ exports[`test @ApiBody should test ApiBody with array 1`] = `
269266
}
270267
`;
271268

269+
exports[`test @ApiBody should test ApiBody with array false 1`] = `
270+
{
271+
"components": {
272+
"schemas": {
273+
"Cat": {
274+
"properties": {
275+
"age": {
276+
"description": "The age of the Cat",
277+
"example": 1,
278+
"type": "number",
279+
},
280+
"name": {
281+
"description": "The name of the Cat",
282+
"example": "Kitty",
283+
"type": "string",
284+
},
285+
},
286+
"type": "object",
287+
},
288+
},
289+
},
290+
"info": {
291+
"contact": {},
292+
"description": "",
293+
"title": "",
294+
"version": "1.0.0",
295+
},
296+
"openapi": "3.0.1",
297+
"paths": {
298+
"/api/update_user": {
299+
"post": {
300+
"description": undefined,
301+
"operationId": "apicontroller_updateuser",
302+
"parameters": [],
303+
"requestBody": {
304+
"content": {
305+
"application/json": {
306+
"schema": {
307+
"$ref": "#/components/schemas/Cat",
308+
},
309+
},
310+
},
311+
"description": undefined,
312+
"required": true,
313+
},
314+
"responses": {
315+
"200": {
316+
"description": "OK",
317+
},
318+
},
319+
"summary": undefined,
320+
"tags": [
321+
"api",
322+
],
323+
},
324+
},
325+
},
326+
"servers": [],
327+
"tags": [
328+
{
329+
"description": "api",
330+
"externalDocs": undefined,
331+
"name": "api",
332+
},
333+
],
334+
}
335+
`;
336+
272337
exports[`test @ApiBody should test ApiBody with binary 1`] = `
273338
{
274339
"components": {},

packages/swagger/test/parser.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,37 @@ describe('test @ApiBody', () => {
416416
expect(explorer.getData()).toMatchSnapshot();
417417
});
418418

419+
it('should test ApiBody with array false', () => {
420+
class Cat {
421+
/**
422+
* The name of the Catcomment
423+
* @example Kitty
424+
*/
425+
@ApiProperty({ example: 'Kitty', description: 'The name of the Cat' })
426+
name: string;
427+
428+
@ApiProperty({ example: 1, description: 'The age of the Cat' })
429+
age: number;
430+
}
431+
432+
@Controller('/api')
433+
@ApiExtraModel(Cat)
434+
class APIController {
435+
@Post('/update_user')
436+
@ApiBody({
437+
type: Cat,
438+
isArray: false,
439+
})
440+
async updateUser(@Body() cat: Cat[]) {
441+
// ...
442+
}
443+
}
444+
445+
const explorer = new CustomSwaggerExplorer();
446+
explorer.generatePath(APIController);
447+
expect(explorer.getData()).toMatchSnapshot();
448+
});
449+
419450
it('should test ApiBody with example', () => {
420451
class Cat {
421452
@ApiProperty({ example: 'Kitty', description: 'The name of the Cat' })

0 commit comments

Comments
 (0)