Skip to content

Commit 36a078d

Browse files
CLOUDP-304939: IPA 106: Create : A Request object must include only input fields (consider resource collection URIs only)
1 parent cbc9fec commit 36a078d

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

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

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
7272
document: {
7373
components: componentSchemas,
7474
paths: {
75-
'/valid-resource': {
75+
'/resource': {
7676
post: {
7777
requestBody: {
7878
content: {
@@ -90,6 +90,9 @@ testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
9090
},
9191
},
9292
},
93+
'/resource/{id}': {
94+
get: {},
95+
},
9396
},
9497
},
9598
errors: [],
@@ -121,7 +124,7 @@ testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
121124
document: {
122125
components: componentSchemas,
123126
paths: {
124-
'/invalid-resource': {
127+
'/resource': {
125128
post: {
126129
requestBody: {
127130
content: {
@@ -140,21 +143,24 @@ testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
140143
},
141144
},
142145
},
146+
'/resource/{id}': {
147+
get: {},
148+
},
143149
},
144150
},
145151
errors: [
146152
{
147153
code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields',
148154
message:
149155
'The Create method request object must not include input fields (readOnly properties). Found readOnly property at: id. http://go/ipa/106',
150-
path: ['paths', '/invalid-resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
156+
path: ['paths', '/resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
151157
severity: DiagnosticSeverity.Warning,
152158
},
153159
{
154160
code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields',
155161
message:
156162
'The Create method request object must not include input fields (readOnly properties). Found readOnly property at one of the inline schemas. http://go/ipa/106',
157-
path: ['paths', '/invalid-resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'],
163+
path: ['paths', '/resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'],
158164
severity: DiagnosticSeverity.Warning,
159165
},
160166
],
@@ -164,7 +170,7 @@ testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
164170
document: {
165171
components: componentSchemas,
166172
paths: {
167-
'/nested-invalid-resource': {
173+
'/resource': {
168174
post: {
169175
requestBody: {
170176
content: {
@@ -177,21 +183,17 @@ testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
177183
},
178184
},
179185
},
186+
'/resource/{id}': {
187+
get: {},
188+
},
180189
},
181190
},
182191
errors: [
183192
{
184193
code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields',
185194
message:
186195
'The Create method request object must not include input fields (readOnly properties). Found readOnly property at: user.userId. http://go/ipa/106',
187-
path: [
188-
'paths',
189-
'/nested-invalid-resource',
190-
'post',
191-
'requestBody',
192-
'content',
193-
'application/vnd.atlas.2023-01-01+json',
194-
],
196+
path: ['paths', '/resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
195197
severity: DiagnosticSeverity.Warning,
196198
},
197199
],
@@ -201,7 +203,7 @@ testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
201203
document: {
202204
components: componentSchemas,
203205
paths: {
204-
'/array-invalid-resource': {
206+
'/resource': {
205207
post: {
206208
requestBody: {
207209
content: {
@@ -214,21 +216,17 @@ testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
214216
},
215217
},
216218
},
219+
'/resource/{id}': {
220+
get: {},
221+
},
217222
},
218223
},
219224
errors: [
220225
{
221226
code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields',
222227
message:
223228
'The Create method request object must not include input fields (readOnly properties). Found readOnly property at: items.items.itemId. http://go/ipa/106',
224-
path: [
225-
'paths',
226-
'/array-invalid-resource',
227-
'post',
228-
'requestBody',
229-
'content',
230-
'application/vnd.atlas.2023-01-01+json',
231-
],
229+
path: ['paths', '/resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
232230
severity: DiagnosticSeverity.Warning,
233231
},
234232
],
@@ -238,7 +236,7 @@ testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
238236
document: {
239237
components: componentSchemas,
240238
paths: {
241-
'/excepted-resource': {
239+
'/resource': {
242240
post: {
243241
requestBody: {
244242
content: {
@@ -254,6 +252,9 @@ testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [
254252
},
255253
},
256254
},
255+
'/resource/{id}': {
256+
get: {},
257+
},
257258
},
258259
},
259260
errors: [],

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
1+
import {
2+
getResourcePathItems,
3+
isCustomMethodIdentifier,
4+
isResourceCollectionIdentifier,
5+
isSingletonResource,
6+
} from './utils/resourceEvaluation.js';
27
import { resolveObject } from './utils/componentUtils.js';
38
import { hasException } from './utils/exceptions.js';
49
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
@@ -10,9 +15,12 @@ const ERROR_MESSAGE = 'The Create method request object must not include input f
1015
export default (input, _, { path, documentInventory }) => {
1116
const resourcePath = path[1];
1217
const oas = documentInventory.resolved;
18+
const resourcePaths = getResourcePathItems(resourcePath, oas.paths);
1319
let mediaType = input;
1420

15-
if (isCustomMethodIdentifier(resourcePath) || !mediaType.endsWith('json')) {
21+
const isResourceCollection = isResourceCollectionIdentifier(resourcePath) && !isSingletonResource(resourcePaths);
22+
23+
if (isCustomMethodIdentifier(resourcePath) || !isResourceCollection || !mediaType.endsWith('json')) {
1624
return;
1725
}
1826

0 commit comments

Comments
 (0)