Skip to content
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

fix: Arrays and objects are not managed properly when located in first level of @Query() #1113

Merged
merged 1 commit into from
Oct 24, 2024
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
75 changes: 31 additions & 44 deletions lib/services/schema-object-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,40 @@ export class SchemaObjectFactory {
return this.createQueryOrParamSchema(param, schemas, schemaRefsStack);
}

const modelName = this.exploreModelSchema(
param.type,
schemas,
schemaRefsStack
);
const name = param.name || modelName;
const schema = {
...((param as BaseParameterObject).schema || {}),
$ref: getSchemaPath(modelName)
};
const isArray = param.isArray;
param = omit(param, 'isArray');

if (isArray) {
return {
...param,
name,
schema: {
type: 'array',
items: schema
}
};
}
return this.getCustomType(param, schemas, schemaRefsStack);
});
return flatten(parameterObjects);
}

getCustomType(param, schemas, schemaRefsStack) {
const modelName = this.exploreModelSchema(
param.type,
schemas,
schemaRefsStack
);
const name = param.name || modelName;
const schema = {
...((param as BaseParameterObject).schema || {}),
$ref: getSchemaPath(modelName)
};
const isArray = param.isArray;
param = omit(param, 'isArray');

if (isArray) {
return {
...param,
name,
schema
schema: {
type: 'array',
items: schema
}
};
});
return flatten(parameterObjects);
}
return {
...param,
name,
schema
};
}

createQueryOrParamSchema(
Expand All @@ -111,24 +115,7 @@ export class SchemaObjectFactory {
};
}
if (isFunction(param.type)) {
const propertiesWithType = this.extractPropertiesFromType(
param.type,
schemas,
schemaRefsStack
);
if (!propertiesWithType) {
return param;
}
return propertiesWithType.map(
(property: ParameterObject & ParamWithTypeMetadata) => {
const parameterObject = {
...(omit(property, 'enumName') as ParameterObject),
in: 'query',
required: property.required ?? true
};
return parameterObject;
}
) as ParameterObject[];
return this.getCustomType(param, schemas, schemaRefsStack);
}
return param;
}
Expand Down
112 changes: 112 additions & 0 deletions test/explorer/swagger-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,4 +971,116 @@ describe('SwaggerExplorer', () => {
]);
});
});

describe('when arrays are used', () => {
enum LettersEnum {
A = 'A',
B = 'B',
C = 'C'
}

class NestedDto {
@ApiProperty()
nestedString: string;
}

class ObjectDto {
@ApiProperty()
field: string;

@ApiProperty()
nestedObject: NestedDto;

@ApiProperty({
isArray: true,
type: NestedDto
})
nestedArrayOfObjects: NestedDto[];

@ApiProperty({
type: [NestedDto]
})
nestedArrayOfObjects2: NestedDto[];
}

class FooDto {
@ApiProperty({
isArray: true,
type: ObjectDto
})
arrayOfObjectsDto: ObjectDto[];
}

class FooController {
@Get('/route1')
route1(@Query() fooDto: FooDto) {}
@Get('/route2')
route2(@Query() objectDto: ObjectDto) {}
}

it('should properly define arrays in query', () => {
const explorer = new SwaggerExplorer(schemaObjectFactory);
const routes = explorer.exploreController(
{
instance: new FooController(),
metatype: FooController
} as InstanceWrapper<FooController>,
'path'
);

expect(routes[0].root.parameters).toEqual([
{
name: 'arrayOfObjectsDto',
required: true,
in: 'query',
schema: {
items: {
$ref: '#/components/schemas/ObjectDto'
},
type: 'array'
}
}
]);
expect(routes[1].root.parameters).toEqual([
{
name: 'field',
required: true,
in: 'query',
schema: {
type: 'string'
}
},
{
name: 'nestedObject',
required: true,
in: 'query',
schema: {
$ref: '#/components/schemas/NestedDto'
}
},
{
name: 'nestedArrayOfObjects',
required: true,
in: 'query',
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/NestedDto'
}
}
},
{
name: 'nestedArrayOfObjects2',
required: true,
in: 'query',
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/NestedDto'
}
}
}
]);
});
});
});
80 changes: 80 additions & 0 deletions test/services/schema-object-factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ApiProperty } from '../../lib/decorators';
import { SchemaObject } from '../../lib/interfaces/open-api-spec.interface';
import { ModelPropertiesAccessor } from '../../lib/services/model-properties-accessor';
import { ParamWithTypeMetadata } from '../../lib/services/parameter-metadata-accessor';
import { SchemaObjectFactory } from '../../lib/services/schema-object-factory';
import { SwaggerTypesMapper } from '../../lib/services/swagger-types-mapper';
import { CreateUserDto } from './fixtures/create-user.dto';
Expand Down Expand Up @@ -234,5 +236,83 @@ describe('SchemaObjectFactory', () => {
properties: { name: { type: 'string', minLength: 1 } }
});
});

it('should create arrays of objects', () => {
class ObjectDto {
@ApiProperty()
field: string;
}

class TestDto {
@ApiProperty()
arrayOfStrings: string[];
}

class Test2Dto {
@ApiProperty({
isArray: true,
type: ObjectDto
})
arrayOfObjects: ObjectDto[];
}

const schemas = [];
schemaObjectFactory.exploreModelSchema(TestDto, schemas);
schemaObjectFactory.exploreModelSchema(Test2Dto, schemas);

expect(schemas[0][TestDto.name]).toEqual({
type: 'object',
properties: {
arrayOfStrings: {
type: 'array',
items: {
type: 'string'
}
}
},
required: ['arrayOfStrings']
});
expect(schemas[2][Test2Dto.name]).toEqual({
type: 'object',
properties: {
arrayOfObjects: {
type: 'array',
items: {
$ref: '#/components/schemas/ObjectDto'
}
}
},
required: ['arrayOfObjects']
});
});
});

describe('createFromModel', () => {
it('should create arrays of objects', () => {
class ObjectDto {
@ApiProperty()
field: string;
}

class TestDto {
@ApiProperty()
arrayOfStrings: string[];
}

class Test2Dto {
@ApiProperty({})
arrayOfObjects: ObjectDto[];
}

const property = ApiProperty({
isArray: true,
type: ObjectDto
});

const parameters: ParamWithTypeMetadata[] = [];

const schemas: SchemaObject[] = [];
const result = schemaObjectFactory.createFromModel(parameters, schemas);
});
});
});