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 not handles null value in the attributes of JSON payload. #10658

Closed
1 task done
arunpk-zageno opened this issue Apr 12, 2023 · 10 comments
Closed
1 task done

KONG not handles null value in the attributes of JSON payload. #10658

arunpk-zageno opened this issue Apr 12, 2023 · 10 comments

Comments

@arunpk-zageno
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Kong version ($ kong version)

Kong 2.8

Current Behavior

When I hit a Http Request with a payload that includes
{ "attribute" : null }

then Kong throws below error.

2023/03/17 05:34:36 [error] 1114#0: *226732 [kong] init.lua:297 [grpc-gateway] /usr/local/share/lua/5.1/kong/plugins/grpc-gateway/deco.lua:247: bad argument #2 to 'encode' (string expected for field 'object_type', got userdata), client: 172.19.0.1, server: kong, request: "PUT /api/v1/products HTTP/1.1", host: "localhost:8082"

Expected Behavior

When I hit a Http Request with a payload that includes
{ "attribute" : null }

It should not throw any error as it is valid JSON.

Steps To Reproduce

No response

Anything else?

No response

@ghost
Copy link

ghost commented Apr 12, 2023

can you show your proto file?

@arunpk-zageno
Copy link
Author

syntax = "proto3";

package stepeventparserservice.model.v1;

option java_multiple_files = true;
option java_package = "com.zageno.stepeventparser.services";

message ProductParserServiceProcessRequest {
ProductPayload payload = 1;
}

message ProductPayload {
Products products = 1;
}

message Products {
repeated Product updates = 1;
repeated DeleteProduct deletes = 2;
}

message Product {
string id = 1;
string name = 2;
string object_type = 3;
repeated Values values = 4;
repeated Suppliers suppliers = 5;
Assets assets = 6;
repeated ReferencedProducts referenced_products = 7;
MasterProductFamily master_product_family = 8;
repeated WebHierarchies web_hierarchies = 9;
PrimaryProductHierarchy primary_product_hierarchy = 10;

message Values {
string attribute_id = 1;
string attribute_name = 2;
string attribute_value = 3;
}

message Suppliers {
string supplier_id = 1;
string supplier_name = 2;
string supplier_catalog_id = 3;
string min_order_quantity = 4;
string pygeno_region_id = 5;
repeated Prices prices = 6;

message Prices {
  string price_id = 1;
  string price_name = 2;
  string price_value = 3;
}

}

message Assets {
repeated Images images = 1;
repeated Resources resources = 2;
message Images {
string asset_id = 1;
string asset_name = 2;
string asset_value = 3;
}
message Resources {
string asset_id = 1;
string asset_name = 2;
string asset_value = 3;
}
}

message ReferencedProducts {
string id = 1;
string type = 2;
}

message MasterProductFamily {
string attribute_id = 1;
string attribute_name = 2;
string attribute_value = 3;
repeated string variant_defining_attribute = 4;
repeated string summary_attributes = 5;
}

message WebHierarchies {
string attribute_value = 1;
}

message PrimaryProductHierarchy {
string attribute_value = 1;
}
}

message DeleteProduct {
string id = 1;
}

@ghost
Copy link

ghost commented Apr 12, 2023

string expected for field 'object_type', got userdata

string object_type = 3;

object_type is expected to be of string type, but it is receiving a userdata type. This may be because you did not pass the object_type parameter, so it is actually nil. The userdata here indicates that Kong has correctly handled null as cjson.null.

Perhaps you should pass the object_type parameter?

@arunpk-zageno
Copy link
Author

Let me give you another example.

Proto:

syntax = "proto3";

package stepeventparserservice.model.v1;

option java_multiple_files = true;
option java_package = "com.zageno.stepeventparser.services";


message PPHParserServiceProcessRequest {
  PPHPayload payload = 1;
}

message PPHPayload {
  PPHs pph = 1;
}

message PPHs {
  repeated PPH updates = 1;
  repeated DeletePPH deletes = 2;
}

// PPH
message PPH {
  string step_id = 1;
  string step_parent_id = 2;
  string name = 3;
}

// DELETE PPH
message DeletePPH {
  string id = 1;
}

Payload which faces 500 error from KONG

{
    "pph": {
        "updates": [
            {
                "step_id": "INT.L3-222",
                "step_parent_id": "INT.L2-64",
                "name": null
            }
        ],
        "deletes": []
    }
}

Error
2023/04/12 19:14:44 [error] 1113#0: *3767 [kong] init.lua:297 [grpc-gateway] /usr/local/share/lua/5.1/kong/plugins/grpc-gateway/deco.lua:247: bad argument #2 to 'encode' (string expected for field 'name', got userdata), client: 172.19.0.1, server: kong, request: "PUT /api/v1/products HTTP/1.1", host: "localhost:8082"

Payload which is successfully accepted

{
    "pph": {
        "updates": [
            {
                "step_id": "INT.L3-222",
                "step_parent_id": "INT.L2-64",
                "name": ""
            }
        ],
        "deletes": []
    }
}

@ghost
Copy link

ghost commented Apr 13, 2023

Payload which is successfully accepted

because "name": "" here "" is an empty string value, it matches the string name defined as a string value, but name: null not a string, in Kong, it is converted null in json of request body to cjson.null, which is not a string but a userdata.

I think your question is why Kong did not escape null to "", which is the handling method of the OpenResty platform. You need to use '""' instead of null in json.

@arunpk-zageno
Copy link
Author

arunpk-zageno commented Apr 13, 2023

So sending null to string type is invalid? But is it valid to send null to object type?

@ghost
Copy link

ghost commented Apr 13, 2023

I think you can test by not passing the name parameter directly and see if the name received by gRPC is null.

So sending null to string type is invalid? But is it valid if we have data type as object and sends null to it?

I don't know.

@oowl
Copy link
Member

oowl commented Apr 13, 2023

No matter what the client sends, Kong should not return 500, It's an edge case bug. But for this problem, I think you can change to "name": "" workaround. cjson.null != "" in Kong.

@ghost
Copy link

ghost commented Apr 14, 2023

No matter what the client sends, Kong should not return 500, It's an edge case bug. But for this problem, I think you can change to "name": "" workaround. cjson.null != "" in Kong.

Do we need to fix it?Is so, I'd like to try.

@arunpk-zageno
Copy link
Author

@sabertobihwy I would prefer to see a fix from KONG, because we use external system to get the input payload, which we cannot customize it easily to avoid sending null or manipulate to empty string "".

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

No branches or pull requests

3 participants