Parse-SDK-Flutter icon indicating copy to clipboard operation
Parse-SDK-Flutter copied to clipboard

Querying Relationships Generates Invalid JSON

Open founderblocks-sils opened this issue 1 year ago • 6 comments

New Issue Checklist

^ note I'm using the latest parse server version available on back4app and the latest SDK available for flutter

Issue Description

Steps to reproduce

  1. Create two classes with a n:n relationship
  2. Create a query for one of those classes
  3. Create a query for the other class with a "whereMatchesIn" clause
  4. Run the query

This is the code from my app:

var deliveryAreasQuery = QueryBuilder(ParseObject('DeliveryArea'))
      ..whereArrayContainsAll('postalCodes', [postalCode]);

    var query = QueryBuilder(ParseObject('Farmer'))
      ..whereMatchesQuery('deliveryAreas', deliveryAreasQuery)
      ..whereEqualTo('isActive', true)
      ..includeObject(['pickupAddress', 'invoiceAddress']);

Note that the other constraints don't do anything to the reproduction. The whereMatchesQuery made the issue appear.

Actual Outcome

Internal Server Error

Upon further investigation, I found that this request URL was generated by the library:

https://parseapi.back4app.com/classes/Farmer?where=%7B%22deliveryAreas%22:%7B%22$inQuery%22:%7B%22where%22:%7B%22postalCodes%22:%7B%20%22$all%22:%5B21075%5D%7D%7D,%22className%22:%22DeliveryArea%22,%7D%7D,%22isActive%22:%20true,%22pickupCoordinates%22:%7B%22$nearSphere%22:%7B%22__type%22:%22GeoPoint%22,%22latitude%22:53.45994049999999,%22longitude%22:9.9288308%7D%7D%7D&include=pickupAddress,invoiceAddress

The decoded JSON part of the query is:

{
  "deliveryAreas": {
    "$inQuery": {
      "where": {
        "postalCodes": {
          "$all": [
            21075
          ]
        }
      },
      "className": "DeliveryArea",
      
    }
  },
  "isActive": true,
  "pickupCoordinates": {
    "$nearSphere": {
      "__type": "GeoPoint",
      "latitude": 53.45994049999999,
      "longitude": 9.9288308
    }
  }
}

NOTE THE TRAILING COMMA AFTER THE className! This is not valid json!

Expected Outcome

Send valid JSON, get the data :)

That's what the previous version did before the upgrade.

Environment

Parse Flutter SDK

  • SDK version: parse_server_sdk_flutter: ^5.1.0
  • Flutter version: 3.10.2
  • Dart version: 3.0.2
  • Operating system version: linux/ubuntu

Server

  • Parse Server version: tested on 5.2.3, 4.10.4, and 4.5.0

Logs

2023-06-03T20:51:01.484Z - 
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:558:11)
    at ServerResponse.header (/usr/src/app/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/usr/src/app/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/usr/src/app/node_modules/express/lib/response.js:267:15)
    at /usr/src/app/src/back/app.js:287:9
    at Layer.handle_error (/usr/src/app/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/usr/src/app/node_modules/express/lib/router/index.js:315:13)
    at /usr/src/app/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/usr/src/app/node_modules/express/lib/router/index.js:335:12)
    at next (/usr/src/app/node_modules/express/lib/router/index.js:275:10)
    at Layer.handle_error (/usr/src/app/node_modules/express/lib/router/layer.js:67:12)
    at trim_prefix (/usr/src/app/node_modules/express/lib/router/index.js:315:13)
    at /usr/src/app/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/usr/src/app/node_modules/express/lib/router/index.js:335:12)
    at next (/usr/src/app/node_modules/express/lib/router/index.js:275:10)
    at Immediate.<anonymous> (/usr/src/app/node_modules/express/lib/application.js:233:9)
    at Immediate._onImmediate (/usr/src/app/node_modules/express/lib/router/index.js:635:15)
    at processImmediate (internal/timers.js:463:21)
2023-06-03T20:51:01.482Z - Uncaught internal server error. Unexpected token } in JSON at position 99
2023-06-03T20:51:01.481Z - Error handling request: SyntaxError: Unexpected token } in JSON at position 99
    at JSON.parse (<anonymous>)
    at ClassesRouter.handleFind (/usr/src/app/node_modules/parse-server/lib/Routers/ClassesRouter.js:41:25)
    at /usr/src/app/node_modules/parse-server/lib/Routers/ClassesRouter.js:210:19
    at /usr/src/app/node_modules/parse-server/lib/PromiseRouter.js:175:7
    at Layer.handle [as handle_request] (/usr/src/app/node_modules/express/lib/router/layer.js:95:5)
    at next (/usr/src/app/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/usr/src/app/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/usr/src/app/node_modules/express/lib/router/layer.js:95:5)
    at /usr/src/app/node_modules/express/lib/router/index.js:281:22
    at param (/usr/src/app/node_modules/express/lib/router/index.js:354:14)
    at param (/usr/src/app/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/usr/src/app/node_modules/express/lib/router/index.js:410:3)
    at next (/usr/src/app/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/usr/src/app/node_modules/express/lib/router/index.js:174:3)
    at router (/usr/src/app/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/usr/src/app/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/usr/src/app/node_modules/express/lib/router/index.js:317:13)
    at /usr/src/app/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/usr/src/app/node_modules/express/lib/router/index.js:335:12)
    at next (/usr/src/app/node_modules/express/lib/router/index.js:275:10)
    at /usr/src/app/node_modules/parse-server/lib/middlewares.js:256:7
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

founderblocks-sils avatar Jun 03 '23 21:06 founderblocks-sils

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

https://github.com/parse-community/Parse-SDK-Flutter/pull/869/files possibly related, but I believe I already use parse_server_sdk 5.1.2 with parse_server_sdk_flutter 5.1.0

founderblocks-sils avatar Jun 03 '23 21:06 founderblocks-sils

Yep, my lockfile is on parse_server_sdk 5.1.2 which should contain that pull request already if I understand correctly

founderblocks-sils avatar Jun 03 '23 21:06 founderblocks-sils

image

def. shows 5.1.2 but doesn't seem to contain the fix. Cleaning that cache (flutter clean) and redoing pub get doesn't help.

founderblocks-sils avatar Jun 03 '23 21:06 founderblocks-sils

In case anyone finds this and has the same issue, I'm using a workaround where I just add a skip 0 constraint to the whereMatchesQuery, that way we get valid JSON with the same result

founderblocks-sils avatar Jun 04 '23 17:06 founderblocks-sils

I think this issue is from this PR #869. I have not checked the issue yet, but just in case, if someone can open a PR to fix this issue.

I would like to help with fixing it, but I don't have any free time at the moment because I am currently so busy with work and college exams.

Nidal-Bakir avatar Jun 05 '23 21:06 Nidal-Bakir