Skip to content

Commit f502e8e

Browse files
authored
feat: support customisable basePath in OAS3 (#135)
1 parent 8cc0ab4 commit f502e8e

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

.changeset/tasty-days-slide.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+
Support customisable basePath in OAS3
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openapi: 3.0.0
2+
info:
3+
x-opc-config-base-path: /custom-prefix
4+
title: OAS
5+
version: 1.0.0
6+
7+
paths:
8+
/path:
9+
get:
10+
responses:
11+
"200":
12+
description: ok
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"consumer": { "name": "consumer" },
3+
"provider": { "name": "provider" },
4+
"interactions": [
5+
{
6+
"description": "should pass when using prefix",
7+
"request": {
8+
"method": "GET",
9+
"path": "/custom-prefix/path"
10+
},
11+
"response": {
12+
"status": 200
13+
}
14+
}
15+
]
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[
2+
{
3+
"code": "request.path-or-method.unknown",
4+
"message": "Path or method not defined in spec file: GET /custom-prefix/path",
5+
"mockDetails": {
6+
"interactionDescription": "should pass when using prefix",
7+
"interactionState": "[none]",
8+
"location": "[root].interactions[0].request.path",
9+
"value": "/custom-prefix/path"
10+
},
11+
"specDetails": {
12+
"location": "[root].paths",
13+
"pathMethod": null,
14+
"pathName": null,
15+
"value": {
16+
"/path": {
17+
"get": {
18+
"responses": {
19+
"200": {
20+
"description": "ok"
21+
}
22+
}
23+
}
24+
}
25+
}
26+
},
27+
"type": "error"
28+
}
29+
]

src/compare/setup.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const SUPPORTED_METHODS = [
2929
"TRACE",
3030
];
3131

32+
interface ExtendedOpenAPIV3InfoObject extends OpenAPIV3.InfoObject {
33+
"x-opc-config-base-path"?: string;
34+
}
35+
3236
export function setupRouter(
3337
oas: OpenAPIV2.Document | OpenAPIV3.Document,
3438
config: Config,
@@ -41,8 +45,12 @@ export function setupRouter(
4145
});
4246
for (const oasPath in oas.paths) {
4347
// NB: all path parameters are required in OAS
48+
const basePath =
49+
(oas.info as ExtendedOpenAPIV3InfoObject)["x-opc-config-base-path"] ||
50+
(oas as OpenAPIV2.Document).basePath ||
51+
"";
4452
const path =
45-
((oas as OpenAPIV2.Document).basePath || "") +
53+
basePath +
4654
oasPath
4755
.replaceAll(/{.*?}/g, cleanPathParameter)
4856
.replaceAll(/{([.;]?)([^*]+?)\*?}/g, "$1:$2(.+)");

0 commit comments

Comments
 (0)