PostgREST / postgrest

REST API for any Postgres database
https://postgrest.org
MIT License
23.48k stars 1.03k forks source link

Using select + text/csv output results in wrong csv header #3627

Open bjornharrtell opened 5 months ago

bjornharrtell commented 5 months ago

When using select query string param to reorder and/or omit columns and outpout format text/csv the csv column names header will still be as without select (but data is selected as expected).

laurenceisla commented 5 months ago

An extra example would be great to further clarify the issue. The request and the expected/wrong responses would be enough, e.g.:

The request:

curl 'localhost:3000/table?select=a,b' -H "Accept: text/csv"

Expected:

expected,csv

Got:

bad,csv
wolfgangwalther commented 4 months ago

Against our spec fixtures:

A test for regular views/tables:

    it "should respond with CSV with custom select" $
      request methodGet "/projects?select=name,id,client_id&id=eq.2"
              (acceptHdrs "text/csv") ""
        `shouldRespondWith`
        [str|name,id,client_id
            |"Windows 10",2,1|]
        { matchStatus  = 200
        , matchHeaders = ["Content-Type" <:> "text/csv; charset=utf-8"]
        }

A test for RPCs:

      it "returns CSV with custom select" $
        request methodGet "/rpc/getproject?select=name,id,client_id&id=2"
                (acceptHdrs "text/csv") ""
          `shouldRespondWith`
          [str|name,id,client_id
              |"Windows 10",2,1|]
          { matchStatus  = 200
          , matchHeaders = ["Content-Type" <:> "text/csv; charset=utf-8"]
          }

The first test passes, the second test fails:

  1) Feature.Query.RpcSpec, remote procedure call, a proc that returns a set, returns CSV with custom select
       body mismatch:
         expected: "name,id,client_id\n\"Windows 10\",2,1"
         but got:  "client_id,id,name\n\"Windows 10\",2,1"

To get the wrong output you need:

The values will be returned in the correct order, but the headers will always be sorted alphabetically.

bjornharrtell commented 4 months ago

Thank you @wolfgangwalther, this is precisely the issue.