Skip to content

Commit d77dfd2

Browse files
authored
fix: support arrays of length 1 in path parameters (#41)
1 parent 7f243f7 commit d77dfd2

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

src/__tests__/fixtures/request-parameters-path-extra/pact.json

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
"status": 200
1313
}
1414
},
15+
{
16+
"description": "should pass when path matches simple exploded spec, with only one item",
17+
"request": {
18+
"method": "GET",
19+
"path": "/simple/explode-true/1"
20+
},
21+
"response": {
22+
"status": 200
23+
}
24+
},
1525
{
1626
"description": "should pass when path matches simple non-exploded spec",
1727
"request": {
@@ -22,11 +32,31 @@
2232
"status": 200
2333
}
2434
},
35+
{
36+
"description": "should pass when path matches simple non-exploded spec, with only one item",
37+
"request": {
38+
"method": "GET",
39+
"path": "/simple/explode-false/1"
40+
},
41+
"response": {
42+
"status": 200
43+
}
44+
},
2545
{
2646
"description": "should pass when path matches label exploded spec",
2747
"request": {
2848
"method": "GET",
29-
"path": "/label/explode-true/.1,2,3"
49+
"path": "/label/explode-true/.1.2.3"
50+
},
51+
"response": {
52+
"status": 200
53+
}
54+
},
55+
{
56+
"description": "should pass when path matches label exploded spec, with only one item",
57+
"request": {
58+
"method": "GET",
59+
"path": "/label/explode-true/.1"
3060
},
3161
"response": {
3262
"status": 200
@@ -42,11 +72,31 @@
4272
"status": 200
4373
}
4474
},
75+
{
76+
"description": "should pass when path matches label non-exploded spec, with only one item",
77+
"request": {
78+
"method": "GET",
79+
"path": "/label/explode-false/.1"
80+
},
81+
"response": {
82+
"status": 200
83+
}
84+
},
4585
{
4686
"description": "should pass when path matches matrix exploded spec",
4787
"request": {
4888
"method": "GET",
49-
"path": "/matrix/explode-true/;1,2,3"
89+
"path": "/matrix/explode-true/;1;2;3"
90+
},
91+
"response": {
92+
"status": 200
93+
}
94+
},
95+
{
96+
"description": "should pass when path matches matrix exploded spec, with only one item",
97+
"request": {
98+
"method": "GET",
99+
"path": "/matrix/explode-true/;1"
50100
},
51101
"response": {
52102
"status": 200
@@ -62,6 +112,16 @@
62112
"status": 200
63113
}
64114
},
115+
{
116+
"description": "should pass when path matches matrix non-exploded spec, with only one item",
117+
"request": {
118+
"method": "GET",
119+
"path": "/matrix/explode-false/;1"
120+
},
121+
"response": {
122+
"status": 200
123+
}
124+
},
65125
{
66126
"description": "should error when path does not match simple exploded integer array spec",
67127
"request": {

src/__tests__/fixtures/request-parameters-path-extra/results.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"mockDetails": {
66
"interactionDescription": "should error when path does not match simple exploded integer array spec",
77
"interactionState": "[none]",
8-
"location": "[root].interactions[6].request.path",
8+
"location": "[root].interactions[12].request.path",
99
"value": "/simple/explode-true/1,2,abc"
1010
},
1111
"specDetails": {
@@ -44,7 +44,7 @@
4444
"mockDetails": {
4545
"interactionDescription": "should error when path does not match simple non-exploded integer array spec",
4646
"interactionState": "[none]",
47-
"location": "[root].interactions[7].request.path",
47+
"location": "[root].interactions[13].request.path",
4848
"value": "/simple/explode-false/1,2,abc"
4949
},
5050
"specDetails": {
@@ -83,7 +83,7 @@
8383
"mockDetails": {
8484
"interactionDescription": "should error when path does not match label exploded integer array spec",
8585
"interactionState": "[none]",
86-
"location": "[root].interactions[8].request.path",
86+
"location": "[root].interactions[14].request.path",
8787
"value": "/label/explode-true/1,2,abc"
8888
},
8989
"specDetails": {
@@ -99,7 +99,7 @@
9999
"mockDetails": {
100100
"interactionDescription": "should error when path does not match label non-exploded integer array spec",
101101
"interactionState": "[none]",
102-
"location": "[root].interactions[9].request.path",
102+
"location": "[root].interactions[15].request.path",
103103
"value": "/label/explode-false/1,2,abc"
104104
},
105105
"specDetails": {
@@ -115,7 +115,7 @@
115115
"mockDetails": {
116116
"interactionDescription": "should error when path does not match matrix exploded integer array spec",
117117
"interactionState": "[none]",
118-
"location": "[root].interactions[10].request.path",
118+
"location": "[root].interactions[16].request.path",
119119
"value": "/matrix/explode-true/1,2,abc"
120120
},
121121
"specDetails": {
@@ -131,7 +131,7 @@
131131
"mockDetails": {
132132
"interactionDescription": "should error when path does not match matrix non-exploded integer array spec",
133133
"interactionState": "[none]",
134-
"location": "[root].interactions[11].request.path",
134+
"location": "[root].interactions[17].request.path",
135135
"value": "/matrix/explode-false/1,2,abc"
136136
},
137137
"specDetails": {

src/compare/requestPath.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,21 @@ export function* compareReqPath(
3939
const validate = getValidateFunction(ajv, schemaId, () =>
4040
minimumSchema(schema, oas),
4141
);
42-
if (!validate(value)) {
42+
43+
let separator = ",";
44+
if (parameter.style?.toLowerCase() === "label" && parameter.explode) {
45+
separator = ".";
46+
}
47+
if (parameter.style?.toLowerCase() === "matrix" && parameter.explode) {
48+
separator = ";";
49+
}
50+
if (
51+
!validate(
52+
schema.type === "array" && typeof value === "string"
53+
? value.split(separator)
54+
: value,
55+
)
56+
) {
4357
for (const _error of validate.errors!) {
4458
yield {
4559
code: "request.path-or-method.unknown",

0 commit comments

Comments
 (0)