From b4569aca9cb3456a79dd8ea046e5ec042f867cd0 Mon Sep 17 00:00:00 2001 From: tatomyr Date: Thu, 6 Jun 2024 01:55:06 +0300 Subject: [PATCH] handle x-types inside parameters --- .../__tests__/__snapshots__/e2e.test.js.snap | 93 +++++++++++++++++++ applications/__tests__/e2e.test.js | 14 +++ ...penapi-with-x-types-inside-parameters.yaml | 26 ++++++ applications/x-redocly.yaml | 3 +- applications/x-types-decorators.js | 12 ++- applications/x-types-plugin.js | 12 ++- 6 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 applications/resources/openapi-with-x-types-inside-parameters.yaml diff --git a/applications/__tests__/__snapshots__/e2e.test.js.snap b/applications/__tests__/__snapshots__/e2e.test.js.snap index b760be5..c34a61a 100644 --- a/applications/__tests__/__snapshots__/e2e.test.js.snap +++ b/applications/__tests__/__snapshots__/e2e.test.js.snap @@ -131,6 +131,42 @@ components: " `; +exports[`bundle > bundle and translate x-type to schema inside parameters 1`] = ` +"openapi: 3.1.0 +info: + title: Test + version: 1.0.0 +paths: + /test: + get: + parameters: + - name: correct + in: query + x-type: string + example: Test + schema: + type: string + - name: wrong-example + in: query + x-type: string + example: 42 + schema: + type: string + - name: lacks-x-type + in: query + - $ref: '#/components/parameters/Referenced' +components: + parameters: + Referenced: + name: referenced-wrong-example + in: query + x-type: string + example: true + schema: + type: string +" +`; + exports[`bundle > do not add schemas if there is no x-type 1`] = ` "openapi: 3.1.0 info: @@ -438,6 +474,63 @@ run \`redocly lint --generate-ignore-file\` to add all problems to the ignore fi " `; +exports[`lint > lints with x-types inside parameters 1`] = ` +"validating applications/resources/openapi-with-x-types-inside-parameters.yaml... +[1] applications/resources/openapi-with-x-types-inside-parameters.yaml:16:20 at #/paths/~1test/get/parameters/1/example + +Example value must conform to the schema: type must be string. + +14 | in: query +15 | x-type: string +16 | example: 42 + | ^^ +17 | - name: lacks-x-type +18 | in: query + +referenced from applications/resources/openapi-with-x-types-inside-parameters.yaml:13:11 at #/paths/~1test/get/parameters/1 + +Error was generated by the no-invalid-parameter-examples rule. + + +[2] applications/resources/openapi-with-x-types-inside-parameters.yaml:17:11 at #/paths/~1test/get/parameters/2 + +Must contain at least one of the following fields: schema, content, x-type. + +15 | x-type: string +16 | example: 42 +17 | - name: lacks-x-type + | ^^^^^^^^^^^^^^^^^^ +18 | in: query + | ^^^^^^^^^ +19 | - $ref: "#/components/parameters/Referenced" +20 | components: + +Error was generated by the spec rule. + + +[3] applications/resources/openapi-with-x-types-inside-parameters.yaml:26:16 at #/components/parameters/Referenced/example + +Example value must conform to the schema: type must be string. + +24 | in: query +25 | x-type: string +26 | example: true + | ^^^^ +27 | + +referenced from applications/resources/openapi-with-x-types-inside-parameters.yaml:23:7 at #/components/parameters/Referenced + +Error was generated by the no-invalid-parameter-examples rule. + + +applications/resources/openapi-with-x-types-inside-parameters.yaml: validated in ms + +❌ Validation failed with 3 errors. +run \`redocly lint --generate-ignore-file\` to add all problems to the ignore file. + +" +`; + exports[`lint > lints x-openapi-with-refs.yaml 1`] = ` "validating applications/outputs/x-openapi-with-refs.yaml... [1] applications/outputs/x-openapi-with-refs.yaml:23:21 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value diff --git a/applications/__tests__/e2e.test.js b/applications/__tests__/e2e.test.js index 0ba8cee..3e3dad4 100644 --- a/applications/__tests__/e2e.test.js +++ b/applications/__tests__/e2e.test.js @@ -48,6 +48,13 @@ describe("bundle", () => { ) expect(stderr).toMatchSnapshot() }) + + test("bundle and translate x-type to schema inside parameters", () => { + const {stdout} = runCommand( + "redocly bundle applications/resources/openapi-with-x-types-inside-parameters.yaml --config=applications/x-redocly.yaml" + ) + expect(stdout).toMatchSnapshot() + }) }) describe("lint", () => { @@ -81,4 +88,11 @@ describe("lint", () => { ) expect(stripCWD(stderr)).toMatchSnapshot() }) + + test("lints with x-types inside parameters", () => { + const {stderr} = runCommand( + "redocly lint applications/resources/openapi-with-x-types-inside-parameters.yaml --config=applications/x-redocly.yaml" + ) + expect(stderr).toMatchSnapshot() + }) }) diff --git a/applications/resources/openapi-with-x-types-inside-parameters.yaml b/applications/resources/openapi-with-x-types-inside-parameters.yaml new file mode 100644 index 0000000..b783451 --- /dev/null +++ b/applications/resources/openapi-with-x-types-inside-parameters.yaml @@ -0,0 +1,26 @@ +openapi: 3.1.0 +info: + title: Test + version: 1.0.0 +paths: + /test: + get: + parameters: + - name: correct + in: query + x-type: string + example: Test + - name: wrong-example + in: query + x-type: string + example: 42 + - name: lacks-x-type + in: query + - $ref: "#/components/parameters/Referenced" +components: + parameters: + Referenced: + name: referenced-wrong-example + in: query + x-type: string + example: true diff --git a/applications/x-redocly.yaml b/applications/x-redocly.yaml index 62c2e1e..9100d63 100644 --- a/applications/x-redocly.yaml +++ b/applications/x-redocly.yaml @@ -1,7 +1,8 @@ rules: spec: error - no-invalid-media-type-examples: error no-unresolved-refs: error + no-invalid-media-type-examples: error + no-invalid-parameter-examples: error extends: - x-types/all diff --git a/applications/x-types-decorators.js b/applications/x-types-decorators.js index 7d0966e..0be37a2 100644 --- a/applications/x-types-decorators.js +++ b/applications/x-types-decorators.js @@ -6,15 +6,21 @@ const generateSchema = () => { MediaType: { leave(mediaType, ctx) { if (typeof mediaType["x-type"] === "undefined") return - const schema = translateXTypeToSchema(mediaType["x-type"], ctx) mediaType.schema = schema }, }, + Parameter: { + leave(parameter, ctx) { + if (typeof parameter["x-type"] === "undefined") return + const schema = translateXTypeToSchema(parameter["x-type"], ctx) + parameter.schema = schema + }, + }, } } -const create$Refs = () => { +const createRefs = () => { return { any: { enter: (node, ctx) => { @@ -34,5 +40,5 @@ const create$Refs = () => { module.exports = { generateSchema, - create$Refs, + createRefs, } diff --git a/applications/x-types-plugin.js b/applications/x-types-plugin.js index a0f101a..62c7346 100644 --- a/applications/x-types-plugin.js +++ b/applications/x-types-plugin.js @@ -1,4 +1,4 @@ -const {generateSchema, create$Refs} = require("./x-types-decorators") +const {generateSchema, createRefs} = require("./x-types-decorators") const {noRefNeighbors} = require("./x-types-rules") const getType = value => { @@ -75,7 +75,7 @@ module.exports = { preprocessors: { oas3: { - "create-$refs": create$Refs, + "create-$refs": createRefs, }, }, @@ -114,6 +114,14 @@ module.exports = { "x-type": getType, }, }, + Parameter: { + ...types.Parameter, + properties: { + ...types.Parameter.properties, + "x-type": getType, + }, + requiredOneOf: ["schema", "content", "x-type"], + }, Components: { ...types.Components, properties: {