Skip to content

Commit

Permalink
handle transitive references (enhance components types)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Apr 6, 2024
1 parent 0fe898f commit f283685
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 27 deletions.
68 changes: 55 additions & 13 deletions applications/__tests__/__snapshots__/e2e.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,28 @@ paths:
value: false
schema:
type: number
'203':
description: A transitive reference
content:
application/json:
x-type:
$ref: '#/components/x-types/Bar'
examples:
Correct:
value:
- anything
Incorrect:
value: anything
schema:
type: array
items:
anyOf:
- type: string
- type: number
- type: boolean
- type: object
- type: array
- type: 'null'
'404':
description: A wrong ref
content:
Expand All @@ -254,6 +276,9 @@ paths:
components:
x-types:
Foo: any
Bar:
array:
$ref: '#/components/x-types/Foo'
"
`;

Expand Down Expand Up @@ -301,15 +326,16 @@ run \`redocly lint --generate-ignore-file\` to add all problems to the ignore fi
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:103:10 at #/components/x-types/Foo
[1] applications/outputs/x-openapi-with-refs.yaml:125:10 at #/components/x-types/Foo
Expected type \`XTypeObject\` (object) but got \`string\`
101 | components:
102 | x-types:
103 | Foo: any
123 | components:
124 | x-types:
125 | Foo: any
| ^^^
104 |
126 | Bar:
127 | array:
referenced from applications/outputs/x-openapi-with-refs.yaml:15:19 at #/paths/~1test/get/responses/200/content/application~1json/x-type/AFieldWithRegularRef
Expand Down Expand Up @@ -380,23 +406,39 @@ referenced from applications/outputs/x-openapi-with-refs.yaml:70:15 at #/paths/~
Error was generated by the no-invalid-media-type-examples rule.
[6] applications/outputs/x-openapi-with-refs.yaml:84:19 at #/paths/~1test/get/responses/404/content/application~1json/x-type/AWrongRef
[6] applications/outputs/x-openapi-with-refs.yaml:89:26 at #/paths/~1test/get/responses/203/content/application~1json/examples/Incorrect/value
Example value must conform to the schema: type must be array.
87 | - anything
88 | Incorrect:
89 | value: anything
| ^^^^^^^^
90 | schema:
91 | type: array
referenced from applications/outputs/x-openapi-with-refs.yaml:82:15 at #/paths/~1test/get/responses/203/content/application~1json
Error was generated by the no-invalid-media-type-examples rule.
[7] applications/outputs/x-openapi-with-refs.yaml:106:19 at #/paths/~1test/get/responses/404/content/application~1json/x-type/AWrongRef
Can't resolve $ref: ENOENT: no such file or directory './applications/outputs/wrong-file.yaml'
82 | x-type:
83 | AWrongRef:
84 | $ref: wrong-file.yaml
| ^^^^^^^^^^^^^^^^^^^^^
85 | example:
86 | AWrongRef: Accepts anything
104 | x-type:
105 | AWrongRef:
106 | $ref: wrong-file.yaml
| ^^^^^^^^^^^^^^^^^^^^^
107 | example:
108 | AWrongRef: Accepts anything
Error was generated by the no-unresolved-refs rule.
applications/outputs/x-openapi-with-refs.yaml: validated in <test>ms
❌ Validation failed with 6 errors.
❌ Validation failed with 7 errors.
run \`redocly lint --generate-ignore-file\` to add all problems to the ignore file.
"
Expand Down
12 changes: 12 additions & 0 deletions applications/resources/openapi-with-refs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ paths:
value: 42
Incorrect:
value: false
203:
description: A transitive reference
content:
application/json:
x-type: $ref:#/components/x-types/Bar
examples:
Correct:
value: [anything]
Incorrect:
value: anything
404:
description: A wrong ref
content:
Expand All @@ -51,3 +61,5 @@ paths:
components:
x-types:
Foo: any
Bar:
array: $ref:#/components/x-types/Foo
12 changes: 12 additions & 0 deletions applications/x-types-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ module.exports = {
"x-type": getType,
},
},
Components: {
...types.Components,
properties: {
...types.Components.properties,
"x-types": {
name: "NamedXTypes",
properties: {},
additionalProperties: getType,
},
},
},
// TODO: Add more types here
}
},
},
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "This document attempts to describe data types (primarily aimed at JSON-like structures) in a simple and natural way. Any [valid JSON](https://www.json.org/) could be validated against an **JSON X-Types** definition.",
"main": "index.js",
"scripts": {
"translate": "redocly bundle applications/resources/openapi.yaml -o=applications/outputs/x-openapi.yaml --force --config=applications/x-inline-refs-config-redocly.yaml && redocly lint applications/outputs/x-openapi.yaml --config=applications/x-inline-refs-config-redocly.yaml",
"refs": "redocly bundle applications/resources/openapi-with-refs.yaml -o=applications/outputs/x-openapi-with-refs.yaml --force --config=applications/x-inline-refs-config-redocly.yaml && redocly lint applications/outputs/x-openapi-with-refs.yaml --config=applications/x-inline-refs-config-redocly.yaml",
"lint": "eslint .",
"test": "vitest",
"coverage": "vitest run --coverage",
Expand All @@ -26,7 +26,7 @@
},
"homepage": "https://github.com/tatomyr/json-types#readme",
"devDependencies": {
"@redocly/cli": "^1.10.6",
"@redocly/cli": "^1.11.0",
"@vitest/coverage-v8": "^1.4.0",
"eslint": "^8.57.0",
"prettier": "^3.2.5",
Expand Down

0 comments on commit f283685

Please sign in to comment.