From f67ce6ecfba1f414c9b7df7a32452f50baa1b4c9 Mon Sep 17 00:00:00 2001 From: tatomyr Date: Wed, 23 Oct 2024 13:14:02 +0300 Subject: [PATCH] remove all unknown $-prefixed fields --- .../__snapshots__/e2e-bundle.test.js.snap | 38 +++++- .../__snapshots__/e2e-lint.test.js.snap | 116 +++++++++--------- applications/__tests__/e2e-bundle.test.js | 9 +- applications/__tests__/e2e-lint.test.js | 2 +- .../openapi-dollar-prefixed-fields.yaml | 16 +++ applications/x-types-adapter.js | 5 +- 6 files changed, 124 insertions(+), 62 deletions(-) create mode 100644 applications/resources/openapi-dollar-prefixed-fields.yaml diff --git a/applications/__tests__/__snapshots__/e2e-bundle.test.js.snap b/applications/__tests__/__snapshots__/e2e-bundle.test.js.snap index a35d489..3806657 100644 --- a/applications/__tests__/__snapshots__/e2e-bundle.test.js.snap +++ b/applications/__tests__/__snapshots__/e2e-bundle.test.js.snap @@ -314,7 +314,43 @@ components: {} exports[`bundle > do not bundle an openapi with type never 1`] = `undefined`; -exports[`bundle > openapi writeOnly and readOnly fields 1`] = ` +exports[`bundle > openapi with $-prefixed fields 1`] = ` +"openapi: 3.1.0 +info: + title: Test + version: 1.0.0 +paths: + /test: + get: + responses: + '200': + description: Test + content: + application/json: + x-type: + $unknown-prefixed-field: should be ignored/removed + $literal:$literal-prefixed-key: should remain + not-starting-with-$: doesn't have a special meaning, should remain + schema: + type: object + properties: + $literal-prefixed-key: + type: string + enum: + - should remain + not-starting-with-$: + type: string + enum: + - doesn't have a special meaning, should remain + required: + - $literal-prefixed-key + - not-starting-with-$ + additionalProperties: false +components: {} +" +`; + +exports[`bundle > openapi with writeOnly and readOnly fields 1`] = ` "openapi: 3.1.0 info: title: Test writeOnly/readOnly fields diff --git a/applications/__tests__/__snapshots__/e2e-lint.test.js.snap b/applications/__tests__/__snapshots__/e2e-lint.test.js.snap index 860ff46..aa09e00 100644 --- a/applications/__tests__/__snapshots__/e2e-lint.test.js.snap +++ b/applications/__tests__/__snapshots__/e2e-lint.test.js.snap @@ -554,64 +554,7 @@ run \`redocly lint --generate-ignore-file\` to add all problems to the ignore fi " `; -exports[`lint > openapi 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 > openapi writeOnly and readOnly fields 1`] = ` +exports[`lint > openapi with writeOnly and readOnly fields 1`] = ` "validating applications/resources/openapi-with-writeonly-and-readonly.yaml... WARNING! Got an 'undefined' type. [1] applications/resources/openapi-with-writeonly-and-readonly.yaml:21:23 at #/paths/~1test/get/requestBody/content/application~1json/examples/Incorrect/value/id @@ -706,6 +649,63 @@ run \`redocly lint --generate-ignore-file\` to add all problems to the ignore fi " `; +exports[`lint > openapi 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 > x-types described with x-types themselves 1`] = ` "validating applications/resources/x-types-described-with-x-types.yaml... WARNING! Circular reference detected: #/Or diff --git a/applications/__tests__/e2e-bundle.test.js b/applications/__tests__/e2e-bundle.test.js index 010ab2e..295962a 100644 --- a/applications/__tests__/e2e-bundle.test.js +++ b/applications/__tests__/e2e-bundle.test.js @@ -92,10 +92,17 @@ describe('bundle', () => { ) }) - test('openapi writeOnly and readOnly fields', () => { + test('openapi with writeOnly and readOnly fields', () => { const {stdout} = runCommand( 'redocly bundle applications/resources/openapi-with-writeonly-and-readonly.yaml --config=applications/x-redocly.yaml' ) expect(stdout).toMatchSnapshot() }) + + test('openapi with $-prefixed fields', () => { + const {stdout} = runCommand( + 'redocly bundle applications/resources/openapi-dollar-prefixed-fields.yaml --config=applications/x-redocly.yaml' + ) + expect(stdout).toMatchSnapshot() + }) }) diff --git a/applications/__tests__/e2e-lint.test.js b/applications/__tests__/e2e-lint.test.js index 57aed76..53ddd4b 100644 --- a/applications/__tests__/e2e-lint.test.js +++ b/applications/__tests__/e2e-lint.test.js @@ -68,7 +68,7 @@ describe('lint', () => { expect(stripCWD(stderr)).toMatchSnapshot() }) - test('openapi writeOnly and readOnly fields', () => { + test('openapi with writeOnly and readOnly fields', () => { const {stderr} = runCommand( 'redocly lint applications/resources/openapi-with-writeonly-and-readonly.yaml --config=applications/x-redocly.yaml' ) diff --git a/applications/resources/openapi-dollar-prefixed-fields.yaml b/applications/resources/openapi-dollar-prefixed-fields.yaml new file mode 100644 index 0000000..3d1eb86 --- /dev/null +++ b/applications/resources/openapi-dollar-prefixed-fields.yaml @@ -0,0 +1,16 @@ +openapi: 3.1.0 +info: + title: Test + version: 1.0.0 +paths: + /test: + get: + responses: + 200: + description: Test + content: + application/json: + x-type: + $unknown-prefixed-field: should be ignored/removed + $literal:$literal-prefixed-key: should remain + not-starting-with-$: doesn't have a special meaning, should remain diff --git a/applications/x-types-adapter.js b/applications/x-types-adapter.js index 641623c..1d016b5 100644 --- a/applications/x-types-adapter.js +++ b/applications/x-types-adapter.js @@ -172,12 +172,15 @@ export const translateXTypeToSchema = xType => { let properties = {} let patternProperties = {} let required = [] - const {string, $descriptions, $schema, ...props} = xType + const {string, $descriptions, ...props} = xType const additionalProperties = typeof string === 'undefined' ? false : translateXTypeToSchema(string) for (const key in props) { + // Remove all unknown $-prefixed fields + if (key.startsWith('$') && !key.startsWith('$literal:')) continue + if (key.startsWith('string::pattern(') && key.endsWith(')')) { // Handle patternProperties const pattern = key.slice('string::pattern('.length, -1)