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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kong decode json empty array to empty object? #3166

Closed
wuyachao opened this issue Jan 22, 2018 · 13 comments
Closed

kong decode json empty array to empty object? #3166

wuyachao opened this issue Jan 22, 2018 · 13 comments
Labels

Comments

@wuyachao
Copy link

wuyachao commented Jan 22, 2018

Summary

I want to change the body proxy kong. So I use the plugin response-transformer.But kong decode json empty array to empty object.

Steps To Reproduce

  1. add a api and plugin to kong. the config of the plugin is this:
{
    "data": [
        {
            "api_id": "d48f92ee-09dd-4970-8e7a-a853aeac30bd",
            "config": {
                "add": {
                    "headers": {},
                    "json": [
                        "age:111",
                        "age2:222"
                    ]
                },
                "append": {
                    "headers": {},
                    "json": {}
                },
                "remove": {
                    "headers": {},
                    "json": {}
                },
                "replace": {
                    "headers": {},
                    "json": {}
                }
            },
            "created_at": 1515747762000,
            "enabled": true,
            "id": "ea5ea2d2-3a3f-4a0a-a719-a41eb124b483",
            "name": "response-transformer"
        }
    ],
    "total": 1
}
  1. request this api:
curl -X POST \
  http://localhost:8000/notifi \
  -H 'content-type: application/json' \
  -d '{"name":"wyc","info":{},"list":[]}'
  1. the reponse is :
{
    "list": {},
    "info": {},
    "age2": "222",
    "age": "111",
    "name": "wyc"
}
  1. the list is empty array. it should be [],but the result is {}.

  2. The dkjson can do it. The master branch of openresty/cjson can do it too.It has the method decode_array_with_array_mt. But kong bundle openresty/1.11.2.4. So I only install the master cjson to replace the current cjson.

  3. Kong can resolve it the next version.May be the latest version is 1.13.6.1 also hasn't the method.
    attempt to call field 'decode_array_with_array_mt' (a nil value)? openresty/lua-cjson#33

Additional Details & Logs

  • Kong version :0.11.0 or 0.10.4
@kikito kikito added the task/needs-investigation Requires investigation and reproduction before classifying it as a bug or not. label Jan 22, 2018
@wuyachao
Copy link
Author

The request body have empty object and empty list is common. encode_empty_table_as_object can't resolve it. ecode_array_with_array_mt is in master branch, I must compile the master branch cjson. Maybe the latest version 1.13.6.1 also hasn't included the method.

@kikito kikito added task/feature Requests for new features in Kong and removed task/needs-investigation Requires investigation and reproduction before classifying it as a bug or not. labels Jan 24, 2018
@kikito
Copy link
Member

kikito commented Jan 24, 2018

Hi, I have investigated this and it is as you say: in order to fix this problem we need the latest cjson, which is not even included in openresty yet, as pointed in openresty/lua-cjson#33

I will leave this issue open but for now there's little we can do.

@kikito kikito added task/bug and removed task/feature Requests for new features in Kong labels Jan 24, 2018
@wuyachao
Copy link
Author

@kikito thank you. Now I have compiled it to replace the old cjson.so.

@erran
Copy link
Contributor

erran commented Jan 29, 2018

This seems related to #2309 which I was bit by today. I ended up updating my client code as described in #2309 (comment). Unfortunately doesn't look like this issue has the same type of work around.

also see #1208

@wuyachao
Copy link
Author

@erran From #2309 , We can know it will be empty array so encode it with cjson.empty_table.It's also harmless. But this response-transformer,we get data from front-bacnend and add some field.I do not know its format, It can cause backend error.

@erran
Copy link
Contributor

erran commented Jan 31, 2018

Unfortunately doesn't look like this issue has the same type of work around.

That's right @wuyachao. I was just commenting to reference related issues when I found #2309.

@maksimlikharev
Copy link

I see OpenResty incorporated the fix:
https://openresty.org/en/changelog-1013006.html

I don't think this is harmless, as the client expects [] and gets {} instead.

Any ETA on this issue?

Thanks

@bungle
Copy link
Member

bungle commented May 29, 2018

We will base our 0.14.0 on OpenResty that contains the fix.

@hbagdi
Copy link
Member

hbagdi commented Aug 24, 2018

I can reproduce this issue with 0.14.0.

@thibaultcha
Copy link
Member

Now that our underlying OpenResty version has the fix, we need to actually use it, which means updating the plugin. Our new Admin API already makes use of it along with a few plugins that received contributions to make use of the fix.

@hbagdi
Copy link
Member

hbagdi commented Aug 24, 2018

Now that our underlying OpenResty version has the fix, we need to actually use it, which means updating the plugin

@thibaultcha I suspected this but didn't actually look into it. Thanks for the quick response here!

@p0pr0ck5
Copy link
Contributor

Is this solved in the new DAO?

@hbagdi
Copy link
Member

hbagdi commented Jan 12, 2019

So, the new DAO returns an empty list for or an explicit null, based on the default:

{
    "config": {
        "blacklist": null,
        "hide_groups_header": false,
        "whitelist": [
            "foo"
        ]
    },
    "consumer": null,
    "created_at": 1547255075,
    "enabled": true,
    "id": "a43ddcb0-6559-4ed1-bab9-cda909c4584b",
    "name": "acl",
    "route": null,
    "run_on": "first",
    "service": null
}
        {
            "config": {
                "add": {
                    "body": [],
                    "headers": [
                        "fp:bar"
                    ],
                    "querystring": []
                },
                "append": {
                    "body": [],
                    "headers": [],
                    "querystring": []
                },
                "http_method": null,
                "remove": {
                    "body": [],
                    "headers": [],
                    "querystring": []
                },
                "rename": {
                    "body": [],
                    "headers": [],
                    "querystring": []
                },
                "replace": {
                    "body": [],
                    "headers": [],
                    "querystring": []
                }
            }

@hbagdi hbagdi closed this as completed Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants