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

[Bug]: Regular expression with toJson method panics #43360

Open
TharmiganK opened this issue Sep 5, 2024 · 1 comment
Open

[Bug]: Regular expression with toJson method panics #43360

TharmiganK opened this issue Sep 5, 2024 · 1 comment
Labels
Lang/LangLib Related to ballerina/lang.* modules Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug

Comments

@TharmiganK
Copy link
Contributor

Description

The toJson method is allowed in anydata types where a non-json types are converted to a respective json compatible types according to the method definition:

# This does a deep copy of parameter `v` converting values that do
# not belong to json into values that do.
# A value of type `xml` is converted into a string as if
# by the `toString` function.
# A value of type `table` is converted into a list of
# mappings one for each row.
# The inherent type of arrays in the return value will be
# `json[]` and of mappings will be `map<json>`.
# A new copy is made of all structural values, including
# immutable values.

But the regexp:RegExp with toJson method panics at runtime:

error: {ballerina/lang.value}ConversionError {"message":"'anydata & readonly' value cannot be converted to 'json'"}
        at ballerina.lang.value.0:toJson(value.bal:262)
           sample:main(sample.bal:7)

The method description should be updated if this is the correct behaviour or the method should return a json value

Steps to Reproduce

  • Simple regular expression with toJson
import ballerina/io;
import ballerina/lang.regexp;

regexp:RegExp regExp = re `a`;

public function main() {
    io:println(regExp.toJson());
}
  • Regular expression as a record field and record with toJson
import ballerina/io;
import ballerina/lang.regexp;

regexp:RegExp regExp = re `a`;

type Rec record {
    xml xmlValue;
    table<map<string>> tableValue;
};

public function main() {
    Rec rec = {
        xmlValue: xml `<name>John</name>`,
        tableValue: table [{"key": "value"}]
    };
    io:println(rec.toJson());

    rec["regExpValue"] = regExp;
    io:println(rec.toJson());
}

Affected Version(s)

Ballerina SwanLake Update 10(2201.10.x)

OS, DB, other environment details and versions

No response

Related area

-> Runtime

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@ballerina-bot ballerina-bot added the Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime label Sep 5, 2024
@SasinduDilshara SasinduDilshara added the Lang/LangLib Related to ballerina/lang.* modules label Sep 5, 2024
@MaryamZi
Copy link
Member

MaryamZi commented Sep 5, 2024

Related to #42070.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Lang/LangLib Related to ballerina/lang.* modules Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug
Projects
None yet
Development

No branches or pull requests

4 participants