@@ -89,34 +89,37 @@ export function getResolver ({
89
89
90
90
// handle arguments provided by links
91
91
for ( let paramName in argsFromLink ) {
92
- let value = argsFromLink [ paramName ]
93
-
94
- // parameter names can specify location of parameter (e.g., path.id)
95
92
let paramNameWithoutLocation = paramName
96
93
if ( paramName . indexOf ( '.' ) !== - 1 ) {
97
94
paramNameWithoutLocation = paramName . split ( '.' ) [ 1 ]
98
95
}
99
96
100
- // CASE: parameter in body
101
- if ( / b o d y # / . test ( value ) ) {
102
- let tokens = JSONPath . JSONPath ( { path : value . split ( 'body#/' ) [ 1 ] , json : root } )
103
- if ( Array . isArray ( tokens ) && tokens . length > 0 ) {
104
- args [ paramNameWithoutLocation ] = tokens [ 0 ]
97
+ // link parameter
98
+ let value = argsFromLink [ paramName ]
99
+
100
+ /**
101
+ * see if the link parameter contains constants that are appended to the link parameter
102
+ *
103
+ * e.g. instead of:
104
+ * $response.body#/employerId
105
+ *
106
+ * it could be:
107
+ * abc_{$response.body#/employerId}
108
+ */
109
+ if ( value . search ( / { | } / ) === - 1 ) {
110
+ if ( isRuntimeExpression ( value ) ) {
111
+ args [ paramNameWithoutLocation ] = resolveLinkParameter ( paramName , value , _oasgraph , root )
105
112
} else {
106
- log ( `Warning: could not extract parameter ${ paramName } form link` )
113
+ args [ paramNameWithoutLocation ] = value
107
114
}
108
- // CASE: parameter in previous query parameter
109
- } else if ( / q u e r y \. / . test ( value ) ) {
110
- args [ paramNameWithoutLocation ] =
111
- _oasgraph . usedParams [ Oas3Tools . beautify ( value . split ( 'query.' ) [ 1 ] ) ]
112
- // CASE: parameter in previous path parameter
113
- } else if ( / p a t h \. / . test ( value ) ) {
114
- args [ paramNameWithoutLocation ] =
115
- _oasgraph . usedParams [ Oas3Tools . beautify ( value . split ( 'path.' ) [ 1 ] ) ]
116
- // CASE: link OASGraph currently does not support
117
115
} else {
118
- log ( `Warning: could not process link parameter ${ paramName } with ` +
119
- `value ${ value } ` )
116
+ // replace link parameters with appropriate values
117
+ let linkParams = value . match ( / { ( [ ^ } ] * ) } / g)
118
+ linkParams . forEach ( ( linkParam ) => {
119
+ value = value . replace ( linkParam , resolveLinkParameter ( paramName , linkParam . substring ( 1 , linkParam . length - 1 ) , _oasgraph , root ) )
120
+ } )
121
+
122
+ args [ paramNameWithoutLocation ] = value
120
123
}
121
124
}
122
125
@@ -428,3 +431,61 @@ function getAuthReqAndProtcolName (
428
431
authRequired
429
432
}
430
433
}
434
+
435
+ /**
436
+ * Given a link parameter, determine the value
437
+ *
438
+ * The link parameter is a reference to data contained in the
439
+ * url/method/statuscode or response/request body/query/path/header
440
+ */
441
+ function resolveLinkParameter ( paramName : string , value : string , _oasgraph : any , root : any ) : any {
442
+ // CASE: parameter in body
443
+ if ( / b o d y # / . test ( value ) ) {
444
+ let tokens = JSONPath . JSONPath ( { path : value . split ( 'body#/' ) [ 1 ] , json : root } )
445
+ if ( Array . isArray ( tokens ) && tokens . length > 0 ) {
446
+ return tokens [ 0 ]
447
+ } else {
448
+ log ( `Warning: could not extract parameter ${ paramName } from link` )
449
+ }
450
+
451
+ // CASE: parameter in previous query parameter
452
+ } else if ( / q u e r y \. / . test ( value ) ) {
453
+ return _oasgraph . usedParams [ Oas3Tools . beautify ( value . split ( 'query.' ) [ 1 ] ) ]
454
+
455
+ // CASE: parameter in previous path parameter
456
+ } else if ( / p a t h \. / . test ( value ) ) {
457
+ return _oasgraph . usedParams [ Oas3Tools . beautify ( value . split ( 'path.' ) [ 1 ] ) ]
458
+
459
+ // CASE: link OASGraph currently does not support
460
+ } else {
461
+ log ( `Warning: could not process link parameter ${ paramName } with ` +
462
+ `value ${ value } ` )
463
+ }
464
+ }
465
+
466
+ /**
467
+ * Check if a string is a runtime expression in the context of link parameters
468
+ */
469
+ function isRuntimeExpression ( str : string ) : boolean {
470
+ let references = [ "header." , "query." , "path." , "body" ]
471
+
472
+ if ( str . startsWith ( "$url" ) || str . startsWith ( "$method" ) || str . startsWith ( "$statusCode" ) ) {
473
+ return true
474
+
475
+ } else if ( str . startsWith ( "$request." ) ) {
476
+ for ( let i = 0 ; i < references . length ; i ++ ) {
477
+ if ( str . startsWith ( `$request.${ references [ i ] } ` ) ) {
478
+ return true
479
+ }
480
+ }
481
+
482
+ } else if ( str . startsWith ( "$response." ) ) {
483
+ for ( let i = 0 ; i < references . length ; i ++ ) {
484
+ if ( str . startsWith ( `$response.${ references [ i ] } ` ) ) {
485
+ return true
486
+ }
487
+ }
488
+ }
489
+
490
+ return false
491
+ }
0 commit comments