diff --git a/FAQ.md b/FAQ.md index 0fe4a1650..23628778d 100644 --- a/FAQ.md +++ b/FAQ.md @@ -51,9 +51,7 @@ getAllowedEndpoints(endpointsOf3_0_0, Implementations3_0_0.resourceDocs) 4) Once we have a final list of routes we serve them: - routes.foreach(route => { - oauthServe(apiPrefix{route}, findResourceDoc(route)) - }) + `registerRoutes(routes, allResourceDocs, apiPrefix, autoValidateAll = true)` diff --git a/obp-api/src/main/scala/code/api/OBPRestHelper.scala b/obp-api/src/main/scala/code/api/OBPRestHelper.scala index b672e36e6..5e0b7a803 100644 --- a/obp-api/src/main/scala/code/api/OBPRestHelper.scala +++ b/obp-api/src/main/scala/code/api/OBPRestHelper.scala @@ -474,26 +474,33 @@ trait OBPRestHelper extends RestHelper with MdcLoggable { result } - protected def findResourceDoc(pf: OBPEndpoint, allResourceDocs: ArrayBuffer[ResourceDoc]): Option[ResourceDoc] = { - allResourceDocs.find(_.partialFunction == pf) - } - protected def registerRoutes(routes: List[OBPEndpoint], allResourceDocs: ArrayBuffer[ResourceDoc], apiPrefix:OBPEndpoint => OBPEndpoint, autoValidateAll: Boolean = false): Unit = { - routes.foreach(route => { - val maybeResourceDoc = findResourceDoc(route, allResourceDocs) - val isAutoValidate = maybeResourceDoc.map { doc => - doc.isValidateEnabled || (autoValidateAll && !doc.isValidateDisabled && doc.implementedInApiVersion == version) - }.getOrElse(false) - // if rd contains ResourceDoc, when autoValidateAll or doc isAutoValidate, just wrapped to auth check endpoint - val authCheckRoute: OBPEndpoint = (maybeResourceDoc, isAutoValidate) match { - case (Some(doc), true) => doc.wrappedWithAuthCheck(route) - case _ => route + def isAutoValidate(doc: ResourceDoc): Boolean = + doc.isValidateEnabled || (autoValidateAll && !doc.isValidateDisabled && doc.implementedInApiVersion == version) + + for(route <- routes) { + // one endpoint can have multiple ResourceDocs, so here use filter instead of find, e.g APIMethods400.Implementations400.createTransactionRequest + val resourceDocs = allResourceDocs.filter(_.partialFunction == route) + + if(resourceDocs.isEmpty) { + oauthServe(apiPrefix(route), None) + } else { + val (autoValidateDocs, other) = resourceDocs.partition(isAutoValidate) + // autoValidateAll or doc isAutoValidate, just wrapped to auth check endpoint + autoValidateDocs.foreach { doc => + val wrappedEndpoint = doc.wrappedWithAuthCheck(route) + oauthServe(apiPrefix(wrappedEndpoint), Some(doc)) + } + //just register once for those not auto validate endpoints . + if (other.nonEmpty) { + oauthServe(apiPrefix(route), other.headOption) + } } - oauthServe(apiPrefix(authCheckRoute), maybeResourceDoc) - }) + } + } } \ No newline at end of file diff --git a/obp-api/src/main/scala/code/api/util/APIUtil.scala b/obp-api/src/main/scala/code/api/util/APIUtil.scala index ad7337740..39526d523 100644 --- a/obp-api/src/main/scala/code/api/util/APIUtil.scala +++ b/obp-api/src/main/scala/code/api/util/APIUtil.scala @@ -1373,25 +1373,25 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{ } // reset connectorMethods { - val checkerClassNames = mutable.ListBuffer[String]() + val checkerFunctions = mutable.ListBuffer[PartialFunction[_, _]]() if (isNeedCheckAuth) { - checkerClassNames += authenticatedAccessFun.getClass.getName + checkerFunctions += authenticatedAccessFun } else { - checkerClassNames += anonymousAccessFun.getClass.getName + checkerFunctions += anonymousAccessFun } if (isNeedCheckRoles) { - checkerClassNames += checkRolesFun.getClass.getName + checkerFunctions += checkRolesFun } if (isNeedCheckBank) { - checkerClassNames += checkBankFun.getClass.getName + checkerFunctions += checkBankFun } if (isNeedCheckAccount) { - checkerClassNames += checkAccountFun.getClass.getName + checkerFunctions += checkAccountFun } if (isNeedCheckView) { - checkerClassNames += checkViewFun.getClass.getName + checkerFunctions += checkViewFun } - val addedMethods: List[String] = checkerClassNames.toList.flatMap(getDependentConnectorMethods(_)).map("obp." +) + val addedMethods: List[String] = checkerFunctions.toList.flatMap(getDependentConnectorMethods(_)).map("obp." +) // add connector method to endpoint info addEndpointInfos(addedMethods, partialFunctionName, implementedInApiVersion)