Skip to content

Commit 9a0d9bd

Browse files
IPA-106 Create: The request should be a Request suffixed object (fix for array schema) (#478)
1 parent 509707a commit 9a0d9bd

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
3131
$ref: '#/components/schemas/SchemaRequest',
3232
},
3333
},
34+
'application/vnd.atlas.2025-01-01+json': {
35+
schema: {
36+
type: 'array',
37+
items: {
38+
$ref: '#/components/schemas/SchemaRequest',
39+
},
40+
},
41+
},
3442
},
3543
},
3644
},
@@ -89,6 +97,14 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
8997
$ref: '#/components/schemas/Schema',
9098
},
9199
},
100+
'application/vnd.atlas.2024-01-01+json': {
101+
schema: {
102+
type: 'array',
103+
items: {
104+
$ref: '#/components/schemas/Schema',
105+
},
106+
},
107+
},
92108
},
93109
},
94110
},
@@ -133,6 +149,12 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
133149
path: ['paths', '/resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
134150
severity: DiagnosticSeverity.Warning,
135151
},
152+
{
153+
code: 'xgen-IPA-106-create-method-request-body-is-request-suffixed-object',
154+
message: 'The response body schema must reference a schema with a Request suffix. http://go/ipa/106',
155+
path: ['paths', '/resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'],
156+
severity: DiagnosticSeverity.Warning,
157+
},
136158
{
137159
code: 'xgen-IPA-106-create-method-request-body-is-request-suffixed-object',
138160
message: 'The response body schema must reference a schema with a Request suffix. http://go/ipa/106',
@@ -170,6 +192,17 @@ testRule('xgen-IPA-106-create-method-request-body-is-request-suffixed-object', [
170192
'xgen-IPA-106-create-method-request-body-is-request-suffixed-object': 'reason',
171193
},
172194
},
195+
'application/vnd.atlas.2024-01-01+json': {
196+
schema: {
197+
type: 'array',
198+
items: {
199+
$ref: '#/components/schemas/Schema',
200+
},
201+
},
202+
'x-xgen-IPA-exception': {
203+
'xgen-IPA-106-create-method-request-body-is-request-suffixed-object': 'reason',
204+
},
205+
},
173206
},
174207
},
175208
},

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,24 @@ export default (input, _, { path, documentInventory }) => {
2424

2525
if (contentPerMediaType.schema) {
2626
const schema = contentPerMediaType.schema;
27-
if (!schema.$ref) {
28-
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE_SCHEMA_REF);
27+
if (schema.type === 'array' && schema.items) {
28+
let schemaItems = schema.items;
29+
if (!schemaItems.$ref) {
30+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE_SCHEMA_REF);
31+
}
32+
if (!schemaItems.$ref.endsWith('Request')) {
33+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE_SCHEMA_NAME);
34+
}
35+
} else {
36+
if (!schema.$ref) {
37+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE_SCHEMA_REF);
38+
}
39+
40+
if (!schema.$ref.endsWith('Request')) {
41+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE_SCHEMA_NAME);
42+
}
2943
}
3044

31-
if (!schema.$ref.endsWith('Request')) {
32-
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE_SCHEMA_NAME);
33-
}
45+
collectAdoption(path, RULE_NAME);
3446
}
35-
36-
collectAdoption(path, RULE_NAME);
3747
};

0 commit comments

Comments
 (0)