Hi Christian,

Thanks for your reply! And thank you for the new BaseX release today!

The error occurred in the context of a test suite where BaseX test functions 
are sending http requests to a BaseX HTTP server running locally to test RESTXQ 
functions.

I’ve tried again starting with version 11.3, then 11.4, then each version 
through 11.7 (!) and the latest snapshot. The problem starts with version 11.4.

I also tried using Postman to send the http request. Using Postman the RESTXQ 
function returns the expected response. So problem is in not in the RESTXQ 
function but actually in the test suite.

I put together a small self-contained example to that illustrates the problem:

module namespace my = "test";


declare
  %rest:POST("{$body}")
  %rest:path("/my/function")
  %rest:consumes('application/json')
  %input:json('format=xquery')
  %output:method("json")
function my:function (
  $body
) {
  admin:write-log($body),
  let $items as xs:string* := array:flatten($body?items)
  return string-join($items, ", ")
};


declare
  %unit:test
function my:test () {
  let $url := "http://localhost:8080/my/function";
  let $body := '{ "items": [ ["item 1", "item 2", "item 3"] ] }'
  let $request :=
    <http:request method="post" json="format=xquery,lax=true">
      <http:header name="Content-Type" value="application/json"/>
      <http:body media-type="application/json">{$body}</http:body>
    </http:request>
  let $response := http:send-request($request, $url)
  return (
    message($response, 'response: '),
    unit:assert-equals($response[2], "item 1, item 2, item 3")
  )
};


declare
  %rest:error("*")
  %rest:error-param('description', "{$description}")
  %rest:error-param('code', "{$code}")
  %rest:error-param('additional', "{$additional}")
  %output:method("html")
function my:error(
  $description as xs:string?,
  $code as xs:string?,
  $additional as item()*
) {
  admin:write-log(string-join((
    $code,
    $description,
    $additional =!> replace(replace(Q{java:org.basex.util.Prop}HOMEDIR(), '\\', 
'/'), '', 'qi'),
    string-join((request:path(), request:query()), '?')
  ), ' '), 'ERROR'),
  web:response-header((), (), map{'status': 400}),
  <p>{"An error occurred: " || $code || ", " || $description }</p>
};


The log records the error:

15:32:16.692    127.0.0.1:53606 admin   REQUEST [POST] /my/function
15:32:16.700    127.0.0.1:53606 admin   INFO    { "items": [ ["item 1", "item 
2", "item 3"] ] }
15:32:16.702    127.0.0.1:53606 admin   ERROR   err:XPTY0004 Input of lookup 
must be map or array: "{ ""items"": [ [""item 1"", ""item 2"", ""item 3""] ] 
}". src/webapp/test.xq, 14/51 src/webapp/test.xq, 10/10 /my/function

The error is shown as occurring on line 14, which is:

let $items as xs:string* := array:flatten($body?items)


Does something need to be updated around the http:send-request() in the 
my:test() function?

Many thanks,
Vincent


_____________________________________________
Vincent M. Lizzi
Head of Information Standards | Taylor & Francis Group
vincent.li...@taylorandfrancis.com<mailto:vincent.li...@taylorandfrancis.com>



Information Classification: General
From: Christian Grün <christian.gr...@gmail.com>
Sent: Friday, January 31, 2025 4:36 AM
To: Lizzi, Vincent <vincent.li...@taylorandfrancis.com>
Cc: BaseX <basex-talk@mailman.uni-konstanz.de>
Subject: Re: [basex-talk] RESTXQ JSON parsing in BaseX 11.6

Hi Vincent,

Could you give us additional information on how you dispatch your HTTP request? 
With the following request, everything seems to work fine:

input.json:
  { "items": [ ["item 1", "item 2", "item 3"] ] }
command:
  curl -H"Content-Type:application/json" -XPOST -Tinput.json 
"http://localhost/my/function<http://localhost/my/function>"

In addition, feel free to run your query with the latest snapshot [1], which 
includes fixes for the processing of data that is incoming through RESTXQ 
endpoints.

Best,
Christian

[1] 
https://files.basex.org/releases/latest/<https://files.basex.org/releases/latest/>


On Thu, Jan 30, 2025 at 8:17 PM Lizzi, Vincent 
<vincent.li...@taylorandfrancis.com<mailto:vincent.li...@taylorandfrancis.com>> 
wrote:
Hi BaseX folks,

I’m encountering a problem in updating to BaseX 11.6 from 11.3

I have a RESTXQ function defined like this:

declare
  %rest:POST("{$body}")
  %rest:path("/my/function")
  %rest:consumes('application/json')
  %input:json('format=xquery')
function my:function (
  $body
) as element(rest:forward) {
  let $items as xs:string* := array:flatten($body?items)
  (: omitted logic :)
  return web:forward()
}

Everything works as expected using BaseX versions 11.3 up to 11.5, but with 
11.6 an error occurs with the lookup operator and produces this error message:

err:XPTY0004 Input of lookup must be map or array

I did an experiment to try isolating the problem:

let $json := '{ "items": [ ["item 1", "item 2", "item 3"] ] }'
return array:flatten(json:parse($json, map{'format': 'xquery'})?items)

but this produced the expected list of items without error.

Has something changed in BaseX version 11.6 specifically in JSON parsing in 
RESTXQ?

Thanks,
Vincent


______________________________________________
Vincent M. Lizzi
Head of Information Standards | Taylor & Francis Group
530 Walnut St., Suite 850, Philadelphia, PA 19106
E-Mail: 
vincent.li...@taylorandfrancis.com<mailto:vincent.li...@taylorandfrancis.com>
Web: www.tandfonline.com<http://www.tandfonline.com>

Taylor & Francis is a trading name of Informa UK Limited,
registered in England under no. 1072954

"Everything should be made as simple as possible, but not simpler."



Information Classification: General

Reply via email to