From e96e249bf7cfc25e18731ba7e7bd8be465e274a1 Mon Sep 17 00:00:00 2001 From: O_Zyzych Date: Thu, 10 Nov 2022 23:54:41 +0200 Subject: [PATCH 1/2] added custom rule --- openapi/petstore.yaml | 3 ++- openapi/reference.page.yaml | 1 + plugins/plugin.js | 14 ++++++++++++++ plugins/rules/operation-id-not-test.js | 14 ++++++++++++++ redocly.yaml | 11 +++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 plugins/plugin.js create mode 100644 plugins/rules/operation-id-not-test.js create mode 100644 redocly.yaml diff --git a/openapi/petstore.yaml b/openapi/petstore.yaml index 5cf68e7b..efbcc4a1 100644 --- a/openapi/petstore.yaml +++ b/openapi/petstore.yaml @@ -1,4 +1,5 @@ openapi: 3.0.3 +security: [] servers: - url: '//petstore.swagger.io/v2' info: @@ -35,7 +36,7 @@ paths: - pet summary: Add a new pet to the store description: '' - operationId: addPet + operationId: test responses: '200': description: OK diff --git a/openapi/reference.page.yaml b/openapi/reference.page.yaml index 451207b2..7620496a 100644 --- a/openapi/reference.page.yaml +++ b/openapi/reference.page.yaml @@ -6,3 +6,4 @@ settings: - lang: curl - lang: JavaScript - lang: Node.js + diff --git a/plugins/plugin.js b/plugins/plugin.js new file mode 100644 index 00000000..5bf2a290 --- /dev/null +++ b/plugins/plugin.js @@ -0,0 +1,14 @@ +const OperationIdNotTest = require('./rules/operation-id-not-test'); +const id = 'plugin'; + +/** @type {import('@redocly/cli').DecoratorsConfig} */ +const rules = { + oas3: { + 'operation-id-not-test': OperationIdNotTest, + }, +}; + +module.exports = { + id, + rules, +}; \ No newline at end of file diff --git a/plugins/rules/operation-id-not-test.js b/plugins/rules/operation-id-not-test.js new file mode 100644 index 00000000..195ab001 --- /dev/null +++ b/plugins/rules/operation-id-not-test.js @@ -0,0 +1,14 @@ +module.exports = OperationIdNotTest; + +function OperationIdNotTest() { + return { + Operation(operation, ctx) { + if (operation.operationId === 'test') { + ctx.report({ + message: `operationId must be not "test"`, + location: ctx.location.child('operationId'), + }); + } + }, + }; +} \ No newline at end of file diff --git a/redocly.yaml b/redocly.yaml new file mode 100644 index 00000000..5f17c3e9 --- /dev/null +++ b/redocly.yaml @@ -0,0 +1,11 @@ +apis: + sample@v1: + root: openapi/petstore.yaml +rules: + plugin/operation-id-not-test: + severity: error + operation-4xx-response: + severity: off + +plugins: + - "./plugins/plugin.js" From ed31d9524cbb897db3a89ea034116468b1dfac79 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Fri, 11 Nov 2022 10:02:41 +0800 Subject: [PATCH 2/2] Apply suggestions from code review --- openapi/petstore.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi/petstore.yaml b/openapi/petstore.yaml index efbcc4a1..0e47b548 100644 --- a/openapi/petstore.yaml +++ b/openapi/petstore.yaml @@ -36,7 +36,7 @@ paths: - pet summary: Add a new pet to the store description: '' - operationId: test + operationId: addPet responses: '200': description: OK