Skip to content

Commit a32c152

Browse files
inline schemas
1 parent bc9e113 commit a32c152

File tree

4 files changed

+67
-34
lines changed

4 files changed

+67
-34
lines changed

tools/spectral/ipa/__tests__/createMethodRequestBodyIsRequestSuffixedObject.test.js

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
9393
},
9494
},
9595
},
96-
'/resource/{id}': {
96+
'/resource2': {
9797
post: {
9898
requestBody: {
9999
content: {
@@ -102,22 +102,22 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
102102
$ref: '#/components/schemas/Schema',
103103
},
104104
},
105+
'application/vnd.atlas.2024-01-01+json': {
106+
schema: {
107+
$ref: '#/components/schemas/Schema',
108+
},
109+
},
105110
},
106111
},
107112
},
108113
},
109-
'/resource2': {
114+
'/resource3': {
110115
post: {
111116
requestBody: {
112117
content: {
113118
'application/vnd.atlas.2023-01-01+json': {
114119
schema: {
115-
$ref: '#/components/schemas/Schema',
116-
},
117-
},
118-
'application/vnd.atlas.2024-01-01+json': {
119-
schema: {
120-
$ref: '#/components/schemas/Schema',
120+
type: "object",
121121
},
122122
},
123123
},
@@ -136,19 +136,19 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
136136
{
137137
code: 'xgen-IPA-106-create-method-request-body-is-request-suffixed-object',
138138
message: 'Response body for the Create method should refer to Request suffixed schema. http://go/ipa/106',
139-
path: ['paths', '/resource/{id}', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
139+
path: ['paths', '/resource2', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
140140
severity: DiagnosticSeverity.Warning,
141141
},
142142
{
143143
code: 'xgen-IPA-106-create-method-request-body-is-request-suffixed-object',
144144
message: 'Response body for the Create method should refer to Request suffixed schema. http://go/ipa/106',
145-
path: ['paths', '/resource2', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
145+
path: ['paths', '/resource2', 'post', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'],
146146
severity: DiagnosticSeverity.Warning,
147147
},
148148
{
149149
code: 'xgen-IPA-106-create-method-request-body-is-request-suffixed-object',
150150
message: 'Response body for the Create method should refer to Request suffixed schema. http://go/ipa/106',
151-
path: ['paths', '/resource2', 'post', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'],
151+
path: ['paths', '/resource3', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
152152
severity: DiagnosticSeverity.Warning,
153153
},
154154
],
@@ -166,9 +166,49 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
166166
schema: {
167167
$ref: '#/components/schemas/Schema',
168168
},
169+
'x-xgen-IPA-exception': {
170+
'xgen-IPA-106-create-method-request-body-is-request-suffixed-object': 'reason',
171+
},
172+
},
173+
},
174+
},
175+
},
176+
},
177+
'/resource/{id}': {
178+
post: {
179+
requestBody: {
180+
content: {
181+
'application/vnd.atlas.2023-01-01+json': {
182+
schema: {
183+
$ref: '#/components/schemas/Schema',
184+
},
185+
'x-xgen-IPA-exception': {
186+
'xgen-IPA-106-create-method-request-body-is-request-suffixed-object': 'reason',
187+
},
188+
},
189+
},
190+
},
191+
},
192+
},
193+
'/resource2': {
194+
post: {
195+
requestBody: {
196+
content: {
197+
'application/vnd.atlas.2023-01-01+json': {
198+
schema: {
199+
$ref: '#/components/schemas/Schema',
200+
},
201+
'x-xgen-IPA-exception': {
202+
'xgen-IPA-106-create-method-request-body-is-request-suffixed-object': 'reason',
203+
},
169204
},
170-
'x-xgen-IPA-exception': {
171-
'xgen-IPA-106-create-method-request-body-is-request-suffixed-object': 'reason',
205+
'application/vnd.atlas.2024-01-01+json': {
206+
schema: {
207+
$ref: '#/components/schemas/Schema',
208+
},
209+
'x-xgen-IPA-exception': {
210+
'xgen-IPA-106-create-method-request-body-is-request-suffixed-object': 'reason',
211+
},
172212
},
173213
},
174214
},

tools/spectral/ipa/rulesets/IPA-106.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ rules:
1111
severity: warn
1212
given: '$.paths[*].post.requestBody.content'
1313
then:
14+
field: '@key'
1415
function: 'createMethodRequestBodyIsRequestSuffixedObject'

tools/spectral/ipa/rulesets/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,3 @@ For rule definitions, see [IPA-123.yaml](https://github.com/mongodb/openapi/blob
6666
| Rule Name | Description | Severity |
6767
| ------------------------------------------------- | ------------------------------------------------------- | -------- |
6868
| xgen-IPA-123-enum-values-must-be-upper-snake-case | Enum values must be UPPER_SNAKE_CASE. http://go/ipa/123 | warn |
69-
70-

tools/spectral/ipa/rulesets/functions/createMethodRequestBodyIsRequestSuffixedObject.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,27 @@ const ERROR_MESSAGE = 'Response body for the Create method should refer to Reque
99
export default (input, _, { path, documentInventory }) => {
1010
const oas = documentInventory.unresolved;
1111
const resourcePath = path[1];
12+
1213
if (isCustomMethod(resourcePath)) {
1314
return;
1415
}
1516

16-
const content = resolveObject(oas, path);
17-
if (hasException(content, RULE_NAME)) {
18-
collectException(content, RULE_NAME, path);
17+
const contentPerMediaType = resolveObject(oas, path);
18+
19+
if (hasException(contentPerMediaType, RULE_NAME)) {
20+
collectException(contentPerMediaType, RULE_NAME, path);
1921
return;
2022
}
2123

22-
const errors = [];
23-
for (const mediaType in content) {
24-
const media = content[mediaType];
25-
if (media.schema) {
26-
const schema = media.schema;
27-
if (schema.$ref && !schema.$ref.endsWith('Request')) {
28-
errors.push({
29-
path: path.concat(mediaType),
30-
message: `${ERROR_MESSAGE}`,
31-
});
32-
}
24+
if (contentPerMediaType.schema) {
25+
const schema = contentPerMediaType.schema;
26+
if (schema.$ref && !schema.$ref.endsWith('Request')) {
27+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
28+
}
29+
if(!schema.$ref) {
30+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
3331
}
3432
}
3533

36-
if (errors.length === 0) {
37-
collectAdoption(path, RULE_NAME);
38-
} else {
39-
return collectAndReturnViolation(path, RULE_NAME, errors);
40-
}
34+
collectAdoption(path, RULE_NAME);
4135
};

0 commit comments

Comments
 (0)