Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,56 @@ testRule('xgen-IPA-110-collections-request-has-itemsPerPage-query-param', [
},
errors: [],
},
{
name: 'valid - non-100 default value greater than 1',
document: {
paths: {
'/resources': {
get: {
parameters: [
{
name: 'itemsPerPage',
in: 'query',
schema: {
type: 'integer',
default: 50,
},
},
],
},
},
'resources/{resourceId}': {
get: {},
},
},
},
errors: [],
},
{
name: 'valid - minimum allowed default value of 2',
document: {
paths: {
'/resources': {
get: {
parameters: [
{
name: 'itemsPerPage',
in: 'query',
schema: {
type: 'integer',
default: 2,
},
},
],
},
},
'resources/{resourceId}': {
get: {},
},
},
},
errors: [],
},
{
name: 'invalid - missing parameters',
document: {
Expand Down Expand Up @@ -195,7 +245,7 @@ testRule('xgen-IPA-110-collections-request-has-itemsPerPage-query-param', [
],
},
{
name: 'invalid - wrong default value',
name: 'invalid - default value of 0',
document: {
paths: {
'/resources': {
Expand All @@ -220,7 +270,39 @@ testRule('xgen-IPA-110-collections-request-has-itemsPerPage-query-param', [
errors: [
{
code: 'xgen-IPA-110-collections-request-has-itemsPerPage-query-param',
message: 'itemsPerPage query parameter of List method must have a default value of 100.',
message: 'itemsPerPage query parameter of List method must have a default value greater than 1.',
path: ['paths', '/resources', 'get'],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'invalid - default value of 1',
document: {
paths: {
'/resources': {
get: {
parameters: [
{
name: 'itemsPerPage',
in: 'query',
schema: {
type: 'integer',
default: 1,
},
},
],
},
},
'resources/{resourceId}': {
get: {},
},
},
},
errors: [
{
code: 'xgen-IPA-110-collections-request-has-itemsPerPage-query-param',
message: 'itemsPerPage query parameter of List method must have a default value greater than 1.',
path: ['paths', '/resources', 'get'],
severity: DiagnosticSeverity.Error,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export default (input, _, { path, documentInventory, rule }) => {
return;
}

const errors = checkPaginationQueryParameterAndReturnErrors(input, path, 'itemsPerPage', 100, ruleName);
const errors = checkPaginationQueryParameterAndReturnErrors(input, path, 'itemsPerPage', { min: 1 }, ruleName);
return evaluateAndCollectAdoptionStatus(errors, ruleName, input, path);
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export default (input, _, { path, documentInventory, rule }) => {
return;
}

const errors = checkPaginationQueryParameterAndReturnErrors(input, path, 'pageNum', 1, ruleName);
const errors = checkPaginationQueryParameterAndReturnErrors(input, path, 'pageNum', { value: 1 }, ruleName);
return evaluateAndCollectAdoptionStatus(errors, ruleName, input, path);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { handleInternalError } from '../collectionUtils.js';
* @param {Object} operation - The OpenAPI operation object to check
* @param {string[]} path - The path to the operation
* @param {string} paramName - The name of the parameter to check ('pageNum' or 'itemsPerPage')
* @param {number} defaultValue - The expected default value (1 for pageNum, 100 for itemsPerPage)
* @param {{value: number}|{min?: number, max?: number}} constraint - Either `{ value: N }` for an exact match, or `{ min: N, max: N }` for a range check (min and max are both optional)
* @param {string} ruleName - The rule name for error handling
* @returns {Array} - Array of error objects or empty array if no errors
*/
export function checkPaginationQueryParameterAndReturnErrors(operation, path, paramName, defaultValue, ruleName) {
export function checkPaginationQueryParameterAndReturnErrors(operation, path, paramName, constraint, ruleName) {
try {
const parameters = operation.parameters;

Expand Down Expand Up @@ -54,13 +54,35 @@ export function checkPaginationQueryParameterAndReturnErrors(operation, path, pa
];
}

if (param.schema.default !== defaultValue) {
return [
{
path,
message: `${paramName} query parameter of List method must have a default value of ${defaultValue}.`,
},
];
if ('value' in constraint) {
if (param.schema.default !== constraint.value) {
return [
{
path,
message: `${paramName} query parameter of List method must have a default value of ${constraint.value}.`,
},
];
}
} else {
if (!('min' in constraint) && !('max' in constraint)) {
throw new Error(`constraint must have either 'value', 'min', or 'max'`);
}
if ('min' in constraint && param.schema.default <= constraint.min) {
return [
{
path,
message: `${paramName} query parameter of List method must have a default value greater than ${constraint.min}.`,
},
];
}
if ('max' in constraint && param.schema.default > constraint.max) {
return [
{
path,
message: `${paramName} query parameter of List method must have a default value less than or equal to ${constraint.max}.`,
},
];
}
}

return [];
Expand Down
Loading