Skip to content

Commit 246df4d

Browse files
authored
CLOUDP-304929: xgen-IPA-102-collection-identifier-camelCase (#499)
1 parent b163e0f commit 246df4d

File tree

5 files changed

+413
-4
lines changed

5 files changed

+413
-4
lines changed
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-102-collection-identifier-camelCase', [
5+
{
6+
name: 'valid camelCase identifiers',
7+
document: {
8+
paths: {
9+
'/api/v2/atlas/test': {},
10+
'/users': {},
11+
'/resourceGroups': {},
12+
'/userProfiles': {},
13+
'/api/v1/test': {},
14+
},
15+
},
16+
errors: [],
17+
},
18+
{
19+
name: 'valid camelCase with path parameters',
20+
document: {
21+
paths: {
22+
'/resourceGroups/{groupId}': {},
23+
'/users/{userId}/userProfiles': {},
24+
},
25+
},
26+
errors: [],
27+
},
28+
{
29+
name: 'valid paths with custom methods (only checking identifier part)',
30+
document: {
31+
paths: {
32+
'/resources:any_Custom_Method': {},
33+
},
34+
},
35+
errors: [],
36+
},
37+
{
38+
name: 'invalid PascalCase instead of camelCase',
39+
document: {
40+
paths: {
41+
'/Resources': {},
42+
},
43+
},
44+
errors: [
45+
{
46+
code: 'xgen-IPA-102-collection-identifier-camelCase',
47+
message:
48+
"Collection identifiers must be in camelCase. Path segment 'Resources' in path '/Resources' is not in camelCase. http://go/ipa/102",
49+
path: ['paths', '/Resources'],
50+
severity: DiagnosticSeverity.Warning,
51+
},
52+
],
53+
},
54+
{
55+
name: 'invalid with snake_case instead of camelCase',
56+
document: {
57+
paths: {
58+
'/resource_groups': {},
59+
},
60+
},
61+
errors: [
62+
{
63+
code: 'xgen-IPA-102-collection-identifier-camelCase',
64+
message:
65+
"Collection identifiers must be in camelCase. Path segment 'resource_groups' in path '/resource_groups' is not in camelCase. http://go/ipa/102",
66+
path: ['paths', '/resource_groups'],
67+
severity: DiagnosticSeverity.Warning,
68+
},
69+
],
70+
},
71+
{
72+
name: 'invalid with kebab-case instead of camelCase',
73+
document: {
74+
paths: {
75+
'/resource-groups': {},
76+
},
77+
},
78+
errors: [
79+
{
80+
code: 'xgen-IPA-102-collection-identifier-camelCase',
81+
message:
82+
"Collection identifiers must be in camelCase. Path segment 'resource-groups' in path '/resource-groups' is not in camelCase. http://go/ipa/102",
83+
path: ['paths', '/resource-groups'],
84+
severity: DiagnosticSeverity.Warning,
85+
},
86+
],
87+
},
88+
{
89+
name: 'invalid resource path with invalid casing but valid custom method',
90+
document: {
91+
paths: {
92+
'/Resources:createResource': {},
93+
},
94+
},
95+
errors: [
96+
{
97+
code: 'xgen-IPA-102-collection-identifier-camelCase',
98+
message:
99+
"Collection identifiers must be in camelCase. Path segment 'Resources' in path '/Resources:createResource' is not in camelCase. http://go/ipa/102",
100+
path: ['paths', '/Resources:createResource'],
101+
severity: DiagnosticSeverity.Warning,
102+
},
103+
],
104+
},
105+
{
106+
name: 'invalid with consecutive uppercase letters',
107+
document: {
108+
paths: {
109+
'/resourcesAPI': {},
110+
},
111+
},
112+
errors: [
113+
{
114+
code: 'xgen-IPA-102-collection-identifier-camelCase',
115+
message:
116+
"Collection identifiers must be in camelCase. Path segment 'resourcesAPI' in path '/resourcesAPI' is not in camelCase. http://go/ipa/102",
117+
path: ['paths', '/resourcesAPI'],
118+
severity: DiagnosticSeverity.Warning,
119+
},
120+
],
121+
},
122+
{
123+
name: 'valid with path-level exception',
124+
document: {
125+
paths: {
126+
'/resource_groups': {
127+
'x-xgen-IPA-exception': {
128+
'xgen-IPA-102-collection-identifier-camelCase': 'Legacy API path that cannot be changed',
129+
},
130+
},
131+
},
132+
},
133+
errors: [],
134+
},
135+
{
136+
name: 'reports violations for paths with double slashes',
137+
document: {
138+
paths: {
139+
'/api//users': {},
140+
'/resources///{resourceId}': {},
141+
'//doubleSlashAtStart': {},
142+
},
143+
},
144+
errors: [
145+
{
146+
code: 'xgen-IPA-102-collection-identifier-camelCase',
147+
message:
148+
"Collection identifiers must be in camelCase. Path '/api//users' contains double slashes (//) which is not allowed. http://go/ipa/102",
149+
path: ['paths', '/api//users'],
150+
severity: DiagnosticSeverity.Warning,
151+
},
152+
{
153+
code: 'xgen-IPA-102-collection-identifier-camelCase',
154+
message:
155+
"Collection identifiers must be in camelCase. Path '/resources///{resourceId}' contains double slashes (//) which is not allowed. http://go/ipa/102",
156+
path: ['paths', '/resources///{resourceId}'],
157+
severity: DiagnosticSeverity.Warning,
158+
},
159+
{
160+
code: 'xgen-IPA-102-collection-identifier-camelCase',
161+
message:
162+
"Collection identifiers must be in camelCase. Path '//doubleSlashAtStart' contains double slashes (//) which is not allowed. http://go/ipa/102",
163+
path: ['paths', '//doubleSlashAtStart'],
164+
severity: DiagnosticSeverity.Warning,
165+
},
166+
],
167+
},
168+
{
169+
name: 'handles paths with trailing slashes',
170+
document: {
171+
paths: {
172+
'/api/users/': {},
173+
'/resources/{resourceId}/': {},
174+
},
175+
},
176+
errors: [],
177+
},
178+
{
179+
name: 'detects multiple failures across a single path',
180+
document: {
181+
paths: {
182+
'/API/Resource_groups/{userId}/User-profiles': {},
183+
},
184+
},
185+
errors: [
186+
{
187+
code: 'xgen-IPA-102-collection-identifier-camelCase',
188+
message:
189+
"Collection identifiers must be in camelCase. Path segment 'API' in path '/API/Resource_groups/{userId}/User-profiles' is not in camelCase. http://go/ipa/102",
190+
path: ['paths', '/API/Resource_groups/{userId}/User-profiles'],
191+
severity: DiagnosticSeverity.Warning,
192+
},
193+
{
194+
code: 'xgen-IPA-102-collection-identifier-camelCase',
195+
message:
196+
"Collection identifiers must be in camelCase. Path segment 'Resource_groups' in path '/API/Resource_groups/{userId}/User-profiles' is not in camelCase. http://go/ipa/102",
197+
path: ['paths', '/API/Resource_groups/{userId}/User-profiles'],
198+
severity: DiagnosticSeverity.Warning,
199+
},
200+
{
201+
code: 'xgen-IPA-102-collection-identifier-camelCase',
202+
message:
203+
"Collection identifiers must be in camelCase. Path segment 'User-profiles' in path '/API/Resource_groups/{userId}/User-profiles' is not in camelCase. http://go/ipa/102",
204+
path: ['paths', '/API/Resource_groups/{userId}/User-profiles'],
205+
severity: DiagnosticSeverity.Warning,
206+
},
207+
],
208+
},
209+
{
210+
name: 'handles mixed valid and invalid segments with custom methods',
211+
document: {
212+
paths: {
213+
'/api/Valid/Invalid_resource/{id}:validCustomMethod': {},
214+
},
215+
},
216+
errors: [
217+
{
218+
code: 'xgen-IPA-102-collection-identifier-camelCase',
219+
message:
220+
"Collection identifiers must be in camelCase. Path segment 'Valid' in path '/api/Valid/Invalid_resource/{id}:validCustomMethod' is not in camelCase. http://go/ipa/102",
221+
path: ['paths', '/api/Valid/Invalid_resource/{id}:validCustomMethod'],
222+
severity: DiagnosticSeverity.Warning,
223+
},
224+
{
225+
code: 'xgen-IPA-102-collection-identifier-camelCase',
226+
message:
227+
"Collection identifiers must be in camelCase. Path segment 'Invalid_resource' in path '/api/Valid/Invalid_resource/{id}:validCustomMethod' is not in camelCase. http://go/ipa/102",
228+
path: ['paths', '/api/Valid/Invalid_resource/{id}:validCustomMethod'],
229+
severity: DiagnosticSeverity.Warning,
230+
},
231+
],
232+
},
233+
{
234+
name: 'handles double slashes with invalid segments - both issues reported',
235+
document: {
236+
paths: {
237+
'/api//Invalid_segment//resources': {},
238+
},
239+
},
240+
errors: [
241+
{
242+
code: 'xgen-IPA-102-collection-identifier-camelCase',
243+
message:
244+
"Collection identifiers must be in camelCase. Path '/api//Invalid_segment//resources' contains double slashes (//) which is not allowed. http://go/ipa/102",
245+
path: ['paths', '/api//Invalid_segment//resources'],
246+
severity: DiagnosticSeverity.Warning,
247+
},
248+
{
249+
code: 'xgen-IPA-102-collection-identifier-camelCase',
250+
message:
251+
"Collection identifiers must be in camelCase. Path segment 'Invalid_segment' in path '/api//Invalid_segment//resources' is not in camelCase. http://go/ipa/102",
252+
path: ['paths', '/api//Invalid_segment//resources'],
253+
severity: DiagnosticSeverity.Warning,
254+
},
255+
],
256+
},
257+
]);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22
# http://go/ipa/102
33

44
rules:
5+
xgen-IPA-102-collection-identifier-camelCase:
6+
description: >-
7+
Collection identifiers must be in camelCase. Logic includes:<br/>
8+
- All path segments that are not path parameters<br/>
9+
- Only the resource identifier part before any colon in custom method paths (e.g., `resource` in `/resource:customMethod`)<br/>
10+
- Path parameters should also follow camelCase naming<br/>
11+
- Certain values can be exempted via the ignoredValues configuration (e.g., 'v1', 'v2') that can be supplied as `ignoredValues`
12+
argument to the rule<br/>
13+
- Paths with `x-xgen-IPA-exception` for this rule are excluded from validation<br/>
14+
- Double slashes (//) are not allowed in paths<br/>
15+
http://go/ipa/102
16+
message: '{{error}} http://go/ipa/102'
17+
severity: warn
18+
given: $.paths
19+
then:
20+
field: '@key'
21+
function: collectionIdentifierCamelCase
22+
functionOptions:
23+
# Contains list of ignored path params
24+
ignoredValues: ['v2', 'v1']
25+
526
xgen-IPA-102-path-alternate-resource-name-path-param:
627
description: 'Paths should alternate between resource names and path params. http://go/ipa/102'
728
message: '{{error}} http://go/ipa/102'
@@ -23,3 +44,4 @@ rules:
2344
functions:
2445
- collectionIdentifierPattern
2546
- eachPathAlternatesBetweenResourceNameAndPathParam
47+
- collectionIdentifierCamelCase

tools/spectral/ipa/rulesets/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ For rule definitions, see [IPA-005.yaml](https://github.com/mongodb/openapi/blob
2020

2121
For rule definitions, see [IPA-102.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-102.yaml).
2222

23-
| Rule Name | Description | Severity |
24-
| ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | -------- |
25-
| xgen-IPA-102-path-alternate-resource-name-path-param | Paths should alternate between resource names and path params. http://go/ipa/102 | error |
26-
| xgen-IPA-102-collection-identifier-pattern | Collection identifiers must begin with a lowercase letter and contain only ASCII letters and numbers. http://go/ipa/102 | warn |
23+
| Rule Name | Description | Severity |
24+
| ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
25+
| xgen-IPA-102-path-alternate-resource-name-path-param | Paths should alternate between resource names and path params. http://go/ipa/102 | error |
26+
| xgen-IPA-102-collection-identifier-camelCase | Collection identifiers must be in camelCase. Logic includes:<br/> - All path segments that are not path parameters<br/> - Only the resource identifier part before any colon in custom method paths (e.g., `resource` in `/resource:customMethod`)<br/> - Path parameters should also follow camelCase naming<br/> - Certain values can be exempted via the ignoredValues configuration (e.g., 'v1', 'v2') that can be supplied as `ignoredValues` argument to the rule<br/> - Paths with `x-xgen-IPA-exception` for this rule are excluded from validation<br/> - Double slashes (//) are not allowed in paths<br/> http://go/ipa/102 | warn |
27+
| xgen-IPA-102-collection-identifier-pattern | Collection identifiers must begin with a lowercase letter and contain only ASCII letters and numbers. http://go/ipa/102 | warn |
2728

2829
### IPA-104
2930

0 commit comments

Comments
 (0)