Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to handle nested JSON objects #8

Open
swinton opened this issue Jan 18, 2022 · 2 comments 路 May be fixed by #9
Open

Unable to handle nested JSON objects #8

swinton opened this issue Jan 18, 2022 · 2 comments 路 May be fixed by #9

Comments

@swinton
Copy link

swinton commented Jan 18, 2022

馃憢 I'm using xcall with Bear's x-callback-url scheme, documented here.

Mostly this is working great, but with one exception: Where Bear's x-success contains nested JSON objects, for example the todo action (which returns an array of objects), xcall fails to properly JSON encode this data.

Example

# search for a todo containing 'hereismytodo'
xcall -url "bear://x-callback-url/todo?token=$BEAR_TOKEN&search=hereismytodo"

Returns the following (note how the notes property is a JSON escaped string):

{
  "notes" : "[{\"creationDate\":\"2022-01-18T21:43:37Z\",\"tags\":\"[]\",\"title\":\"- [ ] hereismytodo\",\"modificationDate\":\"2022-01-18T21:43:44Z\",\"identifier\":\"143D2519-2F4A-49C3-AA20-8A796673D036-70083-0003798CC0EB8A8E\",\"pin\":\"no\"}]",
  "" : null
}

Instead, the following should be returned:

{
  "notes": [
    {
      "creationDate": "2022-01-18T21:43:37Z",
      "tags": "[]",
      "title": "- [ ] hereismytodo",
      "modificationDate": "2022-01-18T21:43:44Z",
      "identifier": "143D2519-2F4A-49C3-AA20-8A796673D036-70083-0003798CC0EB8A8E",
      "pin": "no"
    }
  ]
}

It looks like the source of the issue is in jsonStringFromQueryItems, where it appears to assume the object is a simple set of key/value pairs:

xcall/xcall/AppDelegate.m

Lines 111 to 122 in 0d61fea

+ (NSString *)jsonStringFromQueryItems:(NSArray<NSURLQueryItem *> *)queryItems
{
NSMutableDictionary *items = [NSMutableDictionary new];
// Convert each query item to a key/value pair
for (NSURLQueryItem *queryItem in queryItems)
items[queryItem.name] = queryItem.value ?: NSNull.null;
// Convert to JSON string
NSData *data = [NSJSONSerialization dataWithJSONObject:items options:NSJSONWritingPrettyPrinted error:NULL];
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

I would love to PR this, but I'm not familiar with Objective-C 馃

/cc @pr1mal -- wondering if you've seen this / can provide guidance given your work in #6 馃檱

@swinton
Copy link
Author

swinton commented Jan 20, 2022

It looks like the source of the issue is in jsonStringFromQueryItems, where it appears to assume the object is a simple set of key/value pairs:

Looking at this a bit more closely, I believe the source of this issue is here:

xcall/xcall/AppDelegate.m

Lines 115 to 117 in 0d61fea

// Convert each query item to a key/value pair
for (NSURLQueryItem *queryItem in queryItems)
items[queryItem.name] = queryItem.value ?: NSNull.null;

queryItem.value may contain a JSON-encoded value (as is the case with Bear's /todo x-success callback value) which should first be parsed before being added to items[queryItem.name]. If this fails, then as a fallback, just the pure queryItem.value should be added.

@swinton swinton linked a pull request Jan 24, 2022 that will close this issue
@swinton
Copy link
Author

swinton commented Jan 24, 2022

Potential fix for this up in #9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant