@redocly/openapi-core 1.0.0-beta.96 → 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.
Files changed (106) hide show
  1. package/lib/bundle.d.ts +2 -0
  2. package/lib/bundle.js +6 -3
  3. package/lib/config/all.js +1 -0
  4. package/lib/config/config-resolvers.d.ts +1 -1
  5. package/lib/config/config-resolvers.js +31 -2
  6. package/lib/config/config.d.ts +4 -1
  7. package/lib/config/config.js +18 -12
  8. package/lib/format/format.js +2 -1
  9. package/lib/redocly/index.js +10 -26
  10. package/lib/redocly/registry-api-types.d.ts +1 -0
  11. package/lib/redocly/registry-api.d.ts +1 -1
  12. package/lib/redocly/registry-api.js +2 -1
  13. package/lib/ref-utils.js +1 -0
  14. package/lib/rules/common/response-contains-header.d.ts +2 -0
  15. package/lib/rules/common/response-contains-header.js +29 -0
  16. package/lib/rules/common/scalar-property-missing-example.d.ts +2 -0
  17. package/lib/rules/common/scalar-property-missing-example.js +41 -0
  18. package/lib/rules/oas2/index.d.ts +3 -0
  19. package/lib/rules/oas2/index.js +6 -0
  20. package/lib/rules/oas2/response-contains-property.d.ts +2 -0
  21. package/lib/rules/oas2/response-contains-property.js +38 -0
  22. package/lib/rules/oas3/index.js +6 -0
  23. package/lib/rules/oas3/response-contains-property.d.ts +2 -0
  24. package/lib/rules/oas3/response-contains-property.js +40 -0
  25. package/lib/types/oas2.js +3 -1
  26. package/lib/types/oas3.js +21 -8
  27. package/lib/types/oas3_1.js +12 -8
  28. package/lib/types/redocly-yaml.js +12 -0
  29. package/lib/typings/openapi.d.ts +5 -2
  30. package/lib/typings/swagger.d.ts +2 -0
  31. package/lib/utils.d.ts +1 -1
  32. package/lib/utils.js +5 -3
  33. package/package.json +3 -3
  34. package/{__tests__ → src/__tests__}/__snapshots__/bundle.test.ts.snap +26 -0
  35. package/{__tests__ → src/__tests__}/bundle.test.ts +30 -6
  36. package/{__tests__ → src/__tests__}/codeframes.test.ts +3 -3
  37. package/{__tests__ → src/__tests__}/fixtures/extension.js +0 -0
  38. package/{__tests__ → src/__tests__}/fixtures/refs/definitions.yaml +0 -0
  39. package/{__tests__ → src/__tests__}/fixtures/refs/examples.yaml +0 -0
  40. package/{__tests__ → src/__tests__}/fixtures/refs/external-request-body.yaml +0 -0
  41. package/{__tests__ → src/__tests__}/fixtures/refs/externalref.yaml +0 -0
  42. package/{__tests__ → src/__tests__}/fixtures/refs/hosted.yaml +0 -0
  43. package/{__tests__ → src/__tests__}/fixtures/refs/openapi-with-external-refs-conflicting-names.yaml +0 -0
  44. package/{__tests__ → src/__tests__}/fixtures/refs/openapi-with-external-refs.yaml +0 -0
  45. package/src/__tests__/fixtures/refs/openapi-with-url-refs.yaml +18 -0
  46. package/{__tests__ → src/__tests__}/fixtures/refs/param-b.yaml +0 -0
  47. package/{__tests__ → src/__tests__}/fixtures/refs/param-c.yaml +0 -0
  48. package/{__tests__ → src/__tests__}/fixtures/refs/rename.yaml +0 -0
  49. package/{__tests__ → src/__tests__}/fixtures/refs/requestBody.yaml +0 -0
  50. package/{__tests__ → src/__tests__}/fixtures/refs/schema-a.yaml +0 -0
  51. package/{__tests__ → src/__tests__}/fixtures/refs/simple.yaml +0 -0
  52. package/{__tests__ → src/__tests__}/fixtures/refs/vendor.schema.yaml +0 -0
  53. package/{__tests__ → src/__tests__}/fixtures/resolve/External.yaml +0 -0
  54. package/{__tests__ → src/__tests__}/fixtures/resolve/External2.yaml +0 -0
  55. package/{__tests__ → src/__tests__}/fixtures/resolve/description.md +0 -0
  56. package/{__tests__ → src/__tests__}/fixtures/resolve/externalInfo.yaml +0 -0
  57. package/{__tests__ → src/__tests__}/fixtures/resolve/externalLicense.yaml +0 -0
  58. package/{__tests__ → src/__tests__}/fixtures/resolve/openapi-with-back.yaml +0 -0
  59. package/{__tests__ → src/__tests__}/fixtures/resolve/openapi-with-md-description.yaml +0 -0
  60. package/{__tests__ → src/__tests__}/fixtures/resolve/openapi.yaml +0 -0
  61. package/{__tests__ → src/__tests__}/fixtures/resolve/schemas/type-a.yaml +0 -0
  62. package/{__tests__ → src/__tests__}/fixtures/resolve/schemas/type-b.yaml +0 -0
  63. package/{__tests__ → src/__tests__}/fixtures/resolve/transitive/a.yaml +0 -0
  64. package/{__tests__ → src/__tests__}/fixtures/resolve/transitive/components.yaml +0 -0
  65. package/{__tests__ → src/__tests__}/fixtures/resolve/transitive/schemas.yaml +0 -0
  66. package/src/__tests__/lint.test.ts +13 -0
  67. package/{__tests__ → src/__tests__}/login.test.ts +1 -1
  68. package/{__tests__ → src/__tests__}/normalizeVisitors.test.ts +4 -4
  69. package/{__tests__ → src/__tests__}/ref-utils.test.ts +5 -5
  70. package/{__tests__ → src/__tests__}/resolve-http.test.ts +4 -4
  71. package/{__tests__ → src/__tests__}/resolve.test.ts +4 -4
  72. package/src/__tests__/utils.test.ts +12 -1
  73. package/{__tests__ → src/__tests__}/walk.test.ts +5 -5
  74. package/src/bundle.ts +18 -3
  75. package/src/config/__tests__/__snapshots__/config-resolvers.test.ts.snap +18 -1
  76. package/src/config/__tests__/config-resolvers.test.ts +32 -1
  77. package/src/config/__tests__/fixtures/resolve-config/api/nested-config.yaml +5 -0
  78. package/src/config/__tests__/fixtures/resolve-config/local-config-with-file.yaml +7 -0
  79. package/src/config/all.ts +1 -0
  80. package/src/config/config-resolvers.ts +58 -3
  81. package/src/config/config.ts +21 -12
  82. package/src/format/format.ts +2 -1
  83. package/src/redocly/index.ts +12 -41
  84. package/src/redocly/registry-api-types.ts +1 -0
  85. package/src/redocly/registry-api.ts +2 -0
  86. package/src/ref-utils.ts +1 -0
  87. package/src/rules/common/__tests__/scalar-property-missing-example.test.ts +207 -0
  88. package/src/rules/common/response-contains-header.ts +30 -0
  89. package/src/rules/common/scalar-property-missing-example.ts +55 -0
  90. package/src/rules/oas2/__tests__/response-contains-header.test.ts +174 -0
  91. package/src/rules/oas2/__tests__/response-contains-property.test.ts +155 -0
  92. package/src/rules/oas2/index.ts +6 -0
  93. package/src/rules/oas2/response-contains-property.ts +36 -0
  94. package/src/rules/oas3/__tests__/response-contains-header.test.ts +273 -0
  95. package/src/rules/oas3/__tests__/response-contains-property.test.ts +403 -0
  96. package/src/rules/oas3/index.ts +6 -0
  97. package/src/rules/oas3/response-contains-property.ts +38 -0
  98. package/src/types/oas2.ts +2 -0
  99. package/src/types/oas3.ts +18 -7
  100. package/src/types/oas3_1.ts +11 -7
  101. package/src/types/redocly-yaml.ts +12 -0
  102. package/src/typings/openapi.ts +4 -1
  103. package/src/typings/swagger.ts +2 -0
  104. package/src/utils.ts +6 -2
  105. package/tsconfig.tsbuildinfo +1 -1
  106. package/__tests__/lint.test.ts +0 -17
@@ -0,0 +1,207 @@
1
+ import { outdent } from 'outdent';
2
+ import { lintDocument } from '../../../lint';
3
+ import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
4
+ import { BaseResolver } from '../../../resolve';
5
+
6
+ describe('Oas3 scalar-property-missing-example', () => {
7
+ it('should report on a scalar property missing example', async () => {
8
+ const document = parseYamlToDocument(
9
+ outdent`
10
+ openapi: 3.0.0
11
+ components:
12
+ schemas:
13
+ User:
14
+ type: object
15
+ properties:
16
+ email:
17
+ description: User email address
18
+ type: string
19
+ format: email
20
+ `,
21
+ 'foobar.yaml',
22
+ );
23
+
24
+ const results = await lintDocument({
25
+ externalRefResolver: new BaseResolver(),
26
+ document,
27
+ config: await makeConfig({ 'scalar-property-missing-example': 'error' }),
28
+ });
29
+
30
+ expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`
31
+ Array [
32
+ Object {
33
+ "location": Array [
34
+ Object {
35
+ "pointer": "#/components/schemas/User/properties/email",
36
+ "reportOnKey": true,
37
+ "source": "foobar.yaml",
38
+ },
39
+ ],
40
+ "message": "Scalar property should have \\"example\\" defined.",
41
+ "ruleId": "scalar-property-missing-example",
42
+ "severity": "error",
43
+ "suggest": Array [],
44
+ },
45
+ ]
46
+ `);
47
+ });
48
+ });
49
+
50
+ describe('Oas3.1 scalar-property-missing-example', () => {
51
+ it('should report on a scalar property missing example', async () => {
52
+ const document = parseYamlToDocument(
53
+ outdent`
54
+ openapi: 3.1.0
55
+ components:
56
+ schemas:
57
+ User:
58
+ type: object
59
+ properties:
60
+ email:
61
+ description: User email address
62
+ type: string
63
+ format: email
64
+ `,
65
+ 'foobar.yaml',
66
+ );
67
+
68
+ const results = await lintDocument({
69
+ externalRefResolver: new BaseResolver(),
70
+ document,
71
+ config: await makeConfig({ 'scalar-property-missing-example': 'error' }),
72
+ });
73
+
74
+ expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`
75
+ Array [
76
+ Object {
77
+ "location": Array [
78
+ Object {
79
+ "pointer": "#/components/schemas/User/properties/email",
80
+ "reportOnKey": true,
81
+ "source": "foobar.yaml",
82
+ },
83
+ ],
84
+ "message": "Scalar property should have \\"example\\" or \\"examples\\" defined.",
85
+ "ruleId": "scalar-property-missing-example",
86
+ "severity": "error",
87
+ "suggest": Array [],
88
+ },
89
+ ]
90
+ `);
91
+ });
92
+
93
+ it('should not report on a scalar property with an example', async () => {
94
+ const document = parseYamlToDocument(
95
+ outdent`
96
+ openapi: 3.1.0
97
+ components:
98
+ schemas:
99
+ User:
100
+ type: object
101
+ properties:
102
+ email:
103
+ description: User email address
104
+ type: string
105
+ format: email
106
+ example: john.smith@example.com
107
+ `,
108
+ 'foobar.yaml',
109
+ );
110
+
111
+ const results = await lintDocument({
112
+ externalRefResolver: new BaseResolver(),
113
+ document,
114
+ config: await makeConfig({ 'scalar-property-missing-example': 'error' }),
115
+ });
116
+
117
+ expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
118
+ });
119
+
120
+ it('should not report on a scalar property with an examples', async () => {
121
+ const document = parseYamlToDocument(
122
+ outdent`
123
+ openapi: 3.1.0
124
+ components:
125
+ schemas:
126
+ User:
127
+ type: object
128
+ properties:
129
+ email:
130
+ description: User email address
131
+ type: string
132
+ format: email
133
+ examples:
134
+ - john.smith@example.com
135
+ - other@example.com
136
+ `,
137
+ 'foobar.yaml',
138
+ );
139
+
140
+ const results = await lintDocument({
141
+ externalRefResolver: new BaseResolver(),
142
+ document,
143
+ config: await makeConfig({ 'scalar-property-missing-example': 'error' }),
144
+ });
145
+
146
+ expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
147
+ });
148
+
149
+ it('should not report on a non-scalar property missing an example', async () => {
150
+ const document = parseYamlToDocument(
151
+ outdent`
152
+ openapi: 3.1.0
153
+ components:
154
+ schemas:
155
+ Pet:
156
+ type: object
157
+ required:
158
+ - photoUrls
159
+ properties:
160
+ photoUrls:
161
+ description: The list of URL to a cute photos featuring pet
162
+ type: array
163
+ maxItems: 20
164
+ xml:
165
+ name: photoUrl
166
+ wrapped: true
167
+ items:
168
+ type: string
169
+ format: url
170
+ `,
171
+ 'foobar.yaml',
172
+ );
173
+
174
+ const results = await lintDocument({
175
+ externalRefResolver: new BaseResolver(),
176
+ document,
177
+ config: await makeConfig({ 'scalar-property-missing-example': 'error' }),
178
+ });
179
+
180
+ expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
181
+ });
182
+
183
+ it('should not report on a scalar property of binary format missing an example', async () => {
184
+ const document = parseYamlToDocument(
185
+ outdent`
186
+ openapi: 3.1.0
187
+ components:
188
+ schemas:
189
+ User:
190
+ type: object
191
+ properties:
192
+ responses:
193
+ type: string
194
+ format: binary
195
+ `,
196
+ 'foobar.yaml',
197
+ );
198
+
199
+ const results = await lintDocument({
200
+ externalRefResolver: new BaseResolver(),
201
+ document,
202
+ config: await makeConfig({ 'scalar-property-missing-example': 'error' }),
203
+ });
204
+
205
+ expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
206
+ });
207
+ });
@@ -0,0 +1,30 @@
1
+ import { Oas2Rule, Oas3Rule } from '../../visitors';
2
+ import { UserContext } from '../../walk';
3
+ import { Oas3Response } from '../../typings/openapi';
4
+ import { Oas2Response } from '../../typings/swagger';
5
+ import { getMatchingStatusCodeRange } from '../../utils';
6
+
7
+ export const ResponseContainsHeader: Oas3Rule | Oas2Rule = (options) => {
8
+ const names: Record<string, string[]> = options.names || {};
9
+ return {
10
+ Operation: {
11
+ Response: {
12
+ enter: (response: Oas2Response | Oas3Response, { report, location, key }: UserContext) => {
13
+ const expectedHeaders =
14
+ names[key] ||
15
+ names[getMatchingStatusCodeRange(key)] ||
16
+ names[getMatchingStatusCodeRange(key).toLowerCase()] ||
17
+ [];
18
+ for (const expectedHeader of expectedHeaders) {
19
+ if (!response.headers?.[expectedHeader]) {
20
+ report({
21
+ message: `Response object must contain a "${expectedHeader}" header.`,
22
+ location: location.child('headers').key(),
23
+ });
24
+ }
25
+ }
26
+ },
27
+ },
28
+ },
29
+ };
30
+ };
@@ -0,0 +1,55 @@
1
+ import type { Oas2Rule, Oas3Rule } from '../../visitors';
2
+ import type { UserContext } from '../../walk';
3
+ import type { Oas2Schema } from '../../typings/swagger';
4
+ import type { Oas3Schema, Oas3_1Schema } from '../../typings/openapi';
5
+ import { OasVersion } from '../../oas-types';
6
+
7
+ const SCALAR_TYPES = ['string', 'integer', 'number', 'boolean', 'null'];
8
+
9
+ export const ScalarPropertyMissingExample: Oas3Rule | Oas2Rule = () => {
10
+ return {
11
+ SchemaProperties(
12
+ properties: { [name: string]: Oas2Schema | Oas3Schema | Oas3_1Schema },
13
+ { report, location, oasVersion, resolve }: UserContext,
14
+ ) {
15
+ for (const propName of Object.keys(properties)) {
16
+ const propSchema = resolve(properties[propName]).node;
17
+
18
+ if (!propSchema || !isScalarSchema(propSchema)) {
19
+ continue;
20
+ }
21
+
22
+ if (!propSchema.example && !(propSchema as Oas3_1Schema).examples) {
23
+ report({
24
+ message: `Scalar property should have "example"${
25
+ oasVersion === OasVersion.Version3_1 ? ' or "examples"' : ''
26
+ } defined.`,
27
+ location: location.child(propName).key(),
28
+ });
29
+ }
30
+ }
31
+ },
32
+ };
33
+ };
34
+
35
+ function isScalarSchema(schema: Oas2Schema | Oas3Schema | Oas3_1Schema) {
36
+ if (!schema.type) {
37
+ return false;
38
+ }
39
+
40
+ if (schema.allOf || (schema as Oas3Schema).anyOf || (schema as Oas3Schema).oneOf) {
41
+ // Skip allOf/oneOf/anyOf as it's complicated to validate it right now.
42
+ // We need core support for checking contrstrains through those keywords.
43
+ return false;
44
+ }
45
+
46
+ if (schema.format === 'binary') {
47
+ return false;
48
+ }
49
+
50
+ if (Array.isArray(schema.type)) {
51
+ return schema.type.every((t) => SCALAR_TYPES.includes(t));
52
+ }
53
+
54
+ return SCALAR_TYPES.includes(schema.type);
55
+ }
@@ -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
+ });
@@ -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
+ };