Skip to content

Commit

Permalink
remove all unknown $-prefixed fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Oct 23, 2024
1 parent 8399a0b commit f67ce6e
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 62 deletions.
38 changes: 37 additions & 1 deletion applications/__tests__/__snapshots__/e2e-bundle.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
116 changes: 58 additions & 58 deletions applications/__tests__/__snapshots__/e2e-lint.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 <test>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
Expand Down Expand Up @@ -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 <test>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
Expand Down
9 changes: 8 additions & 1 deletion applications/__tests__/e2e-bundle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
2 changes: 1 addition & 1 deletion applications/__tests__/e2e-lint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down
16 changes: 16 additions & 0 deletions applications/resources/openapi-dollar-prefixed-fields.yaml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion applications/x-types-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f67ce6e

Please sign in to comment.