Skip to content

Commit d9bc634

Browse files
committed
fix: support escaping json path
1 parent 2c78229 commit d9bc634

File tree

6 files changed

+76
-6
lines changed

6 files changed

+76
-6
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,49 @@
4242
},
4343
"type": "error"
4444
},
45+
{
46+
"code": "request.query.incompatible",
47+
"message": "Value is incompatible with the parameter defined in the spec file: must have required property 'value'",
48+
"mockDetails": {
49+
"interactionDescription": "should fail with missing parameters",
50+
"interactionState": "[none]",
51+
"location": "[root].interactions[1]",
52+
"value": {
53+
"description": "should fail with missing parameters",
54+
"providerState": "",
55+
"request": {
56+
"method": "POST",
57+
"path": "/login",
58+
"headers": {
59+
"Content-Type": "application/json"
60+
},
61+
"body": {
62+
"username": "bob"
63+
}
64+
},
65+
"response": {
66+
"status": 201,
67+
"body": {
68+
"id": "123"
69+
}
70+
}
71+
}
72+
},
73+
"specDetails": {
74+
"location": "[root].paths./login.post.parameters[1]",
75+
"pathMethod": "post",
76+
"pathName": "/login",
77+
"value": {
78+
"in": "query",
79+
"name": "nested",
80+
"required": true,
81+
"schema": {
82+
"type": "string"
83+
}
84+
}
85+
},
86+
"type": "error"
87+
},
4588
{
4689
"code": "request.body.incompatible",
4790
"message": "Request body is incompatible with the request body schema in the spec file: must have required property 'username'",

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ paths:
88
post:
99
parameters:
1010
- $ref: "#/components/parameters/Request.Param"
11+
- $ref: "#/components/parameters/nested.param~1with~0tilde"
1112
requestBody:
1213
$ref: "#/components/requestBodies/Request.Body"
1314
responses:
@@ -22,6 +23,12 @@ components:
2223
required: true
2324
schema:
2425
type: string
26+
"nested.param/with~tilde":
27+
in: query
28+
name: nested
29+
required: true
30+
schema:
31+
type: string
2532
requestBodies:
2633
Request.Body:
2734
content:

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"request": {
99
"method": "POST",
1010
"path": "/login",
11-
"query": "account=foo",
11+
"query": "account=foo&nested=bar",
1212
"headers": {
1313
"Content-Type": "application/json"
1414
},
@@ -49,7 +49,7 @@
4949
"request": {
5050
"method": "POST",
5151
"path": "/login",
52-
"query": "account=foo",
52+
"query": "account=foo&nested=bar",
5353
"headers": {
5454
"Content-Type": "application/json"
5555
},
@@ -68,7 +68,7 @@
6868
"request": {
6969
"method": "POST",
7070
"path": "/login",
71-
"query": "account=foo",
71+
"query": "account=foo&nested=bar",
7272
"headers": {
7373
"Content-Type": "application/json"
7474
},

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515
},
1616
"type": "error"
1717
},
18+
{
19+
"code": "request.query.incompatible",
20+
"message": "Value is incompatible with the parameter defined in the spec file: must be string",
21+
"mockDetails": {
22+
"interactionDescription": "should fail with missing parameters",
23+
"interactionState": "[none]",
24+
"location": "[root].interactions[1].request.query.nested."
25+
},
26+
"specDetails": {
27+
"location": "[root].paths./login.post.parameters[1].schema.type",
28+
"pathMethod": "post",
29+
"pathName": "/login",
30+
"value": "string"
31+
},
32+
"type": "error"
33+
},
1834
{
1935
"code": "request.body.incompatible",
2036
"message": "Request body is incompatible with the request body schema in the spec file: must have required property 'username'",

src/compare/setup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import addFormats from "ajv-formats";
44
import Router, { HTTPMethod } from "find-my-way";
55
import { uniqWith } from "lodash-es";
66
import { cleanPathParameter } from "./utils/parameters";
7+
import { dereferenceOas } from "../utils/schema";
78

89
export function setupAjv(options: Options): Ajv {
910
const ajv = new Ajv(options);
@@ -51,7 +52,7 @@ export function setupRouter(
5152
[
5253
...(operation.parameters || []),
5354
...(oas.paths[oasPath].parameters || []),
54-
] as OpenAPIV3.ParameterObject[],
55+
].map((p) => dereferenceOas(p, oas as OpenAPIV3.Document)),
5556
(a, b) => `${a.name}${a.in}` === `${b.name}${b.in}`,
5657
);
5758
if (parameters.length) {

src/utils/schema.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import type { OpenAPIV3 } from "openapi-types";
22
import type { SchemaObject } from "ajv";
33
import { each, get } from "lodash-es";
44

5+
const unescape = (subpath: string) =>
6+
subpath.replaceAll("~1", "/").replaceAll("~0", "~");
7+
58
export const splitPath = (path: string) => {
69
if (path.startsWith("#/")) {
7-
return path.substring(2).split("/");
10+
return path.substring(2).split("/").map(unescape);
811
}
912

1013
if (path.startsWith("/")) {
11-
return path.substring(1).split("/");
14+
return path.substring(1).split("/").map(unescape);
1215
}
1316

1417
return path;

0 commit comments

Comments
 (0)