mirror of
https://github.com/elastic/kibana.git
synced 2025-06-27 18:51:07 -04:00
feat(slo): limit perPage in find api (#167185)
This commit is contained in:
parent
911ed802b7
commit
c2745d3c19
5 changed files with 23 additions and 1 deletions
|
@ -160,7 +160,8 @@
|
|||
"description": "The number of SLOs to return per page",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"default": 25
|
||||
"default": 25,
|
||||
"maximum": 5000
|
||||
},
|
||||
"example": 25
|
||||
},
|
||||
|
|
|
@ -99,6 +99,7 @@ paths:
|
|||
schema:
|
||||
type: integer
|
||||
default: 25
|
||||
maximum: 5000
|
||||
example: 25
|
||||
- name: sortBy
|
||||
in: query
|
||||
|
|
|
@ -79,6 +79,7 @@ get:
|
|||
schema:
|
||||
type: integer
|
||||
default: 25
|
||||
maximum: 5000
|
||||
example: 25
|
||||
- name: sortBy
|
||||
in: query
|
||||
|
|
|
@ -138,6 +138,19 @@ describe('FindSLO', () => {
|
|||
`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('validation', () => {
|
||||
it("throws an error when 'perPage > 5000'", async () => {
|
||||
const slo = createSLO();
|
||||
mockSummarySearchClient.search.mockResolvedValueOnce(summarySearchResult(slo));
|
||||
mockRepository.findAllByIds.mockResolvedValueOnce([slo]);
|
||||
|
||||
await expect(findSLO.execute({ perPage: '5000' })).resolves.not.toThrow();
|
||||
await expect(findSLO.execute({ perPage: '5001' })).rejects.toThrowError(
|
||||
'perPage limit to 5000'
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function summarySearchResult(slo: SLO): Paginated<SLOSummary> {
|
||||
|
|
|
@ -7,11 +7,13 @@
|
|||
|
||||
import { FindSLOParams, FindSLOResponse, findSLOResponseSchema } from '@kbn/slo-schema';
|
||||
import { SLO, SLOWithSummary } from '../../domain/models';
|
||||
import { IllegalArgumentError } from '../../errors';
|
||||
import { SLORepository } from './slo_repository';
|
||||
import { Pagination, SLOSummary, Sort, SummarySearchClient } from './summary_search_client';
|
||||
|
||||
const DEFAULT_PAGE = 1;
|
||||
const DEFAULT_PER_PAGE = 25;
|
||||
const MAX_PER_PAGE = 5000;
|
||||
|
||||
export class FindSLO {
|
||||
constructor(
|
||||
|
@ -52,6 +54,10 @@ function toPagination(params: FindSLOParams): Pagination {
|
|||
const page = Number(params.page);
|
||||
const perPage = Number(params.perPage);
|
||||
|
||||
if (!isNaN(perPage) && perPage > MAX_PER_PAGE) {
|
||||
throw new IllegalArgumentError('perPage limit to 5000');
|
||||
}
|
||||
|
||||
return {
|
||||
page: !isNaN(page) && page >= 1 ? page : DEFAULT_PAGE,
|
||||
perPage: !isNaN(perPage) && perPage >= 1 ? perPage : DEFAULT_PER_PAGE,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue