Skip to content

Commit 20950e7

Browse files
committed
return both readonly and writeonly when mode is not specified; start working on separate x-type resolver and validator; refactor transforming inline refs
1 parent 22a0abb commit 20950e7

24 files changed

+530
-79
lines changed

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

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,21 +1014,35 @@ paths:
10141014
content:
10151015
application/json:
10161016
x-type:
1017-
AFieldWithRegularRef: any
1018-
AFieldWithInlineRef: any
1017+
AFieldWithRegularRef:
1018+
- az
1019+
- bukh
1020+
AFieldWithInlineRef:
1021+
- az
1022+
- bukh
10191023
examples:
10201024
Correct:
10211025
value:
1022-
AFieldWithInlineRef: something
1023-
AFieldWithRegularRef: anything
1026+
AFieldWithInlineRef: az
1027+
AFieldWithRegularRef: bukh
10241028
Incorrect:
10251029
value:
1030+
AFieldWithInlineRef: wrong
1031+
AFieldWithRegularRef: wrong
10261032
NonExistingField: wrong
10271033
schema:
10281034
type: object
10291035
properties:
1030-
AFieldWithRegularRef: {}
1031-
AFieldWithInlineRef: {}
1036+
AFieldWithRegularRef:
1037+
type: string
1038+
enum:
1039+
- az
1040+
- bukh
1041+
AFieldWithInlineRef:
1042+
type: string
1043+
enum:
1044+
- az
1045+
- bukh
10321046
required:
10331047
- AFieldWithRegularRef
10341048
- AFieldWithInlineRef
@@ -1037,11 +1051,17 @@ paths:
10371051
description: A bare ref
10381052
content:
10391053
application/json:
1040-
x-type: any
1054+
x-type:
1055+
- az
1056+
- bukh
10411057
examples:
10421058
Correct:
1043-
value: Anything
1044-
schema: {}
1059+
value: az
1060+
schema:
1061+
type: string
1062+
enum:
1063+
- az
1064+
- bukh
10451065
'202':
10461066
description: A file reference
10471067
content:
@@ -1059,16 +1079,22 @@ paths:
10591079
content:
10601080
application/json:
10611081
x-type:
1062-
array: any
1082+
array:
1083+
- az
1084+
- bukh
10631085
examples:
10641086
Correct:
10651087
value:
1066-
- anything
1088+
- az
10671089
Incorrect:
10681090
value: anything
10691091
schema:
10701092
type: array
1071-
items: {}
1093+
items:
1094+
type: string
1095+
enum:
1096+
- az
1097+
- bukh
10721098
'404':
10731099
description: A wrong ref
10741100
content:
@@ -1087,9 +1113,13 @@ paths:
10871113
additionalProperties: false
10881114
components:
10891115
x-types:
1090-
Foo: any
1116+
Foo:
1117+
- az
1118+
- bukh
10911119
Bar:
1092-
array: any
1120+
array:
1121+
- az
1122+
- bukh
10931123
"
10941124
`;
10951125

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

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -118,96 +118,104 @@ run \`redocly lint --generate-ignore-file\` to add all problems to the ignore fi
118118
119119
exports[`lint > inline refs in x-openapi-with-refs.yaml 1`] = `
120120
"validating applications/outputs/x-openapi-with-refs.yaml...
121-
[1] applications/outputs/x-openapi-with-refs.yaml:23:21 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value
121+
[1] applications/outputs/x-openapi-with-refs.yaml:29:21 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value/NonExistingField
122122
123-
Example value must conform to the schema: must have required property 'AFieldWithRegularRef'.
123+
Example value must conform to the schema: must NOT have additional properties \`NonExistingField\`.
124124
125-
21 | Incorrect:
126-
22 | value:
127-
23 | NonExistingField: wrong
128-
| ^^^^^^^^^^^^^^^^^^^^^^^
129-
24 | schema:
130-
25 | type: object
125+
27 | AFieldWithInlineRef: wrong
126+
28 | AFieldWithRegularRef: wrong
127+
29 | NonExistingField: wrong
128+
| ^^^^^^^^^^^^^^^^
129+
30 | schema:
130+
31 | type: object
131131
132132
referenced from applications/outputs/x-openapi-with-refs.yaml:13:15 at #/paths/~1test/get/responses/200/content/application~1json
133133
134134
Error was generated by the no-invalid-media-type-examples rule.
135135
136136
137-
[2] applications/outputs/x-openapi-with-refs.yaml:23:21 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value
137+
[2] applications/outputs/x-openapi-with-refs.yaml:28:43 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value/AFieldWithRegularRef
138+
139+
Example value must conform to the schema: \`AFieldWithRegularRef\` property must be equal to one of the allowed values "az", "bukh".
138140
139-
Example value must conform to the schema: must have required property 'AFieldWithInlineRef'.
141+
Did you mean:
142+
- az
143+
- bukh
140144
141-
21 | Incorrect:
142-
22 | value:
143-
23 | NonExistingField: wrong
144-
| ^^^^^^^^^^^^^^^^^^^^^^^
145-
24 | schema:
146-
25 | type: object
145+
26 | value:
146+
27 | AFieldWithInlineRef: wrong
147+
28 | AFieldWithRegularRef: wrong
148+
| ^^^^^
149+
29 | NonExistingField: wrong
150+
30 | schema:
147151
148152
referenced from applications/outputs/x-openapi-with-refs.yaml:13:15 at #/paths/~1test/get/responses/200/content/application~1json
149153
150154
Error was generated by the no-invalid-media-type-examples rule.
151155
152156
153-
[3] applications/outputs/x-openapi-with-refs.yaml:23:21 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value/NonExistingField
157+
[3] applications/outputs/x-openapi-with-refs.yaml:27:42 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value/AFieldWithInlineRef
154158
155-
Example value must conform to the schema: must NOT have additional properties \`NonExistingField\`.
159+
Example value must conform to the schema: \`AFieldWithInlineRef\` property must be equal to one of the allowed values "az", "bukh".
156160
157-
21 | Incorrect:
158-
22 | value:
159-
23 | NonExistingField: wrong
160-
| ^^^^^^^^^^^^^^^^
161-
24 | schema:
162-
25 | type: object
161+
Did you mean:
162+
- az
163+
- bukh
164+
165+
25 | Incorrect:
166+
26 | value:
167+
27 | AFieldWithInlineRef: wrong
168+
| ^^^^^
169+
28 | AFieldWithRegularRef: wrong
170+
29 | NonExistingField: wrong
163171
164172
referenced from applications/outputs/x-openapi-with-refs.yaml:13:15 at #/paths/~1test/get/responses/200/content/application~1json
165173
166174
Error was generated by the no-invalid-media-type-examples rule.
167175
168176
169-
[4] applications/outputs/x-openapi-with-refs.yaml:51:26 at #/paths/~1test/get/responses/202/content/application~1json/examples/Incorrect/value
177+
[4] applications/outputs/x-openapi-with-refs.yaml:71:26 at #/paths/~1test/get/responses/202/content/application~1json/examples/Incorrect/value
170178
171179
Example value must conform to the schema: type must be number.
172180
173-
49 | value: 42
174-
50 | Incorrect:
175-
51 | value: false
181+
69 | value: 42
182+
70 | Incorrect:
183+
71 | value: false
176184
| ^^^^^
177-
52 | schema:
178-
53 | type: number
185+
72 | schema:
186+
73 | type: number
179187
180-
referenced from applications/outputs/x-openapi-with-refs.yaml:46:15 at #/paths/~1test/get/responses/202/content/application~1json
188+
referenced from applications/outputs/x-openapi-with-refs.yaml:66:15 at #/paths/~1test/get/responses/202/content/application~1json
181189
182190
Error was generated by the no-invalid-media-type-examples rule.
183191
184192
185-
[5] applications/outputs/x-openapi-with-refs.yaml:65:26 at #/paths/~1test/get/responses/203/content/application~1json/examples/Incorrect/value
193+
[5] applications/outputs/x-openapi-with-refs.yaml:87:26 at #/paths/~1test/get/responses/203/content/application~1json/examples/Incorrect/value
186194
187195
Example value must conform to the schema: type must be array.
188196
189-
63 | - anything
190-
64 | Incorrect:
191-
65 | value: anything
197+
85 | - az
198+
86 | Incorrect:
199+
87 | value: anything
192200
| ^^^^^^^^
193-
66 | schema:
194-
67 | type: array
201+
88 | schema:
202+
89 | type: array
195203
196-
referenced from applications/outputs/x-openapi-with-refs.yaml:58:15 at #/paths/~1test/get/responses/203/content/application~1json
204+
referenced from applications/outputs/x-openapi-with-refs.yaml:78:15 at #/paths/~1test/get/responses/203/content/application~1json
197205
198206
Error was generated by the no-invalid-media-type-examples rule.
199207
200208
201-
[6] applications/outputs/x-openapi-with-refs.yaml:75:19 at #/paths/~1test/get/responses/404/content/application~1json/x-type/AWrongRef
209+
[6] applications/outputs/x-openapi-with-refs.yaml:101:19 at #/paths/~1test/get/responses/404/content/application~1json/x-type/AWrongRef
202210
203211
Can't resolve $ref: ENOENT: no such file or directory './applications/outputs/wrong-file.yaml'
204212
205-
73 | x-type:
206-
74 | AWrongRef:
207-
75 | $ref: wrong-file.yaml
208-
| ^^^^^^^^^^^^^^^^^^^^^
209-
76 | example:
210-
77 | AWrongRef: Accepts anything
213+
99 | x-type:
214+
100 | AWrongRef:
215+
101 | $ref: wrong-file.yaml
216+
| ^^^^^^^^^^^^^^^^^^^^^
217+
102 | example:
218+
103 | AWrongRef: Accepts anything
211219
212220
Error was generated by the no-unresolved-refs rule.
213221

applications/__tests__/resolver.test.js renamed to applications/__tests__/x-types-resolver.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ describe('resolver', () => {
218218
expect(
219219
resolveAndMerge(
220220
{
221-
az: {$writeonly: 'string', somethingElse: 'wrong, must be ignored'},
222-
bukh: {$readonly: 'number'},
221+
az: {$writeonly: 'request', somethingElse: 'wrong, must be ignored'},
222+
bukh: {$readonly: 'response', $writeonly: 'request'},
223223
},
224224
{}
225225
)
226-
).toEqual({az: 'undefined', bukh: 'undefined'})
226+
).toEqual({az: ['request', 'undefined'], bukh: ['request', 'response']})
227227
})
228228

229229
test('$description in combination with $and', () => {

applications/json-schema-adapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function translateJSONSchemaToXType(schema, ctx) {
8787
isPlainObject(schema.additionalProperties) ||
8888
isPlainObject(schema.items)
8989
) {
90-
return extractObjectLikeNode(schema)
90+
return extractObjectLikeNode(schema, ctx)
9191
}
9292

9393
if (schema.allOf) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"title": "New Blog Post",
3+
"content": "This is the content of the blog post...",
4+
"publishedDate": "2023-08-25T15:00:00Z",
5+
"author": {
6+
"username": "authoruser",
7+
"email": "[email protected]",
8+
"age": "34"
9+
},
10+
"tags": ["Technology", "Programming"]
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"description": "A representation of a blog post",
3+
"type": "object",
4+
"required": ["title", "content", "author"],
5+
"properties": {
6+
"title": {
7+
"type": "string"
8+
},
9+
"content": {
10+
"type": "string"
11+
},
12+
"publishedDate": {
13+
"type": "string",
14+
"format": "date-time"
15+
},
16+
"author": {
17+
"$ref": "user-profile.schema.json"
18+
},
19+
"tags": {
20+
"type": "array",
21+
"items": {
22+
"type": "string"
23+
}
24+
}
25+
}
26+
}
27+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
description: A representation of a blog post
2+
type: object
3+
required:
4+
- title
5+
- content
6+
- author
7+
properties:
8+
title:
9+
type: string
10+
content:
11+
type: string
12+
publishedDate:
13+
type: string
14+
format: date-time
15+
author:
16+
'$ref': user-profile.schema.json
17+
tags:
18+
type: array
19+
items:
20+
type: string
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: string
2+
content: string
3+
publishedDate:
4+
- string::date-time
5+
- undefined
6+
author: $ref:user-profile.x-type.yaml
7+
tags:
8+
- array: string
9+
- undefined
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Az Bukh",
3+
"friends": [
4+
{
5+
"name": "Vidh Glagol",
6+
"friends": [43]
7+
}
8+
]
9+
}

0 commit comments

Comments
 (0)