Minor fix to operation-id rule

This commit is contained in:
Mike Kistler 2021-09-03 06:49:06 -05:00
Родитель d396248da3
Коммит 1399ab263e
4 изменённых файлов: 22 добавлений и 14 удалений

Просмотреть файл

@ -42,7 +42,7 @@ The `Verb` of the `operationId` should be or contain a specific value depending
| operation method | verb should contain | notes |
| ---------------- | ------------------- | ------ |
| get | "Get" or "List" | should be "List" if response is pageable |
| put | "Create" | could be "CreateOrUpdate" |
| put | "Create" or "Update" | could be "CreateOrUpdate" |
| patch | "Update" | could be "CreateOrUpdate" |
| delete | "Delete" | |
@ -52,7 +52,7 @@ Operation should have a summary or description.
### az-pagination-response
If the operation returns a list that is potentially large, it should [support pagination](../opeapi-style-guidelines.md#).
If the operation returns a list that is potentially large, it should [support pagination](../opeapi-style-guidelines.md#support-for-pagination).
### az-parameter-default-not-allowed

Просмотреть файл

@ -6,21 +6,21 @@
// - patch operation should have "update" in the operationId verb [R1007]
// - delete operations should have "delete" in the "verb" component of the operationId [R1009]
module.exports = (targetVal, _opts, paths) => {
module.exports = (operation, _opts, paths) => {
// targetVal should be an operation
if (targetVal === null || typeof targetVal !== 'object') {
if (operation === null || typeof operation !== 'object') {
return [];
}
const path = paths.target || paths.given;
const errors = [];
if (!targetVal.operationId) {
if (!operation.operationId) {
// Missing operationId is caught elsewhere, so just return
return errors;
}
const m = targetVal.operationId.match(/[A-Za-z0-9]+_([A-Za-z0-9]+)/);
const m = operation.operationId.match(/[A-Za-z0-9]+_([A-Za-z0-9]+)/);
if (!m) {
errors.push({
message: 'OperationId should be of the form "Noun_Verb"',
@ -28,13 +28,13 @@ module.exports = (targetVal, _opts, paths) => {
});
}
const verb = m ? m[1] : targetVal.operationId;
const verb = m ? m[1] : operation.operationId;
const method = path[path.length - 1];
const isCreateOrUpdate = ['put', 'patch'].includes(method)
&& (targetVal.responses?.['200'] && targetVal.responses?.['201']);
const isCreate = ['put', 'patch'].includes(method) && operation.responses?.['201'];
const isUpdate = ['put', 'patch'].includes(method) && operation.responses?.['200'];
if (isCreateOrUpdate) {
if (isCreate && isUpdate) {
if (!verb.match(/create/i) || !verb.match(/update/i)) {
errors.push({
message: `OperationId for ${method} method should contain both "Create" and "Update"`,
@ -42,16 +42,16 @@ module.exports = (targetVal, _opts, paths) => {
});
}
} else {
const isList = method === 'get' && targetVal['x-ms-pageable'];
const isList = method === 'get' && operation['x-ms-pageable'];
const patterns = {
get: isList ? /list/i : /(get|list)/i,
put: /create/i,
put: isCreate ? /create/i : /(create|update)/i,
patch: /update/i,
delete: /delete/i,
};
const frags = {
get: isList ? '"List"' : '"Get" or "list"',
put: '"Create"',
put: isCreate ? '"Create"' : '"Create" or "Update"',
patch: '"Update"',
delete: '"Delete"',
};

Просмотреть файл

@ -95,7 +95,7 @@ The `Verb` of the `operationId` should be or contain a specific value depending
| operation method | verb should contain | notes |
| ---------------- | ------------------- | ------ |
| get | "Get" or "List" | should be "List" if response is pageable |
| put | "Create" | could be "CreateOrUpdate" |
| put | "Create" or "Update" | could be "CreateOrUpdate" |
| patch | "Update" | could be "CreateOrUpdate" |
| delete | "Delete" | |

Просмотреть файл

@ -175,6 +175,14 @@ test('az-operation-id should find no errors', () => {
operationId: 'noun_delete',
},
},
'/api/test6': {
put: {
operationId: 'noun_update',
200: {
description: 'Success',
},
},
},
},
};
return linter.run(oasDoc).then((results) => {