strapi icon indicating copy to clipboard operation
strapi copied to clipboard

JSON type columns are returned as string

Open dsolay opened this issue 2 years ago • 14 comments
trafficstars

Bug report

Required System information

  • Node.js version: 18.17.1
  • YARN version: 1.22.19
  • Strapi version: 4.13.6
  • Database: Postgres 15.3
  • Operating system: ArchLinux x64, Kernel 6.4.12-arch1-1
  • Is your project Javascript or Typescript: Typescript

Describe the bug

I updated to the latest version of strapi and realized that several of my endpoints were returning json data as strings instead of objects

Doing a little research I noticed this PR where the automatic conversion of JSONB type columns is disabled

I know I can perform a JSON.parse(result) on each of my endpoints but there are too many of them and I think this should be marked as a breaking change in the release notes

Steps to reproduce the behavior

  1. Start strapi console
  2. Exec this code
let result = await strapi.db.connection.raw("SELECT jsonb_build_object('key', 1) AS jsonb_column")
result.rows

Expected behavior

The result of the previous query should be an object instead of a string

  • Actual result
[ { jsonb_column: '{"key": 1}' } ]
  • Expected result
[ { jsonb_column: { key: 1 } } ]

dsolay avatar Sep 14 '23 23:09 dsolay

sir can you please assign this issue to me :)

AnkurDhattarwal avatar Sep 30 '23 17:09 AnkurDhattarwal

can you assign me this issue

avdhendra avatar Sep 30 '23 20:09 avdhendra

I've applied PR #18215 in my local installation and it fixes the issue for me. I completely broke my Gatsby app when updated Strapi from 4.12.x to 4.14.x.

gentakojima avatar Oct 03 '23 12:10 gentakojima

@Feranchz since you self assigned yourself to this issue mind filling out the labels since I don't know what severity you want.

Boegie19 avatar Oct 03 '23 17:10 Boegie19

Hey, anyone up who can review my PR #18215, which may close this issue.

rishabh1S avatar Oct 04 '23 17:10 rishabh1S

@rishabh1S I assume it has been done for a reason so we need to wait for @Feranchz Who assigned himself to the issue and created the problem. so he would need to review your solution.

Boegie19 avatar Oct 04 '23 17:10 Boegie19

@Boegie19 Sure thing man!

rishabh1S avatar Oct 04 '23 18:10 rishabh1S

Hey @dsolay, thank you so much for reporting this and sorry for the late response.

I would appreciate if you can give me more context about your application and when is happening this error to you. I did try and I'm always receiving an object when fields are JSON values. The reason why Postgres' automatic conversion of JSONB fields is disabled is because we are parsing json fields by ourself here (In the PR you mentioned there is a more clear explanation about this). So if we enabled the automatic conversion you are parsing the field two times.

Feranchz avatar Oct 05 '23 07:10 Feranchz

Hey @dsolay, thank you so much for reporting this and sorry for the late response.

I would appreciate if you can give me more context about your application and when is happening this error to you. I did try and I'm always receiving an object when fields are JSON values. The reason why Postgres' automatic conversion of JSONB fields is disabled is because we are parsing json fields by ourself here (In the PR you mentioned there is a more clear explanation about this). So if we enabled the automatic conversion you are parsing the field two times.

Looking at the initial issue report he is using a raw db connection and that is what we changed

Boegie19 avatar Oct 05 '23 09:10 Boegie19

Apologies for the delayed response. As mentioned by @Boegie19, we utilize raw queries in our project.

Additional context: We have a dashboard that displays various metrics, such as orders by period, revenue, shipments, drayage by day, etc. To avoid making separate entity service calls for each metric, I constructed a single query and am utilizing the knex instance provided by strapi.db.connection to execute a raw query.

Before that pull request, everything worked fine

dsolay avatar Oct 10 '23 14:10 dsolay

I just wanted to mention that this issue also came up with a previous release (v4.6.2): https://github.com/strapi/strapi/issues/16073

This also effects our app, as we use the raw knex instance for doing some queries.

bozz avatar Oct 12 '23 11:10 bozz

Any updates on this and how this will be handled in the future? Can the knex instance be used directly in Strapi with the standard knex behaviour (i.e. JSON columns are returned as parsed JSON)?

bozz avatar Nov 06 '23 16:11 bozz

Is it going to be solved anytime soon?

talhayoun avatar Feb 19 '24 12:02 talhayoun

Hi Strapi Team,

Thank you for your project.

I encountered the same issue while developing my strapi plugin. I'm using the pg-boss library to schedule jobs, which relies on the pg library to retrieve job data. Since jsonb data comes as a string, it breaks the pg-boss logic and causes the jobs to fail. I applied #18215 in my local installation, and it fixed the issue for me as well.

As a workaround in my plugin's bootstrap.ts, I did this: */my-plugin/server/bootstrap.ts:

function fixStrapiJsonbParser(strapi: any) {
  strapi.db.connection.client.driver.types.setTypeParser(
    strapi.db.connection.client.driver.types.builtins.JSONB,
    "text",
    (v) => {
      try {
        return JSON.parse(v);
      } catch (error) {
        return v;
      }
    }
  );
}

export default async ({ strapi }: { strapi: Strapi }) => {
   // Temporary fix for pg JSONB parsing (see https://github.com/strapi/strapi/issues/18073)
  fixStrapiJsonbParser(strapi);
};

dimhold avatar Jun 26 '24 16:06 dimhold