Skip to content

Commit f67ce6e

Browse files
committed
remove all unknown $-prefixed fields
1 parent 8399a0b commit f67ce6e

File tree

6 files changed

+124
-62
lines changed

6 files changed

+124
-62
lines changed

applications/__tests__/__snapshots__/e2e-bundle.test.js.snap

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,43 @@ components: {}
314314

315315
exports[`bundle > do not bundle an openapi with type never 1`] = `undefined`;
316316

317-
exports[`bundle > openapi writeOnly and readOnly fields 1`] = `
317+
exports[`bundle > openapi with $-prefixed fields 1`] = `
318+
"openapi: 3.1.0
319+
info:
320+
title: Test
321+
version: 1.0.0
322+
paths:
323+
/test:
324+
get:
325+
responses:
326+
'200':
327+
description: Test
328+
content:
329+
application/json:
330+
x-type:
331+
$unknown-prefixed-field: should be ignored/removed
332+
$literal:$literal-prefixed-key: should remain
333+
not-starting-with-$: doesn't have a special meaning, should remain
334+
schema:
335+
type: object
336+
properties:
337+
$literal-prefixed-key:
338+
type: string
339+
enum:
340+
- should remain
341+
not-starting-with-$:
342+
type: string
343+
enum:
344+
- doesn't have a special meaning, should remain
345+
required:
346+
- $literal-prefixed-key
347+
- not-starting-with-$
348+
additionalProperties: false
349+
components: {}
350+
"
351+
`;
352+
353+
exports[`bundle > openapi with writeOnly and readOnly fields 1`] = `
318354
"openapi: 3.1.0
319355
info:
320356
title: Test writeOnly/readOnly fields

applications/__tests__/__snapshots__/e2e-lint.test.js.snap

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -554,64 +554,7 @@ run \`redocly lint --generate-ignore-file\` to add all problems to the ignore fi
554554
"
555555
`;
556556
557-
exports[`lint > openapi with x-types inside parameters 1`] = `
558-
"validating applications/resources/openapi-with-x-types-inside-parameters.yaml...
559-
[1] applications/resources/openapi-with-x-types-inside-parameters.yaml:16:20 at #/paths/~1test/get/parameters/1/example
560-
561-
Example value must conform to the schema: type must be string.
562-
563-
14 | in: query
564-
15 | x-type: string
565-
16 | example: 42
566-
| ^^
567-
17 | - name: lacks-x-type
568-
18 | in: query
569-
570-
referenced from applications/resources/openapi-with-x-types-inside-parameters.yaml:13:11 at #/paths/~1test/get/parameters/1
571-
572-
Error was generated by the no-invalid-parameter-examples rule.
573-
574-
575-
[2] applications/resources/openapi-with-x-types-inside-parameters.yaml:17:11 at #/paths/~1test/get/parameters/2
576-
577-
Must contain at least one of the following fields: schema, content, x-type.
578-
579-
15 | x-type: string
580-
16 | example: 42
581-
17 | - name: lacks-x-type
582-
| ^^^^^^^^^^^^^^^^^^
583-
18 | in: query
584-
| ^^^^^^^^^
585-
19 | - $ref: '#/components/parameters/Referenced'
586-
20 | components:
587-
588-
Error was generated by the spec rule.
589-
590-
591-
[3] applications/resources/openapi-with-x-types-inside-parameters.yaml:26:16 at #/components/parameters/Referenced/example
592-
593-
Example value must conform to the schema: type must be string.
594-
595-
24 | in: query
596-
25 | x-type: string
597-
26 | example: true
598-
| ^^^^
599-
27 |
600-
601-
referenced from applications/resources/openapi-with-x-types-inside-parameters.yaml:23:7 at #/components/parameters/Referenced
602-
603-
Error was generated by the no-invalid-parameter-examples rule.
604-
605-
606-
applications/resources/openapi-with-x-types-inside-parameters.yaml: validated in <test>ms
607-
608-
❌ Validation failed with 3 errors.
609-
run \`redocly lint --generate-ignore-file\` to add all problems to the ignore file.
610-
611-
"
612-
`;
613-
614-
exports[`lint > openapi writeOnly and readOnly fields 1`] = `
557+
exports[`lint > openapi with writeOnly and readOnly fields 1`] = `
615558
"validating applications/resources/openapi-with-writeonly-and-readonly.yaml...
616559
WARNING! Got an 'undefined' type.
617560
[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
706649
"
707650
`;
708651
652+
exports[`lint > openapi with x-types inside parameters 1`] = `
653+
"validating applications/resources/openapi-with-x-types-inside-parameters.yaml...
654+
[1] applications/resources/openapi-with-x-types-inside-parameters.yaml:16:20 at #/paths/~1test/get/parameters/1/example
655+
656+
Example value must conform to the schema: type must be string.
657+
658+
14 | in: query
659+
15 | x-type: string
660+
16 | example: 42
661+
| ^^
662+
17 | - name: lacks-x-type
663+
18 | in: query
664+
665+
referenced from applications/resources/openapi-with-x-types-inside-parameters.yaml:13:11 at #/paths/~1test/get/parameters/1
666+
667+
Error was generated by the no-invalid-parameter-examples rule.
668+
669+
670+
[2] applications/resources/openapi-with-x-types-inside-parameters.yaml:17:11 at #/paths/~1test/get/parameters/2
671+
672+
Must contain at least one of the following fields: schema, content, x-type.
673+
674+
15 | x-type: string
675+
16 | example: 42
676+
17 | - name: lacks-x-type
677+
| ^^^^^^^^^^^^^^^^^^
678+
18 | in: query
679+
| ^^^^^^^^^
680+
19 | - $ref: '#/components/parameters/Referenced'
681+
20 | components:
682+
683+
Error was generated by the spec rule.
684+
685+
686+
[3] applications/resources/openapi-with-x-types-inside-parameters.yaml:26:16 at #/components/parameters/Referenced/example
687+
688+
Example value must conform to the schema: type must be string.
689+
690+
24 | in: query
691+
25 | x-type: string
692+
26 | example: true
693+
| ^^^^
694+
27 |
695+
696+
referenced from applications/resources/openapi-with-x-types-inside-parameters.yaml:23:7 at #/components/parameters/Referenced
697+
698+
Error was generated by the no-invalid-parameter-examples rule.
699+
700+
701+
applications/resources/openapi-with-x-types-inside-parameters.yaml: validated in <test>ms
702+
703+
❌ Validation failed with 3 errors.
704+
run \`redocly lint --generate-ignore-file\` to add all problems to the ignore file.
705+
706+
"
707+
`;
708+
709709
exports[`lint > x-types described with x-types themselves 1`] = `
710710
"validating applications/resources/x-types-described-with-x-types.yaml...
711711
WARNING! Circular reference detected: #/Or

applications/__tests__/e2e-bundle.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,17 @@ describe('bundle', () => {
9292
)
9393
})
9494

95-
test('openapi writeOnly and readOnly fields', () => {
95+
test('openapi with writeOnly and readOnly fields', () => {
9696
const {stdout} = runCommand(
9797
'redocly bundle applications/resources/openapi-with-writeonly-and-readonly.yaml --config=applications/x-redocly.yaml'
9898
)
9999
expect(stdout).toMatchSnapshot()
100100
})
101+
102+
test('openapi with $-prefixed fields', () => {
103+
const {stdout} = runCommand(
104+
'redocly bundle applications/resources/openapi-dollar-prefixed-fields.yaml --config=applications/x-redocly.yaml'
105+
)
106+
expect(stdout).toMatchSnapshot()
107+
})
101108
})

applications/__tests__/e2e-lint.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('lint', () => {
6868
expect(stripCWD(stderr)).toMatchSnapshot()
6969
})
7070

71-
test('openapi writeOnly and readOnly fields', () => {
71+
test('openapi with writeOnly and readOnly fields', () => {
7272
const {stderr} = runCommand(
7373
'redocly lint applications/resources/openapi-with-writeonly-and-readonly.yaml --config=applications/x-redocly.yaml'
7474
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Test
4+
version: 1.0.0
5+
paths:
6+
/test:
7+
get:
8+
responses:
9+
200:
10+
description: Test
11+
content:
12+
application/json:
13+
x-type:
14+
$unknown-prefixed-field: should be ignored/removed
15+
$literal:$literal-prefixed-key: should remain
16+
not-starting-with-$: doesn't have a special meaning, should remain

applications/x-types-adapter.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,15 @@ export const translateXTypeToSchema = xType => {
172172
let properties = {}
173173
let patternProperties = {}
174174
let required = []
175-
const {string, $descriptions, $schema, ...props} = xType
175+
const {string, $descriptions, ...props} = xType
176176

177177
const additionalProperties =
178178
typeof string === 'undefined' ? false : translateXTypeToSchema(string)
179179

180180
for (const key in props) {
181+
// Remove all unknown $-prefixed fields
182+
if (key.startsWith('$') && !key.startsWith('$literal:')) continue
183+
181184
if (key.startsWith('string::pattern(') && key.endsWith(')')) {
182185
// Handle patternProperties
183186
const pattern = key.slice('string::pattern('.length, -1)

0 commit comments

Comments
 (0)