@redocly/openapi-core 1.0.0-beta.98 → 1.0.0-beta.99
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/bundle.d.ts +2 -0
- package/lib/bundle.js +6 -3
- package/lib/config/all.js +1 -0
- package/lib/ref-utils.js +1 -0
- package/lib/rules/common/response-contains-header.d.ts +2 -0
- package/lib/rules/common/response-contains-header.js +29 -0
- package/lib/rules/common/scalar-property-missing-example.d.ts +2 -0
- package/lib/rules/common/scalar-property-missing-example.js +41 -0
- package/lib/rules/oas2/index.d.ts +3 -0
- package/lib/rules/oas2/index.js +6 -0
- package/lib/rules/oas2/response-contains-property.d.ts +2 -0
- package/lib/rules/oas2/response-contains-property.js +38 -0
- package/lib/rules/oas3/index.js +6 -0
- package/lib/rules/oas3/response-contains-property.d.ts +2 -0
- package/lib/rules/oas3/response-contains-property.js +40 -0
- package/lib/types/oas3.js +17 -6
- package/lib/types/oas3_1.js +9 -7
- package/lib/types/redocly-yaml.js +10 -0
- package/lib/typings/openapi.d.ts +3 -2
- package/lib/utils.d.ts +1 -0
- package/lib/utils.js +3 -1
- package/package.json +1 -1
- package/{__tests__ → src/__tests__}/__snapshots__/bundle.test.ts.snap +26 -0
- package/{__tests__ → src/__tests__}/bundle.test.ts +30 -6
- package/{__tests__ → src/__tests__}/codeframes.test.ts +3 -3
- package/{__tests__ → src/__tests__}/fixtures/extension.js +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/definitions.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/examples.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/external-request-body.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/externalref.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/hosted.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/openapi-with-external-refs-conflicting-names.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/openapi-with-external-refs.yaml +0 -0
- package/src/__tests__/fixtures/refs/openapi-with-url-refs.yaml +18 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/param-b.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/param-c.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/rename.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/requestBody.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/schema-a.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/simple.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/refs/vendor.schema.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/External.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/External2.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/description.md +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/externalInfo.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/externalLicense.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/openapi-with-back.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/openapi-with-md-description.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/openapi.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/schemas/type-a.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/schemas/type-b.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/transitive/a.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/transitive/components.yaml +0 -0
- package/{__tests__ → src/__tests__}/fixtures/resolve/transitive/schemas.yaml +0 -0
- package/src/__tests__/lint.test.ts +13 -0
- package/{__tests__ → src/__tests__}/login.test.ts +1 -1
- package/{__tests__ → src/__tests__}/normalizeVisitors.test.ts +4 -4
- package/{__tests__ → src/__tests__}/ref-utils.test.ts +5 -5
- package/{__tests__ → src/__tests__}/resolve-http.test.ts +4 -4
- package/{__tests__ → src/__tests__}/resolve.test.ts +4 -4
- package/src/__tests__/utils.test.ts +12 -1
- package/{__tests__ → src/__tests__}/walk.test.ts +5 -5
- package/src/bundle.ts +18 -3
- package/src/config/__tests__/config-resolvers.test.ts +1 -1
- package/src/config/all.ts +1 -0
- package/src/ref-utils.ts +1 -0
- package/src/rules/common/__tests__/scalar-property-missing-example.test.ts +207 -0
- package/src/rules/common/response-contains-header.ts +30 -0
- package/src/rules/common/scalar-property-missing-example.ts +55 -0
- package/src/rules/oas2/__tests__/response-contains-header.test.ts +174 -0
- package/src/rules/oas2/__tests__/response-contains-property.test.ts +155 -0
- package/src/rules/oas2/index.ts +6 -0
- package/src/rules/oas2/response-contains-property.ts +36 -0
- package/src/rules/oas3/__tests__/response-contains-header.test.ts +273 -0
- package/src/rules/oas3/__tests__/response-contains-property.test.ts +403 -0
- package/src/rules/oas3/index.ts +6 -0
- package/src/rules/oas3/response-contains-property.ts +38 -0
- package/src/types/oas3.ts +15 -6
- package/src/types/oas3_1.ts +9 -7
- package/src/types/redocly-yaml.ts +10 -0
- package/src/typings/openapi.ts +2 -1
- package/src/utils.ts +3 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/__tests__/lint.test.ts +0 -17
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { outdent } from 'outdent';
|
|
2
|
+
import { lintDocument } from '../../../lint';
|
|
3
|
+
import { parseYamlToDocument, makeConfig } from '../../../../__tests__/utils';
|
|
4
|
+
import { BaseResolver } from '../../../resolve';
|
|
5
|
+
|
|
6
|
+
describe('Oas2 response-contains-header', () => {
|
|
7
|
+
it('should report a response object not containing the header', async () => {
|
|
8
|
+
const document = parseYamlToDocument(outdent`
|
|
9
|
+
swagger: '2.0'
|
|
10
|
+
schemes:
|
|
11
|
+
- https
|
|
12
|
+
basePath: /v2
|
|
13
|
+
paths:
|
|
14
|
+
'/accounts/{accountId}':
|
|
15
|
+
get:
|
|
16
|
+
description: Retrieve a sub account under the master account.
|
|
17
|
+
operationId: account
|
|
18
|
+
responses:
|
|
19
|
+
'201':
|
|
20
|
+
description: Account Created
|
|
21
|
+
headers:
|
|
22
|
+
Content-Location:
|
|
23
|
+
description: Location of created Account
|
|
24
|
+
type: string
|
|
25
|
+
'404':
|
|
26
|
+
description: User not found
|
|
27
|
+
`);
|
|
28
|
+
const results = await lintDocument({
|
|
29
|
+
externalRefResolver: new BaseResolver(),
|
|
30
|
+
document,
|
|
31
|
+
config: await makeConfig({
|
|
32
|
+
'response-contains-header': {
|
|
33
|
+
severity: 'error',
|
|
34
|
+
names: { '2xx': ['Content-Length'], '4xx': ['Content-Length'] },
|
|
35
|
+
},
|
|
36
|
+
}),
|
|
37
|
+
});
|
|
38
|
+
expect(results).toMatchInlineSnapshot(`
|
|
39
|
+
Array [
|
|
40
|
+
Object {
|
|
41
|
+
"location": Array [
|
|
42
|
+
Object {
|
|
43
|
+
"pointer": "#/paths/~1accounts~1{accountId}/get/responses/201/headers",
|
|
44
|
+
"reportOnKey": true,
|
|
45
|
+
"source": Source {
|
|
46
|
+
"absoluteRef": "",
|
|
47
|
+
"body": "swagger: '2.0'
|
|
48
|
+
schemes:
|
|
49
|
+
- https
|
|
50
|
+
basePath: /v2
|
|
51
|
+
paths:
|
|
52
|
+
'/accounts/{accountId}':
|
|
53
|
+
get:
|
|
54
|
+
description: Retrieve a sub account under the master account.
|
|
55
|
+
operationId: account
|
|
56
|
+
responses:
|
|
57
|
+
'201':
|
|
58
|
+
description: Account Created
|
|
59
|
+
headers:
|
|
60
|
+
Content-Location:
|
|
61
|
+
description: Location of created Account
|
|
62
|
+
type: string
|
|
63
|
+
'404':
|
|
64
|
+
description: User not found",
|
|
65
|
+
"mimeType": undefined,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
"message": "Response object must contain a \\"Content-Length\\" header.",
|
|
70
|
+
"ruleId": "response-contains-header",
|
|
71
|
+
"severity": "error",
|
|
72
|
+
"suggest": Array [],
|
|
73
|
+
},
|
|
74
|
+
Object {
|
|
75
|
+
"location": Array [
|
|
76
|
+
Object {
|
|
77
|
+
"pointer": "#/paths/~1accounts~1{accountId}/get/responses/404/headers",
|
|
78
|
+
"reportOnKey": true,
|
|
79
|
+
"source": Source {
|
|
80
|
+
"absoluteRef": "",
|
|
81
|
+
"body": "swagger: '2.0'
|
|
82
|
+
schemes:
|
|
83
|
+
- https
|
|
84
|
+
basePath: /v2
|
|
85
|
+
paths:
|
|
86
|
+
'/accounts/{accountId}':
|
|
87
|
+
get:
|
|
88
|
+
description: Retrieve a sub account under the master account.
|
|
89
|
+
operationId: account
|
|
90
|
+
responses:
|
|
91
|
+
'201':
|
|
92
|
+
description: Account Created
|
|
93
|
+
headers:
|
|
94
|
+
Content-Location:
|
|
95
|
+
description: Location of created Account
|
|
96
|
+
type: string
|
|
97
|
+
'404':
|
|
98
|
+
description: User not found",
|
|
99
|
+
"mimeType": undefined,
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
"message": "Response object must contain a \\"Content-Length\\" header.",
|
|
104
|
+
"ruleId": "response-contains-header",
|
|
105
|
+
"severity": "error",
|
|
106
|
+
"suggest": Array [],
|
|
107
|
+
},
|
|
108
|
+
]
|
|
109
|
+
`);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should not report a response object containing the header nor not applicable', async () => {
|
|
113
|
+
const document = parseYamlToDocument(outdent`
|
|
114
|
+
swagger: '2.0'
|
|
115
|
+
schemes:
|
|
116
|
+
- https
|
|
117
|
+
basePath: /v2
|
|
118
|
+
paths:
|
|
119
|
+
'/accounts/{accountId}':
|
|
120
|
+
get:
|
|
121
|
+
description: Retrieve a sub account under the master account.
|
|
122
|
+
operationId: account
|
|
123
|
+
responses:
|
|
124
|
+
'201':
|
|
125
|
+
description: Account Created
|
|
126
|
+
headers:
|
|
127
|
+
Content-Length:
|
|
128
|
+
description: calls per hour allowed by the user
|
|
129
|
+
schema:
|
|
130
|
+
type: integer
|
|
131
|
+
format: int32
|
|
132
|
+
'404':
|
|
133
|
+
description: User not found
|
|
134
|
+
`);
|
|
135
|
+
const results = await lintDocument({
|
|
136
|
+
externalRefResolver: new BaseResolver(),
|
|
137
|
+
document,
|
|
138
|
+
config: await makeConfig({
|
|
139
|
+
'response-contains-header': {
|
|
140
|
+
severity: 'error',
|
|
141
|
+
names: { '2xx': ['Content-Length'], '400': ['Content-Length'] },
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
});
|
|
145
|
+
expect(results).toMatchInlineSnapshot(`Array []`);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should not report a response object when there is no `names` section defined', async () => {
|
|
149
|
+
const document = parseYamlToDocument(outdent`
|
|
150
|
+
swagger: '2.0'
|
|
151
|
+
schemes:
|
|
152
|
+
- https
|
|
153
|
+
basePath: /v2
|
|
154
|
+
paths:
|
|
155
|
+
'/accounts/{accountId}':
|
|
156
|
+
get:
|
|
157
|
+
description: Retrieve a sub account under the master account.
|
|
158
|
+
operationId: account
|
|
159
|
+
responses:
|
|
160
|
+
'404':
|
|
161
|
+
description: User not found
|
|
162
|
+
`);
|
|
163
|
+
const results = await lintDocument({
|
|
164
|
+
externalRefResolver: new BaseResolver(),
|
|
165
|
+
document,
|
|
166
|
+
config: await makeConfig({
|
|
167
|
+
'response-contains-header': {
|
|
168
|
+
severity: 'error',
|
|
169
|
+
},
|
|
170
|
+
}),
|
|
171
|
+
});
|
|
172
|
+
expect(results).toMatchInlineSnapshot(`Array []`);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { outdent } from 'outdent';
|
|
2
|
+
import { lintDocument } from '../../../lint';
|
|
3
|
+
import { parseYamlToDocument, makeConfig } from '../../../../__tests__/utils';
|
|
4
|
+
import { BaseResolver } from '../../../resolve';
|
|
5
|
+
|
|
6
|
+
describe('Oas2 response-contains-property', () => {
|
|
7
|
+
it('should report a response object not containing the property', async () => {
|
|
8
|
+
const document = parseYamlToDocument(outdent`
|
|
9
|
+
swagger: '2.0'
|
|
10
|
+
schemes:
|
|
11
|
+
- https
|
|
12
|
+
basePath: /v2
|
|
13
|
+
paths:
|
|
14
|
+
'/accounts/{accountId}':
|
|
15
|
+
get:
|
|
16
|
+
description: Retrieve a sub account under the master account.
|
|
17
|
+
operationId: account
|
|
18
|
+
responses:
|
|
19
|
+
'200':
|
|
20
|
+
description: Account object returned
|
|
21
|
+
schema:
|
|
22
|
+
type: object
|
|
23
|
+
properties:
|
|
24
|
+
created_at:
|
|
25
|
+
description: Account creation date/time
|
|
26
|
+
format: date-time
|
|
27
|
+
type: string
|
|
28
|
+
owner_email:
|
|
29
|
+
description: Account Owner email
|
|
30
|
+
type: string
|
|
31
|
+
'404':
|
|
32
|
+
description: User not found
|
|
33
|
+
`);
|
|
34
|
+
const results = await lintDocument({
|
|
35
|
+
externalRefResolver: new BaseResolver(),
|
|
36
|
+
document,
|
|
37
|
+
config: await makeConfig({
|
|
38
|
+
'response-contains-property': {
|
|
39
|
+
severity: 'error',
|
|
40
|
+
names: { '2xx': ['id'], '4xx': ['id'] },
|
|
41
|
+
},
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
44
|
+
expect(results).toMatchInlineSnapshot(`
|
|
45
|
+
Array [
|
|
46
|
+
Object {
|
|
47
|
+
"location": Array [
|
|
48
|
+
Object {
|
|
49
|
+
"pointer": "#/paths/~1accounts~1{accountId}/get/responses/200/schema/properties",
|
|
50
|
+
"reportOnKey": true,
|
|
51
|
+
"source": Source {
|
|
52
|
+
"absoluteRef": "",
|
|
53
|
+
"body": "swagger: '2.0'
|
|
54
|
+
schemes:
|
|
55
|
+
- https
|
|
56
|
+
basePath: /v2
|
|
57
|
+
paths:
|
|
58
|
+
'/accounts/{accountId}':
|
|
59
|
+
get:
|
|
60
|
+
description: Retrieve a sub account under the master account.
|
|
61
|
+
operationId: account
|
|
62
|
+
responses:
|
|
63
|
+
'200':
|
|
64
|
+
description: Account object returned
|
|
65
|
+
schema:
|
|
66
|
+
type: object
|
|
67
|
+
properties:
|
|
68
|
+
created_at:
|
|
69
|
+
description: Account creation date/time
|
|
70
|
+
format: date-time
|
|
71
|
+
type: string
|
|
72
|
+
owner_email:
|
|
73
|
+
description: Account Owner email
|
|
74
|
+
type: string
|
|
75
|
+
'404':
|
|
76
|
+
description: User not found",
|
|
77
|
+
"mimeType": undefined,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
"message": "Response object must contain a top-level \\"id\\" property.",
|
|
82
|
+
"ruleId": "response-contains-property",
|
|
83
|
+
"severity": "error",
|
|
84
|
+
"suggest": Array [],
|
|
85
|
+
},
|
|
86
|
+
]
|
|
87
|
+
`);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should not report a response object containing the expected property', async () => {
|
|
91
|
+
const document = parseYamlToDocument(outdent`
|
|
92
|
+
swagger: '2.0'
|
|
93
|
+
schemes:
|
|
94
|
+
- https
|
|
95
|
+
basePath: /v2
|
|
96
|
+
paths:
|
|
97
|
+
'/accounts/{accountId}':
|
|
98
|
+
get:
|
|
99
|
+
description: Retrieve a sub account under the master account.
|
|
100
|
+
operationId: account
|
|
101
|
+
responses:
|
|
102
|
+
'200':
|
|
103
|
+
description: Account object returned
|
|
104
|
+
schema:
|
|
105
|
+
type: object
|
|
106
|
+
properties:
|
|
107
|
+
created_at:
|
|
108
|
+
description: Account creation date/time
|
|
109
|
+
format: date-time
|
|
110
|
+
type: string
|
|
111
|
+
id: some-id
|
|
112
|
+
'404':
|
|
113
|
+
description: User not found
|
|
114
|
+
id: some-id
|
|
115
|
+
`);
|
|
116
|
+
const results = await lintDocument({
|
|
117
|
+
externalRefResolver: new BaseResolver(),
|
|
118
|
+
document,
|
|
119
|
+
config: await makeConfig({
|
|
120
|
+
'response-contains-property': {
|
|
121
|
+
severity: 'error',
|
|
122
|
+
names: { '200': ['id'], '4xx': ['id'] },
|
|
123
|
+
},
|
|
124
|
+
}),
|
|
125
|
+
});
|
|
126
|
+
expect(results).toMatchInlineSnapshot(`Array []`);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should not report a response object when there is no `names` section defined', async () => {
|
|
130
|
+
const document = parseYamlToDocument(outdent`
|
|
131
|
+
swagger: '2.0'
|
|
132
|
+
schemes:
|
|
133
|
+
- https
|
|
134
|
+
basePath: /v2
|
|
135
|
+
paths:
|
|
136
|
+
'/accounts/{accountId}':
|
|
137
|
+
get:
|
|
138
|
+
description: Retrieve a sub account under the master account.
|
|
139
|
+
operationId: account
|
|
140
|
+
responses:
|
|
141
|
+
'404':
|
|
142
|
+
description: User not found
|
|
143
|
+
`);
|
|
144
|
+
const results = await lintDocument({
|
|
145
|
+
externalRefResolver: new BaseResolver(),
|
|
146
|
+
document,
|
|
147
|
+
config: await makeConfig({
|
|
148
|
+
'response-contains-property': {
|
|
149
|
+
severity: 'error',
|
|
150
|
+
},
|
|
151
|
+
}),
|
|
152
|
+
});
|
|
153
|
+
expect(results).toMatchInlineSnapshot(`Array []`);
|
|
154
|
+
});
|
|
155
|
+
});
|
package/src/rules/oas2/index.ts
CHANGED
|
@@ -37,6 +37,9 @@ import { PathExcludesPatterns } from '../common/path-excludes-patterns';
|
|
|
37
37
|
import { RequestMimeType } from './request-mime-type';
|
|
38
38
|
import { ResponseMimeType } from './response-mime-type';
|
|
39
39
|
import { PathSegmentPlural } from '../common/path-segment-plural';
|
|
40
|
+
import { ResponseContainsHeader } from '../common/response-contains-header';
|
|
41
|
+
import { ResponseContainsProperty } from './response-contains-property';
|
|
42
|
+
import { ScalarPropertyMissingExample } from '../common/scalar-property-missing-example';
|
|
40
43
|
|
|
41
44
|
export const rules = {
|
|
42
45
|
spec: OasSpec as Oas2Rule,
|
|
@@ -78,6 +81,9 @@ export const rules = {
|
|
|
78
81
|
'request-mime-type': RequestMimeType as Oas2Rule,
|
|
79
82
|
'response-mime-type': ResponseMimeType as Oas2Rule,
|
|
80
83
|
'path-segment-plural': PathSegmentPlural as Oas2Rule,
|
|
84
|
+
'response-contains-header': ResponseContainsHeader as Oas2Rule,
|
|
85
|
+
'response-contains-property': ResponseContainsProperty as Oas2Rule,
|
|
86
|
+
'scalar-property-missing-example': ScalarPropertyMissingExample,
|
|
81
87
|
};
|
|
82
88
|
|
|
83
89
|
export const preprocessors = {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Oas2Rule } from '../../visitors';
|
|
2
|
+
import { UserContext } from '../../walk';
|
|
3
|
+
import { getMatchingStatusCodeRange } from '../../utils';
|
|
4
|
+
|
|
5
|
+
export const ResponseContainsProperty: Oas2Rule = (options) => {
|
|
6
|
+
const names: Record<string, string[]> = options.names || {};
|
|
7
|
+
let key: string | number;
|
|
8
|
+
return {
|
|
9
|
+
Operation: {
|
|
10
|
+
Response: {
|
|
11
|
+
skip: (_response, key) => {
|
|
12
|
+
return `${key}` === '204';
|
|
13
|
+
},
|
|
14
|
+
enter: (_response, ctx: UserContext) => {
|
|
15
|
+
key = ctx.key;
|
|
16
|
+
},
|
|
17
|
+
Schema(schema, { report, location }) {
|
|
18
|
+
if (schema.type !== 'object') return;
|
|
19
|
+
const expectedProperties =
|
|
20
|
+
names[key] ||
|
|
21
|
+
names[getMatchingStatusCodeRange(key)] ||
|
|
22
|
+
names[getMatchingStatusCodeRange(key).toLowerCase()] ||
|
|
23
|
+
[];
|
|
24
|
+
for (const expectedProperty of expectedProperties) {
|
|
25
|
+
if (!schema.properties?.[expectedProperty]) {
|
|
26
|
+
report({
|
|
27
|
+
message: `Response object must contain a top-level "${expectedProperty}" property.`,
|
|
28
|
+
location: location.child('properties').key(),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
};
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { outdent } from 'outdent';
|
|
2
|
+
import { lintDocument } from '../../../lint';
|
|
3
|
+
import { parseYamlToDocument, makeConfig } from '../../../../__tests__/utils';
|
|
4
|
+
import { BaseResolver } from '../../../resolve';
|
|
5
|
+
|
|
6
|
+
describe('Oas3 response-contains-header', () => {
|
|
7
|
+
it('should report a response object not containing the header', async () => {
|
|
8
|
+
const document = parseYamlToDocument(outdent`
|
|
9
|
+
openapi: 3.0.3
|
|
10
|
+
info:
|
|
11
|
+
version: 3.0.0
|
|
12
|
+
paths:
|
|
13
|
+
/store/subscribe:
|
|
14
|
+
post:
|
|
15
|
+
responses:
|
|
16
|
+
'200':
|
|
17
|
+
description: successful operation
|
|
18
|
+
headers:
|
|
19
|
+
X-Rate-Limit:
|
|
20
|
+
description: calls per hour allowed by the user
|
|
21
|
+
schema:
|
|
22
|
+
type: integer
|
|
23
|
+
format: int32
|
|
24
|
+
`);
|
|
25
|
+
|
|
26
|
+
const results = await lintDocument({
|
|
27
|
+
externalRefResolver: new BaseResolver(),
|
|
28
|
+
document,
|
|
29
|
+
config: await makeConfig({
|
|
30
|
+
'response-contains-header': {
|
|
31
|
+
severity: 'error',
|
|
32
|
+
names: { '200': ['Content-Length'] },
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
expect(results).toMatchInlineSnapshot(`
|
|
37
|
+
Array [
|
|
38
|
+
Object {
|
|
39
|
+
"location": Array [
|
|
40
|
+
Object {
|
|
41
|
+
"pointer": "#/paths/~1store~1subscribe/post/responses/200/headers",
|
|
42
|
+
"reportOnKey": true,
|
|
43
|
+
"source": Source {
|
|
44
|
+
"absoluteRef": "",
|
|
45
|
+
"body": "openapi: 3.0.3
|
|
46
|
+
info:
|
|
47
|
+
version: 3.0.0
|
|
48
|
+
paths:
|
|
49
|
+
/store/subscribe:
|
|
50
|
+
post:
|
|
51
|
+
responses:
|
|
52
|
+
'200':
|
|
53
|
+
description: successful operation
|
|
54
|
+
headers:
|
|
55
|
+
X-Rate-Limit:
|
|
56
|
+
description: calls per hour allowed by the user
|
|
57
|
+
schema:
|
|
58
|
+
type: integer
|
|
59
|
+
format: int32",
|
|
60
|
+
"mimeType": undefined,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
"message": "Response object must contain a \\"Content-Length\\" header.",
|
|
65
|
+
"ruleId": "response-contains-header",
|
|
66
|
+
"severity": "error",
|
|
67
|
+
"suggest": Array [],
|
|
68
|
+
},
|
|
69
|
+
]
|
|
70
|
+
`);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should report response objects not containing headers for a subset of status codes', async () => {
|
|
74
|
+
const document = parseYamlToDocument(outdent`
|
|
75
|
+
openapi: 3.0.3
|
|
76
|
+
info:
|
|
77
|
+
version: 3.0.0
|
|
78
|
+
paths:
|
|
79
|
+
/store/subscribe:
|
|
80
|
+
post:
|
|
81
|
+
responses:
|
|
82
|
+
'200':
|
|
83
|
+
description: successful operation
|
|
84
|
+
headers:
|
|
85
|
+
X-Rate-Limit:
|
|
86
|
+
description: calls per hour allowed by the user
|
|
87
|
+
schema:
|
|
88
|
+
type: integer
|
|
89
|
+
format: int32
|
|
90
|
+
400:
|
|
91
|
+
description: error
|
|
92
|
+
headers:
|
|
93
|
+
AccessForbidden:
|
|
94
|
+
description: Access forbidden
|
|
95
|
+
content:
|
|
96
|
+
application/json:
|
|
97
|
+
schema:
|
|
98
|
+
type: object
|
|
99
|
+
properties:
|
|
100
|
+
status:
|
|
101
|
+
type: integer
|
|
102
|
+
description: The HTTP status code.
|
|
103
|
+
error:
|
|
104
|
+
type: string
|
|
105
|
+
`);
|
|
106
|
+
const results = await lintDocument({
|
|
107
|
+
externalRefResolver: new BaseResolver(),
|
|
108
|
+
document,
|
|
109
|
+
config: await makeConfig({
|
|
110
|
+
'response-contains-header': {
|
|
111
|
+
severity: 'error',
|
|
112
|
+
names: {
|
|
113
|
+
'2XX': ['x-request-id'],
|
|
114
|
+
'400': ['Content-Length'],
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
}),
|
|
118
|
+
});
|
|
119
|
+
expect(results).toMatchInlineSnapshot(`
|
|
120
|
+
Array [
|
|
121
|
+
Object {
|
|
122
|
+
"location": Array [
|
|
123
|
+
Object {
|
|
124
|
+
"pointer": "#/paths/~1store~1subscribe/post/responses/200/headers",
|
|
125
|
+
"reportOnKey": true,
|
|
126
|
+
"source": Source {
|
|
127
|
+
"absoluteRef": "",
|
|
128
|
+
"body": "openapi: 3.0.3
|
|
129
|
+
info:
|
|
130
|
+
version: 3.0.0
|
|
131
|
+
paths:
|
|
132
|
+
/store/subscribe:
|
|
133
|
+
post:
|
|
134
|
+
responses:
|
|
135
|
+
'200':
|
|
136
|
+
description: successful operation
|
|
137
|
+
headers:
|
|
138
|
+
X-Rate-Limit:
|
|
139
|
+
description: calls per hour allowed by the user
|
|
140
|
+
schema:
|
|
141
|
+
type: integer
|
|
142
|
+
format: int32
|
|
143
|
+
400:
|
|
144
|
+
description: error
|
|
145
|
+
headers:
|
|
146
|
+
AccessForbidden:
|
|
147
|
+
description: Access forbidden
|
|
148
|
+
content:
|
|
149
|
+
application/json:
|
|
150
|
+
schema:
|
|
151
|
+
type: object
|
|
152
|
+
properties:
|
|
153
|
+
status:
|
|
154
|
+
type: integer
|
|
155
|
+
description: The HTTP status code.
|
|
156
|
+
error:
|
|
157
|
+
type: string",
|
|
158
|
+
"mimeType": undefined,
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
"message": "Response object must contain a \\"x-request-id\\" header.",
|
|
163
|
+
"ruleId": "response-contains-header",
|
|
164
|
+
"severity": "error",
|
|
165
|
+
"suggest": Array [],
|
|
166
|
+
},
|
|
167
|
+
Object {
|
|
168
|
+
"location": Array [
|
|
169
|
+
Object {
|
|
170
|
+
"pointer": "#/paths/~1store~1subscribe/post/responses/400/headers",
|
|
171
|
+
"reportOnKey": true,
|
|
172
|
+
"source": Source {
|
|
173
|
+
"absoluteRef": "",
|
|
174
|
+
"body": "openapi: 3.0.3
|
|
175
|
+
info:
|
|
176
|
+
version: 3.0.0
|
|
177
|
+
paths:
|
|
178
|
+
/store/subscribe:
|
|
179
|
+
post:
|
|
180
|
+
responses:
|
|
181
|
+
'200':
|
|
182
|
+
description: successful operation
|
|
183
|
+
headers:
|
|
184
|
+
X-Rate-Limit:
|
|
185
|
+
description: calls per hour allowed by the user
|
|
186
|
+
schema:
|
|
187
|
+
type: integer
|
|
188
|
+
format: int32
|
|
189
|
+
400:
|
|
190
|
+
description: error
|
|
191
|
+
headers:
|
|
192
|
+
AccessForbidden:
|
|
193
|
+
description: Access forbidden
|
|
194
|
+
content:
|
|
195
|
+
application/json:
|
|
196
|
+
schema:
|
|
197
|
+
type: object
|
|
198
|
+
properties:
|
|
199
|
+
status:
|
|
200
|
+
type: integer
|
|
201
|
+
description: The HTTP status code.
|
|
202
|
+
error:
|
|
203
|
+
type: string",
|
|
204
|
+
"mimeType": undefined,
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
"message": "Response object must contain a \\"Content-Length\\" header.",
|
|
209
|
+
"ruleId": "response-contains-header",
|
|
210
|
+
"severity": "error",
|
|
211
|
+
"suggest": Array [],
|
|
212
|
+
},
|
|
213
|
+
]
|
|
214
|
+
`);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should not report response objects containing specified headers', async () => {
|
|
218
|
+
const document = parseYamlToDocument(outdent`
|
|
219
|
+
openapi: 3.0.3
|
|
220
|
+
info:
|
|
221
|
+
version: 3.0.0
|
|
222
|
+
paths:
|
|
223
|
+
/store/subscribe:
|
|
224
|
+
post:
|
|
225
|
+
responses:
|
|
226
|
+
'200':
|
|
227
|
+
description: successful operation
|
|
228
|
+
headers:
|
|
229
|
+
X-Rate-Limit:
|
|
230
|
+
description: calls per hour allowed by the user
|
|
231
|
+
schema:
|
|
232
|
+
type: integer
|
|
233
|
+
format: int32
|
|
234
|
+
x-request-id:
|
|
235
|
+
description: Request ID
|
|
236
|
+
schema:
|
|
237
|
+
type: string
|
|
238
|
+
400:
|
|
239
|
+
description: error
|
|
240
|
+
headers:
|
|
241
|
+
AccessForbidden:
|
|
242
|
+
description: Access forbidden
|
|
243
|
+
content:
|
|
244
|
+
application/json:
|
|
245
|
+
schema:
|
|
246
|
+
type: object
|
|
247
|
+
properties:
|
|
248
|
+
status:
|
|
249
|
+
type: integer
|
|
250
|
+
description: The HTTP status code.
|
|
251
|
+
error:
|
|
252
|
+
type: string
|
|
253
|
+
Content-Length:
|
|
254
|
+
description: The number of bytes in the file
|
|
255
|
+
schema:
|
|
256
|
+
type: integer
|
|
257
|
+
`);
|
|
258
|
+
const results = await lintDocument({
|
|
259
|
+
externalRefResolver: new BaseResolver(),
|
|
260
|
+
document,
|
|
261
|
+
config: await makeConfig({
|
|
262
|
+
'response-contains-header': {
|
|
263
|
+
severity: 'error',
|
|
264
|
+
names: {
|
|
265
|
+
'2xx': ['x-request-id'],
|
|
266
|
+
'400': ['Content-Length'],
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
}),
|
|
270
|
+
});
|
|
271
|
+
expect(results).toMatchInlineSnapshot(`Array []`);
|
|
272
|
+
});
|
|
273
|
+
});
|