mulesoft-labs / data-weave-cli

DataWeave CLI and Native Library
BSD 3-Clause "New" or "Revised" License
97 stars 31 forks source link

substring fails depending on whether we log the contents of a variable (works) or not (does not work) #64

Closed julian-miras closed 1 year ago

julian-miras commented 1 year ago

Describe the bug It looks like the var fileName (see script below) is like a “promise” ? so when we force it to be logged (line 8 , it gets instantiated and therefore the substring works, but when we don’t enable the log line, then the substring finds the “promise” and it fails? Forgive me if my usage of “promise” is not adequate here, but I meant to say that the instantiation of the variable hasn’t really happened when the substring takes action?

To Reproduce

This is the script:

import substring from dw::core::Strings

output text/plain

fun getFileName(instance: Object) : String = do {

    var fileName = (("./filePrefix_") ++ (instance.id as String) ++ (".json") ) as String
    //var dummy = log(fileName)
    var fileURI = ("file://" ++ substring(fileName,1,sizeOf(fileName))) as String
    ---
    fileURI
}

---
getFileName({id:912345})

if you run the previous code, you get an exception:

[ERROR] Error while executing the script:
[ERROR] Internal error : java.lang.StringIndexOutOfBoundsException : begin 0, end -2, length 5
java.lang.StringIndexOutOfBoundsException: begin 0, end -2, length 5
        at java.base@11.0.17/java.lang.String.checkBoundsBeginEnd(String.java:3319)
        at java.base@11.0.17/java.lang.String.substring(String.java:1874)
        at java.base@11.0.17/java.lang.String.subSequence(String.java:1913)
        at org.mule.weave.v2.model.structure.SliceCharSequence.subSequence(SliceCharSequence.scala:90)
        at org.mule.weave.v2.runtime.core.operator.selectors.StringRangeSelectorOperator.doExecute(RangeSelectorOperator.scala:118)
        at org.mule.weave.v2.core.functions.BaseBinaryFunctionValue.call(BinaryFunctionValue.scala:40)
        at org.mule.weave.v2.core.functions.BaseBinaryFunctionValue.call$(BinaryFunctionValue.scala:39)

But if you uncomment the line 8 ( //var dummy = log(fileName))

import substring from dw::core::Strings

output text/plain

fun getFileName(instance: Object) : String = do {

    var fileName = (("./filePrefix_") ++ (instance.id as String) ++ (".json") ) as String
    var dummy = log(fileName)
    var fileURI = ("file://" ++ substring(fileName,1,sizeOf(fileName))) as String
    ---
    fileURI
}

---
getFileName({id:912345})

The script runs without problems showing the log entry:

dw -f dw/testIssue.dwl
"./filePrefix_912345.json"
file:///filePrefix_912345.json

This behaviour has started to happen after upgrading to CLI version V1.0.28. My previous version was V1.0.25 and it didn't happen.

Expected behavior The substring function should work regardless of whether we log the content of the variable or not

Desktop (please complete the following information):

machaval commented 1 year ago

Hi Julian, Thanks for reporting this issue we will take a look at it.

julian-miras commented 1 year ago

No problem. Thanks @machaval

machaval commented 1 year ago

Hi We have just merged the fix. So I will make a new release of the cli this week for you to try out.

julian-miras commented 1 year ago

Thanks @machaval for the update.

machaval commented 1 year ago

Fixed on latest version