Skip to content

Commit

Permalink
return both readonly and writeonly when mode is not specified; start …
Browse files Browse the repository at this point in the history
…working on separate x-type resolver and validator; refactor transforming inline refs
  • Loading branch information
tatomyr committed Dec 20, 2024
1 parent 22a0abb commit 20950e7
Show file tree
Hide file tree
Showing 24 changed files with 530 additions and 79 deletions.
58 changes: 44 additions & 14 deletions applications/__tests__/__snapshots__/e2e-bundle.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1014,21 +1014,35 @@ paths:
content:
application/json:
x-type:
AFieldWithRegularRef: any
AFieldWithInlineRef: any
AFieldWithRegularRef:
- az
- bukh
AFieldWithInlineRef:
- az
- bukh
examples:
Correct:
value:
AFieldWithInlineRef: something
AFieldWithRegularRef: anything
AFieldWithInlineRef: az
AFieldWithRegularRef: bukh
Incorrect:
value:
AFieldWithInlineRef: wrong
AFieldWithRegularRef: wrong
NonExistingField: wrong
schema:
type: object
properties:
AFieldWithRegularRef: {}
AFieldWithInlineRef: {}
AFieldWithRegularRef:
type: string
enum:
- az
- bukh
AFieldWithInlineRef:
type: string
enum:
- az
- bukh
required:
- AFieldWithRegularRef
- AFieldWithInlineRef
Expand All @@ -1037,11 +1051,17 @@ paths:
description: A bare ref
content:
application/json:
x-type: any
x-type:
- az
- bukh
examples:
Correct:
value: Anything
schema: {}
value: az
schema:
type: string
enum:
- az
- bukh
'202':
description: A file reference
content:
Expand All @@ -1059,16 +1079,22 @@ paths:
content:
application/json:
x-type:
array: any
array:
- az
- bukh
examples:
Correct:
value:
- anything
- az
Incorrect:
value: anything
schema:
type: array
items: {}
items:
type: string
enum:
- az
- bukh
'404':
description: A wrong ref
content:
Expand All @@ -1087,9 +1113,13 @@ paths:
additionalProperties: false
components:
x-types:
Foo: any
Foo:
- az
- bukh
Bar:
array: any
array:
- az
- bukh
"
`;

Expand Down
98 changes: 53 additions & 45 deletions applications/__tests__/__snapshots__/e2e-lint.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -118,96 +118,104 @@ run \`redocly lint --generate-ignore-file\` to add all problems to the ignore fi
exports[`lint > inline refs in 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
[1] applications/outputs/x-openapi-with-refs.yaml:29:21 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value/NonExistingField
Example value must conform to the schema: must have required property 'AFieldWithRegularRef'.
Example value must conform to the schema: must NOT have additional properties \`NonExistingField\`.
21 | Incorrect:
22 | value:
23 | NonExistingField: wrong
| ^^^^^^^^^^^^^^^^^^^^^^^
24 | schema:
25 | type: object
27 | AFieldWithInlineRef: wrong
28 | AFieldWithRegularRef: wrong
29 | NonExistingField: wrong
| ^^^^^^^^^^^^^^^^
30 | schema:
31 | type: object
referenced from applications/outputs/x-openapi-with-refs.yaml:13:15 at #/paths/~1test/get/responses/200/content/application~1json
Error was generated by the no-invalid-media-type-examples rule.
[2] applications/outputs/x-openapi-with-refs.yaml:23:21 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value
[2] applications/outputs/x-openapi-with-refs.yaml:28:43 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value/AFieldWithRegularRef
Example value must conform to the schema: \`AFieldWithRegularRef\` property must be equal to one of the allowed values "az", "bukh".
Example value must conform to the schema: must have required property 'AFieldWithInlineRef'.
Did you mean:
- az
- bukh
21 | Incorrect:
22 | value:
23 | NonExistingField: wrong
| ^^^^^^^^^^^^^^^^^^^^^^^
24 | schema:
25 | type: object
26 | value:
27 | AFieldWithInlineRef: wrong
28 | AFieldWithRegularRef: wrong
| ^^^^^
29 | NonExistingField: wrong
30 | schema:
referenced from applications/outputs/x-openapi-with-refs.yaml:13:15 at #/paths/~1test/get/responses/200/content/application~1json
Error was generated by the no-invalid-media-type-examples rule.
[3] applications/outputs/x-openapi-with-refs.yaml:23:21 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value/NonExistingField
[3] applications/outputs/x-openapi-with-refs.yaml:27:42 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value/AFieldWithInlineRef
Example value must conform to the schema: must NOT have additional properties \`NonExistingField\`.
Example value must conform to the schema: \`AFieldWithInlineRef\` property must be equal to one of the allowed values "az", "bukh".
21 | Incorrect:
22 | value:
23 | NonExistingField: wrong
| ^^^^^^^^^^^^^^^^
24 | schema:
25 | type: object
Did you mean:
- az
- bukh
25 | Incorrect:
26 | value:
27 | AFieldWithInlineRef: wrong
| ^^^^^
28 | AFieldWithRegularRef: wrong
29 | NonExistingField: wrong
referenced from applications/outputs/x-openapi-with-refs.yaml:13:15 at #/paths/~1test/get/responses/200/content/application~1json
Error was generated by the no-invalid-media-type-examples rule.
[4] applications/outputs/x-openapi-with-refs.yaml:51:26 at #/paths/~1test/get/responses/202/content/application~1json/examples/Incorrect/value
[4] applications/outputs/x-openapi-with-refs.yaml:71:26 at #/paths/~1test/get/responses/202/content/application~1json/examples/Incorrect/value
Example value must conform to the schema: type must be number.
49 | value: 42
50 | Incorrect:
51 | value: false
69 | value: 42
70 | Incorrect:
71 | value: false
| ^^^^^
52 | schema:
53 | type: number
72 | schema:
73 | type: number
referenced from applications/outputs/x-openapi-with-refs.yaml:46:15 at #/paths/~1test/get/responses/202/content/application~1json
referenced from applications/outputs/x-openapi-with-refs.yaml:66:15 at #/paths/~1test/get/responses/202/content/application~1json
Error was generated by the no-invalid-media-type-examples rule.
[5] applications/outputs/x-openapi-with-refs.yaml:65:26 at #/paths/~1test/get/responses/203/content/application~1json/examples/Incorrect/value
[5] applications/outputs/x-openapi-with-refs.yaml:87:26 at #/paths/~1test/get/responses/203/content/application~1json/examples/Incorrect/value
Example value must conform to the schema: type must be array.
63 | - anything
64 | Incorrect:
65 | value: anything
85 | - az
86 | Incorrect:
87 | value: anything
| ^^^^^^^^
66 | schema:
67 | type: array
88 | schema:
89 | type: array
referenced from applications/outputs/x-openapi-with-refs.yaml:58:15 at #/paths/~1test/get/responses/203/content/application~1json
referenced from applications/outputs/x-openapi-with-refs.yaml:78:15 at #/paths/~1test/get/responses/203/content/application~1json
Error was generated by the no-invalid-media-type-examples rule.
[6] applications/outputs/x-openapi-with-refs.yaml:75:19 at #/paths/~1test/get/responses/404/content/application~1json/x-type/AWrongRef
[6] applications/outputs/x-openapi-with-refs.yaml:101: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'
73 | x-type:
74 | AWrongRef:
75 | $ref: wrong-file.yaml
| ^^^^^^^^^^^^^^^^^^^^^
76 | example:
77 | AWrongRef: Accepts anything
99 | x-type:
100 | AWrongRef:
101 | $ref: wrong-file.yaml
| ^^^^^^^^^^^^^^^^^^^^^
102 | example:
103 | AWrongRef: Accepts anything
Error was generated by the no-unresolved-refs rule.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ describe('resolver', () => {
expect(
resolveAndMerge(
{
az: {$writeonly: 'string', somethingElse: 'wrong, must be ignored'},
bukh: {$readonly: 'number'},
az: {$writeonly: 'request', somethingElse: 'wrong, must be ignored'},
bukh: {$readonly: 'response', $writeonly: 'request'},
},
{}
)
).toEqual({az: 'undefined', bukh: 'undefined'})
).toEqual({az: ['request', 'undefined'], bukh: ['request', 'response']})
})

test('$description in combination with $and', () => {
Expand Down
2 changes: 1 addition & 1 deletion applications/json-schema-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function translateJSONSchemaToXType(schema, ctx) {
isPlainObject(schema.additionalProperties) ||
isPlainObject(schema.items)
) {
return extractObjectLikeNode(schema)
return extractObjectLikeNode(schema, ctx)
}

if (schema.allOf) {
Expand Down
11 changes: 11 additions & 0 deletions applications/json-schema-examples/blog-post.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"title": "New Blog Post",
"content": "This is the content of the blog post...",
"publishedDate": "2023-08-25T15:00:00Z",
"author": {
"username": "authoruser",
"email": "[email protected]",
"age": "34"
},
"tags": ["Technology", "Programming"]
}
27 changes: 27 additions & 0 deletions applications/json-schema-examples/blog-post.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"description": "A representation of a blog post",
"type": "object",
"required": ["title", "content", "author"],
"properties": {
"title": {
"type": "string"
},
"content": {
"type": "string"
},
"publishedDate": {
"type": "string",
"format": "date-time"
},
"author": {
"$ref": "user-profile.schema.json"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
}

20 changes: 20 additions & 0 deletions applications/json-schema-examples/blog-post.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
description: A representation of a blog post
type: object
required:
- title
- content
- author
properties:
title:
type: string
content:
type: string
publishedDate:
type: string
format: date-time
author:
'$ref': user-profile.schema.json
tags:
type: array
items:
type: string
9 changes: 9 additions & 0 deletions applications/json-schema-examples/blog-post.x-type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title: string
content: string
publishedDate:
- string::date-time
- undefined
author: $ref:user-profile.x-type.yaml
tags:
- array: string
- undefined
9 changes: 9 additions & 0 deletions applications/json-schema-examples/circular.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Az Bukh",
"friends": [
{
"name": "Vidh Glagol",
"friends": [43]
}
]
}
13 changes: 13 additions & 0 deletions applications/json-schema-examples/circular.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
title: Person
type: object
properties:
name:
type: string
friends:
type: array
items:
$ref: '#'
required:
- name
- friends
additionalProperties: false
3 changes: 3 additions & 0 deletions applications/json-schema-examples/circular.x-type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: string
friends:
array: $ref:#
Loading

0 comments on commit 20950e7

Please sign in to comment.