Skip to content

Default value of null in an union schema causes panic. #1

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

Closed
amrx101 opened this issue Oct 12, 2020 · 4 comments · Fixed by #8
Closed

Default value of null in an union schema causes panic. #1

amrx101 opened this issue Oct 12, 2020 · 4 comments · Fixed by #8
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@amrx101
Copy link

amrx101 commented Oct 12, 2020

When we have a union type with null and a default value of null, the serialisation causes error at run time.

The schema. Here in we can see that we have union type with null and default value null.

{
	"namespace": "atherenergy.vcu_cloud_connect",
    "type": "record",
	"name": "common",
	"fields" : [
		{"name": "can_id", "type": ["null", "string"], "default": null},
        {"name": "data", "type": ["null", "string"], "default": null},
        {"name": "value", "type": ["null", "string"], "default": null},
        {"name": "key", "type": ["null", "string"], "default": null},
        {"name": "timestamp", "type": ["null", "double"], "default": null},
        {"name": "start_timestamp", "type": ["null", "string"],"default": null },
        {"name": "end_timestamp", "type": ["null", "string"], "default": null},
        {"name": "release_name", "type": ["null", "string"], "default": null},
        {"name": "bms_version", "type":["null", "string"], "default": null},
        {"name": "charger_version", "type":["null","string"], "default": null},
        {"name": "sim_ccid", "type":["null", "string"], "default": null},
        {"name": "sim_cimi", "type":["null", "string"], "default": null},
        {"name":"mender_artifact_ver", "type":["null", "string"], "default": null},
        {"name":"mcu_version", "type": ["null","string"], "default": null},
        {"name": "vin", "type": ["null","string"], "default": null},
        {"name": "bike_type", "type": ["null","string"], "default": null},
        {"name": "motor_version", "type": ["null","string"], "default": null},
        {"name": "system_boot_time", "type": ["null","string"], "default": null},
        {"name": "mode", "type": ["null","int"], "default": null},
        {"name": "ttff_s", "type": ["null","float"], "default": null},
        {"name": "error_code", "type": ["null","string"],  "default": null},
        {"name": "is_valid", "type": ["null","int"],  "default": null},
        {"name": "ACC_X_MPS2", "type": ["null","float"],  "default": null},
		{"name": "ACC_Y_MPS2", "type": ["null","float"],  "default": null},
		{"name": "ACC_Z_MPS2", "type": ["null","float"],  "default": null},
		{"name": "GYR_X_DEG", "type": ["null","float"],  "default": null},
		{"name": "GYR_Y_DEG", "type": ["null","float"],  "default": null},
        {"name": "GYR_Z_DEG", "type": ["null","float"],  "default": null}
	]
}
let sample_data = r#"{
            "ACC_X_MPS2": "33213.322",
            "ACC_Y_MPS2": "323.909803",
            "ACC_Z_MPS2": "2121.443",
            "GYR_X_DEG": "2323.111",
            "GYR_Y_DEG": "223.11274",
            "GYR_Z_DEG": "3434.2211"
        }"#;
        let sample_imu = serde_json::from_str(imu_data).unwrap();
        println!("{:?}", sample_imu);

        let rec = Rr::from_json(sample_imu, &ss).unwrap();

This serialisation results in Err` value: DefaultValueParse

@creativcoder
Copy link
Owner

creativcoder commented Oct 13, 2020

Hi @amrx101

Thanks for reporting this. Looks like we need to put priority on iterating over the schema fields when creating fields of the Record instance. Right now from_json iterates over the fields on the provide json value.

Here's how we might solve this:

iterate over the schema fields,
         if the field is in json - use that field
         else parse value if there's a default value in schema.
else we bail with an error.

Code:

pub fn from_json(

Note: The schema might also be a union so we need to take into account of trying all the variants in the union schema when parsing default value from schema, as is the case with your example.

Wanna make a PR for this?

@creativcoder creativcoder added bug Something isn't working good first issue Good for newcomers labels Oct 13, 2020
@amrx101
Copy link
Author

amrx101 commented Oct 13, 2020

Yup will make a PR for that.

@creativcoder
Copy link
Owner

@amrx101 Any update on this?

@amrx101
Copy link
Author

amrx101 commented Oct 20, 2020

Not yet. Bit busy here due to the releases coming in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants