Skip to content

Commit

Permalink
chore: resolve conflicts, add types
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Oct 24, 2024
2 parents 59aa2cb + e2b6715 commit 371dd98
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 138 deletions.
163 changes: 73 additions & 90 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -548,99 +548,11 @@
}
},
{
"name": "page",
"in": "query",
"required": true,
"schema": {
"minimum": 0,
"maximum": 10000,
"exclusiveMaximum": true,
"exclusiveMinimum": true,
"title": "Page",
"format": "int32",
"default": 0,
"example": 123,
"type": "number"
}
},
{
"name": "_sortBy",
"in": "query",
"required": true,
"schema": {
"nullable": true,
"example": [
"sort1",
"sort2"
],
"type": "array",
"items": {
"type": "string"
}
}
},
{
"name": "limit",
"in": "query",
"required": true,
"schema": {
"type": "number"
}
},
{
"name": "enum",
"in": "query",
"required": true,
"schema": {
"$ref": "#/components/schemas/LettersEnum"
}
},
{
"name": "enumArr",
"in": "query",
"required": true,
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/LettersEnum"
}
}
},
{
"name": "letters",
"in": "query",
"required": true,
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Letter"
}
}
},
{
"name": "beforeDate",
"in": "query",
"name": "PaginationQuery",
"required": true,
"schema": {
"format": "date-time",
"type": "string"
}
},
{
"name": "filter",
"in": "query",
"required": true,
"schema": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"additionalProperties": true,
"type": "object"
"$ref": "#/components/schemas/PaginationQuery"
}
},
{
Expand Down Expand Up @@ -1353,6 +1265,77 @@
"B",
"C"
]
},
"PaginationQuery": {
"type": "object",
"properties": {
"page": {
"type": "number",
"minimum": 0,
"maximum": 10000,
"title": "Page",
"exclusiveMaximum": true,
"exclusiveMinimum": true,
"format": "int32",
"default": 0,
"example": 123
},
"_sortBy": {
"nullable": true,
"example": [
"sort1",
"sort2"
],
"type": "array",
"items": {
"type": "string"
}
},
"limit": {
"type": "number"
},
"enum": {
"$ref": "#/components/schemas/LettersEnum"
},
"enumArr": {
"type": "array",
"items": {
"$ref": "#/components/schemas/LettersEnum"
}
},
"letters": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Letter"
}
},
"beforeDate": {
"format": "date-time",
"type": "string"
},
"filter": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"additionalProperties": true
}
},
"required": [
"page",
"_sortBy",
"limit",
"enum",
"enumArr",
"letters",
"beforeDate",
"filter"
]
}
}
},
Expand Down
67 changes: 29 additions & 38 deletions lib/services/schema-object-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,39 @@ export class SchemaObjectFactory {
return this.createQueryOrParamSchema(param, schemas);
}

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

if (isArray) {
return {
...param,
name,
schema: {
type: 'array',
items: schema
}
};
}
getCustomType(
param: ParamWithTypeMetadata,
schemas: Record<string, SchemaObject>
) {
const modelName = this.exploreModelSchema(param.type, schemas);
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
};
}

private createQueryOrParamSchema(
Expand All @@ -111,23 +118,7 @@ export class SchemaObjectFactory {
};
}
if (isFunction(param.type)) {
const propertiesWithType = this.extractPropertiesFromType(
param.type,
schemas
);
if (!propertiesWithType) {
return param;
}
return propertiesWithType.map(
(property: ParameterObject & ParamWithTypeMetadata) => {
const parameterObject = {
...(omit(property, 'enumName') as ParameterObject),
in: param.in ?? 'query',
required: property.required ?? true
};
return parameterObject;
}
) as ParameterObject[];
return this.getCustomType(param, schemas);
}
return param;
}
Expand Down
113 changes: 113 additions & 0 deletions test/explorer/swagger-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1999,4 +1999,117 @@ 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>,
new ApplicationConfig(),
'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'
}
}
}
]);
});
});
});
Loading

0 comments on commit 371dd98

Please sign in to comment.