File tree Expand file tree Collapse file tree 6 files changed +72
-1
lines changed
__tests__/fixtures/oas-base-path Expand file tree Collapse file tree 6 files changed +72
-1
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @pactflow/openapi-pact-comparator " : minor
3+ ---
4+
5+ Support customisable basePath in OAS3
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ []
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ ]
Original file line number Diff line number Diff 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+
3236export 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(.+)" ) ;
You can’t perform that action at this time.
0 commit comments