Skip to content

Commit c91b7b9

Browse files
Fix: Create method checks should apply resource collection URIs
1 parent fe27b87 commit c91b7b9

File tree

5 files changed

+61
-21
lines changed

5 files changed

+61
-21
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
4949
content: {
5050
'application/vnd.atlas.2023-01-01+json': {
5151
schema: {
52-
$ref: '#/components/schemas/SchemaRequest',
52+
$ref: '#/components/schemas/Schema',
5353
},
5454
},
5555
'application/vnd.atlas.2024-01-01+json': {
5656
schema: {
57-
$ref: '#/components/schemas/SchemaRequest',
57+
$ref: '#/components/schemas/Schema',
5858
},
5959
},
6060
},
@@ -109,7 +109,10 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
109109
},
110110
},
111111
},
112-
'/resource2': {
112+
'/resource/{id}': {
113+
get: {},
114+
},
115+
'/resourceTwo': {
113116
post: {
114117
requestBody: {
115118
content: {
@@ -127,7 +130,10 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
127130
},
128131
},
129132
},
130-
'/resource3': {
133+
'/resourceTwo/{id}': {
134+
get: {},
135+
},
136+
'/resourceThree': {
131137
post: {
132138
requestBody: {
133139
content: {
@@ -140,6 +146,9 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
140146
},
141147
},
142148
},
149+
'/resourceThree/{id}': {
150+
get: {},
151+
},
143152
},
144153
},
145154
errors: [
@@ -158,19 +167,19 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
158167
{
159168
code: 'xgen-IPA-106-create-method-request-body-is-request-suffixed-object',
160169
message: 'The response body schema must reference a schema with a Request suffix. http://go/ipa/106',
161-
path: ['paths', '/resource2', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
170+
path: ['paths', '/resourceTwo', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
162171
severity: DiagnosticSeverity.Warning,
163172
},
164173
{
165174
code: 'xgen-IPA-106-create-method-request-body-is-request-suffixed-object',
166175
message: 'The response body schema must reference a schema with a Request suffix. http://go/ipa/106',
167-
path: ['paths', '/resource2', 'post', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'],
176+
path: ['paths', '/resourceTwo', 'post', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'],
168177
severity: DiagnosticSeverity.Warning,
169178
},
170179
{
171180
code: 'xgen-IPA-106-create-method-request-body-is-request-suffixed-object',
172181
message: 'The response body schema is defined inline and must reference a predefined schema. http://go/ipa/106',
173-
path: ['paths', '/resource3', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
182+
path: ['paths', '/resourceThree', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
174183
severity: DiagnosticSeverity.Warning,
175184
},
176185
],
@@ -207,7 +216,7 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
207216
},
208217
},
209218
},
210-
'/resource2': {
219+
'/resourceTwo': {
211220
post: {
212221
requestBody: {
213222
content: {
@@ -231,7 +240,7 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
231240
},
232241
},
233242
},
234-
'/resource3': {
243+
'/resourceThree': {
235244
post: {
236245
requestBody: {
237246
content: {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ testRule('xgen-IPA-106-create-method-should-not-have-query-parameters', [
7373
],
7474
},
7575
},
76-
'/resource2': {
76+
'/resourceTwo': {
7777
post: {
7878
parameters: [],
7979
},
@@ -98,7 +98,7 @@ testRule('xgen-IPA-106-create-method-should-not-have-query-parameters', [
9898
],
9999
},
100100
},
101-
'/resource2': {
101+
'/resourceTwo': {
102102
post: {
103103
parameters: [
104104
{
@@ -127,14 +127,14 @@ testRule('xgen-IPA-106-create-method-should-not-have-query-parameters', [
127127
{
128128
code: 'xgen-IPA-106-create-method-should-not-have-query-parameters',
129129
message: 'Input parameter [query-param]: Create operations should not have query parameters. http://go/ipa/106',
130-
path: ['paths', '/resource2', 'post'],
130+
path: ['paths', '/resourceTwo', 'post'],
131131
severity: DiagnosticSeverity.Warning,
132132
},
133133
{
134134
code: 'xgen-IPA-106-create-method-should-not-have-query-parameters',
135135
message:
136136
'Input parameter [query-param-2]: Create operations should not have query parameters. http://go/ipa/106',
137-
path: ['paths', '/resource2', 'post'],
137+
path: ['paths', '/resourceTwo', 'post'],
138138
severity: DiagnosticSeverity.Warning,
139139
},
140140
],
@@ -158,7 +158,7 @@ testRule('xgen-IPA-106-create-method-should-not-have-query-parameters', [
158158
},
159159
},
160160
},
161-
'/resource2': {
161+
'/resourceTwo': {
162162
post: {
163163
parameters: [
164164
{

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { getResponseOfGetMethodByMediaType, isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
1+
import {
2+
getResourcePathItems,
3+
getResponseOfGetMethodByMediaType,
4+
isCustomMethodIdentifier, isResourceCollectionIdentifier, isSingletonResource,
5+
} from './utils/resourceEvaluation.js';
26
import { resolveObject } from './utils/componentUtils.js';
37
import { isDeepEqual, omitDeep } from './utils/compareUtils.js';
48
import { hasException } from './utils/exceptions.js';
@@ -12,7 +16,14 @@ export default (input, opts, { path, documentInventory }) => {
1216
const oas = documentInventory.resolved;
1317
const resourcePath = path[1];
1418
let mediaType = input;
15-
if (isCustomMethodIdentifier(resourcePath) || !mediaType.endsWith('json')) {
19+
const resourcePaths = getResourcePathItems(resourcePath, oas.paths);
20+
21+
const isResourceCollection = isResourceCollectionIdentifier(resourcePath) && !isSingletonResource(resourcePaths);
22+
if (
23+
isCustomMethodIdentifier(resourcePath) ||
24+
!isResourceCollection ||
25+
!mediaType.endsWith('json')
26+
) {
1627
return;
1728
}
1829

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import {
55
collectException,
66
handleInternalError,
77
} from './utils/collectionUtils.js';
8-
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
8+
import {
9+
getResourcePathItems,
10+
isCustomMethodIdentifier,
11+
isResourceCollectionIdentifier,
12+
isSingletonResource,
13+
} from './utils/resourceEvaluation.js';
914
import { resolveObject } from './utils/componentUtils.js';
1015
import { getSchemaRef } from './utils/methodUtils.js';
1116

@@ -17,8 +22,15 @@ export default (input, _, { path, documentInventory }) => {
1722
const oas = documentInventory.unresolved;
1823
const resourcePath = path[1];
1924
const contentPerMediaType = resolveObject(oas, path);
25+
const resourcePaths = getResourcePathItems(resourcePath, oas.paths);
2026

21-
if (isCustomMethodIdentifier(resourcePath) || !input.endsWith('json') || !contentPerMediaType.schema) {
27+
const isResourceCollection = isResourceCollectionIdentifier(resourcePath) && !isSingletonResource(resourcePaths);
28+
if (
29+
isCustomMethodIdentifier(resourcePath) ||
30+
!isResourceCollection ||
31+
!input.endsWith('json') ||
32+
!contentPerMediaType.schema
33+
) {
2234
return;
2335
}
2436

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@ import {
55
collectException,
66
handleInternalError,
77
} from './utils/collectionUtils.js';
8-
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
8+
import {
9+
getResourcePathItems,
10+
isCustomMethodIdentifier,
11+
isResourceCollectionIdentifier,
12+
isSingletonResource,
13+
} from './utils/resourceEvaluation.js';
914

1015
const RULE_NAME = 'xgen-IPA-106-create-method-should-not-have-query-parameters';
1116
const ERROR_MESSAGE = 'Create operations should not have query parameters.';
1217

1318
const ignoredParameters = ['envelope', 'pretty'];
1419

15-
export default (input, _, { path }) => {
20+
export default (input, _, { path, documentInventory }) => {
1621
const resourcePath = path[1];
22+
const oas = documentInventory.resolved;
23+
const resourcePaths = getResourcePathItems(resourcePath, oas.paths);
1724

18-
if (isCustomMethodIdentifier(resourcePath)) {
25+
const isResourceCollection = isResourceCollectionIdentifier(resourcePath) && !isSingletonResource(resourcePaths);
26+
if (isCustomMethodIdentifier(resourcePath) || !isResourceCollection) {
1927
return;
2028
}
2129

0 commit comments

Comments
 (0)