From 29b96eb7b74fa8b71938c339df7bb89a445ae722 Mon Sep 17 00:00:00 2001
From: "Mark Rainbird (WLT GB)" <mark.rainbird@Wlt.com>
Date: Mon, 25 Jan 2021 19:07:02 +0000
Subject: [PATCH] Changes for Fusion compatability

Existing application does not work with Fusion due to different
implementation of collection function. Fusion does not return non-XML
content such as XQuery files which means the endpoints are not
found. There is also a function name clash in openapi-tests-simple.xqm
which creates an error in Fusion with the sample endpoints.

Issue is resolved with recursive functions that use xmldb functions  in
order to find child resources and collections to build the list of uris in
place of existing base-uri function. This works in both Fusion and eXist

There is also an extension to the list of recognised xquery file
extensions to cover other used formats.
---
 content/openapi-tests-simple.xqm |  4 ++--
 content/openapi.xqm              | 40 +++++++++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/content/openapi-tests-simple.xqm b/content/openapi-tests-simple.xqm
index 2bbb59b..7bcf507 100644
--- a/content/openapi-tests-simple.xqm
+++ b/content/openapi-tests-simple.xqm
@@ -210,7 +210,7 @@ as element(test) {
 declare
 %rest:POST("{$body}")
 %rest:path("/openapi-test/simple/multi-function")
-function openapi-test-simple:multi-post($body)
+function openapi-test-simple:multi-post-function($body)
 as element(test) {
     <test>
         <parameters n="0"/>
@@ -226,7 +226,7 @@ as element(test) {
 declare
 %rest:POST("{$body}")
 %rest:path("/openapi-test/simple/multi-module")
-function openapi-test-simple:multi-post($body)
+function openapi-test-simple:multi-post-module($body)
 as element(test) {
     <test>
         <parameters n="0"/>
diff --git a/content/openapi.xqm b/content/openapi.xqm
index 00e7fb6..4484a47 100644
--- a/content/openapi.xqm
+++ b/content/openapi.xqm
@@ -34,7 +34,7 @@ as xs:string {
  :)
 declare function openapi:main($target as xs:string)
 as map(*) {
-  let $modules-uris := collection($target)[openapi:xquery-resource(string(base-uri()))]/base-uri()
+  let $modules-uris := openapi:files($target, ())[openapi:xquery-resource(.)]
   let $module :=
     for $module in $modules-uris
     let $test4rest := contains(util:binary-doc($module) => util:base64-decode(), "%rest:")
@@ -420,4 +420,42 @@ as xs:boolean {
      ends-with($baseuri, ".xqm")
   or ends-with($baseuri, ".xql")
   or ends-with($baseuri, ".xq")
+  or ends-with($baseuri, ".xquery")
+  or ends-with($baseuri, ".xqy")
+};
+
+(:~
+ : Returns the names of the child resources in collection URI supplied
+ : @param $target URI under which child resources are to be returned :)
+declare function openapi:files-uris($target as xs:string) {
+	let $new-uris as xs:string* := xmldb:get-child-resources($target)
+	let $new-uris as xs:string* :=
+	    for $n in $new-uris return fn:concat($target, '/', $n)
+	return $new-uris
+};
+
+(:~
+ : Returns the names of the child collections in collection URI supplied
+ : @param $target URI under which collections are to be returned :)
+declare function openapi:collections-uris($target as xs:string) {
+	let $collections as xs:string* := xmldb:get-child-collections($target)
+	return $collections
+};
+
+(:~
+ : Recursive function to use instead of collection as implementation of this function
+ : varies between Fusion and eXist so in Fusion XQuery files are not found.
+ : @param $target URI under which file listing is required
+ : @param $uris Existing URIs to be retained and added to until completion :)
+declare function openapi:files($target as xs:string, $uris as xs:string*) {
+	let $current-uris as xs:string* := openapi:files-uris($target)
+	let $current-collections as xs:string* := openapi:collections-uris($target)
+	let $new-uris as xs:string* :=
+    	if (fn:empty($current-collections)) then ()
+    	else
+        	for $c in $current-collections
+        	    let $target := fn:concat($target, '/', $c)
+            	return (openapi:files($target, $uris))
+    let $uris as xs:string* := ($current-uris, $new-uris, $uris)
+    return ($uris)
 };
-- 
GitLab