Skip to content

Commit 17103d0

Browse files
authored
fix: handle uri encoding in oas references (#137)
1 parent 1589500 commit 17103d0

File tree

6 files changed

+83
-5
lines changed

6 files changed

+83
-5
lines changed

.changeset/little-webs-stand.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pactflow/openapi-pact-comparator": minor
3+
---
4+
5+
Handle URI encoding in OAS references

src/__tests__/fixtures/oas-references/oas.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ paths:
99
parameters:
1010
- $ref: "#/components/parameters/Request.Param"
1111
- $ref: "#/components/parameters/nested.param~1with~0tilde"
12+
- $ref: "#/components/parameters/nested.param.with%7Bbrackets%7D"
1213
requestBody:
1314
$ref: "#/components/requestBodies/Request.Body"
1415
responses:
@@ -39,6 +40,12 @@ components:
3940
required: true
4041
schema:
4142
type: string
43+
"nested.param.with{brackets}":
44+
in: query
45+
name: bracket
46+
required: true
47+
schema:
48+
type: string
4249
requestBodies:
4350
Request.Body:
4451
required: true

src/__tests__/fixtures/oas-references/opc-results.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@
3131
},
3232
"type": "error"
3333
},
34+
{
35+
"code": "request.query.incompatible",
36+
"message": "Value is incompatible with the parameter defined in the spec file: must be string",
37+
"mockDetails": {
38+
"interactionDescription": "should fail with missing parameters",
39+
"interactionState": "[none]",
40+
"location": "[root].interactions[1].request.query.bracket"
41+
},
42+
"specDetails": {
43+
"location": "[root].paths./login.post.parameters[2].schema.type",
44+
"pathMethod": "post",
45+
"pathName": "/login",
46+
"value": "string"
47+
},
48+
"type": "error"
49+
},
3450
{
3551
"code": "request.body.incompatible",
3652
"message": "Request body is incompatible with the request body schema in the spec file: must have required property 'username'",

src/__tests__/fixtures/oas-references/pact.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"request": {
88
"method": "POST",
99
"path": "/login",
10-
"query": "account=foo&nested=bar",
10+
"query": "account=foo&nested=bar&bracket=baz",
1111
"headers": {
1212
"Content-Type": "application/json"
1313
},
@@ -49,7 +49,7 @@
4949
"request": {
5050
"method": "POST",
5151
"path": "/login",
52-
"query": "account=foo&nested=bar",
52+
"query": "account=foo&nested=bar&bracket=baz",
5353
"headers": {
5454
"Content-Type": "application/json"
5555
},
@@ -67,7 +67,7 @@
6767
"request": {
6868
"method": "POST",
6969
"path": "/login",
70-
"query": "account=foo&nested=bar",
70+
"query": "account=foo&nested=bar&bracket=baz",
7171
"headers": {
7272
"Content-Type": "application/json"
7373
},
@@ -90,7 +90,7 @@
9090
"request": {
9191
"method": "POST",
9292
"path": "/login",
93-
"query": "account=foo&nested=bar",
93+
"query": "account=foo&nested=bar&bracket=baz",
9494
"headers": {
9595
"Content-Type": "application/json"
9696
},

src/__tests__/fixtures/oas-references/smv-results.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,48 @@
8383
},
8484
"type": "error"
8585
},
86+
{
87+
"code": "request.query.incompatible",
88+
"message": "Value is incompatible with the parameter defined in the spec file: must have required property 'value'",
89+
"mockDetails": {
90+
"interactionDescription": "should fail with missing parameters",
91+
"interactionState": "[none]",
92+
"location": "[root].interactions[1]",
93+
"value": {
94+
"description": "should fail with missing parameters",
95+
"request": {
96+
"method": "POST",
97+
"path": "/login",
98+
"headers": {
99+
"Content-Type": "application/json"
100+
},
101+
"body": {
102+
"username": "bob"
103+
}
104+
},
105+
"response": {
106+
"status": 201,
107+
"body": {
108+
"id": "123"
109+
}
110+
}
111+
}
112+
},
113+
"specDetails": {
114+
"location": "[root].paths./login.post.parameters[2]",
115+
"pathMethod": "post",
116+
"pathName": "/login",
117+
"value": {
118+
"in": "query",
119+
"name": "bracket",
120+
"required": true,
121+
"schema": {
122+
"type": "string"
123+
}
124+
}
125+
},
126+
"type": "error"
127+
},
86128
{
87129
"code": "request.body.incompatible",
88130
"message": "Request body is incompatible with the request body schema in the spec file: must have required property 'username'",
@@ -175,6 +217,14 @@
175217
"schema": {
176218
"type": "string"
177219
}
220+
},
221+
{
222+
"in": "query",
223+
"name": "bracket",
224+
"required": true,
225+
"schema": {
226+
"type": "string"
227+
}
178228
}
179229
],
180230
"requestBody": {

src/utils/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { SchemaObject } from "ajv";
33
import { each, get } from "lodash-es";
44

55
const unescape = (subpath: string) =>
6-
subpath.replaceAll("~1", "/").replaceAll("~0", "~");
6+
decodeURI(subpath.replaceAll("~1", "/").replaceAll("~0", "~"));
77

88
export const splitPath = (path: string) => {
99
if (path.startsWith("#/")) {

0 commit comments

Comments
 (0)