0

When calling a REST function that returns JSON and requesting the object return schema, sometimes (randomly it seems) the result is wrapped in an object that has the function name as the only key. This makes parsing the result a bit tiresome, e.g.:

With a DB function:

CREATE OR REPLACE FUNCTION api.conf_test(param TEXT)
RETURNS JSON AS $$
BEGIN
    RETURN JSON_BUILD_OBJECT('auth', param, 'perm', param);
END;
$$
LANGUAGE plpgsql STABLE;
GRANT EXECUTE ON FUNCTION api.conf_test(TEXT) TO anon, rest_user_max;

And a REST call:

return ajax({
    url: 'https://' + site.domain + '/api/v1/rpc/conf_test',
    type: 'POST',
    data: JSON.stringify({param: user.id}),
    headers: {
        'Accept': 'application/vnd.pgrst.object+json',
        'Content-Type': 'application/json'
    }
}).then(function(result) {
    if (result.data.conf_test === undefined) {
        site.jwts = result.data;
    } else {
        site.jwts = result.data.conf_test;
    }
}).catch(function(e) {
    site.jwts = null;
    throw e;
});

Sometimes the response is:

{"auth" : "12213", "perm" : "12213"}

And other times it is:

{"conf_test" : {"auth" : "12213", "perm" : "12213"}}

I have seen it change from one format to the other all by itself on a given server. Anyone know why this is happening?

EDIT:

Note - the REST request does not have the Authorization header, as in real life it is a request to get the two JWTs (auth and perm). Not sure this matters, just that the PostgreSQL role is "anon" in this case.

user9645
  • 6,286
  • 6
  • 29
  • 43
  • Interesting, could you add this info as a new issue on the project in Github? We can debug it there. https://github.com/begriffs/postgrest/issues – Joe Nelson Feb 15 '18 at 08:35
  • @Joe Nelson - have done [#1068](https://github.com/begriffs/postgrest/issues/1068), thanks. – user9645 Feb 15 '18 at 18:18

1 Answers1

0

It looks like my issue was probably due to the PostgREST schema cache not being refreshed after schema changes were made. I have not seen the problem since implementing the automatic schema reloading as described in the latest docs.

user9645
  • 6,286
  • 6
  • 29
  • 43