ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.66k stars 748 forks source link

[Bug]: Invalid result for field access expression on optional record json field #38840

Open prakanth97 opened 1 year ago

prakanth97 commented 1 year ago

Description

import ballerina/io;

type FooRec1 record {
    json j;
};

type FooRec3 record {
    json j?;
    FooRec1 fooRec?;
};

public function main() returns error? {
    FooRec3 rec2 = {j: "1", fooRec: {j: {k: "3", l: {m: "4"}}}};
    json val = check rec2.fooRec?.j.k;
    io:println(val); // got {"k":"3","l":{"m":"4"}} which is wrong
}

expected result for val is 3 but got {"k":"3","l":{"m":"4"}}

Steps to Reproduce

No response

Affected Version(s)

Master 2201.3.0

OS, DB, other environment details and versions

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

chiranSachintha commented 1 year ago
import ballerina/io;

type FooRec1 record {
    map<json> j;
};

type FooRec3 record {
    FooRec1 fooRec?;
};

public function main() returns error? {
    FooRec3 rec2 = {fooRec: {j: {k: "3", l: {m: "4"}}}};
    json val = check rec2.fooRec?.j.k;

    io:println(val); // prints 3
}

If the field type is map<json>, the obtained output matches with the expected outcome.