sdk-go icon indicating copy to clipboard operation
sdk-go copied to clipboard

Validation fails when transition.NextState is equal to currentState

Open Kshitiz1403 opened this issue 1 year ago • 5 comments

What happened: When doing state transitions, whenever the transition.NextState is defined as the currentState, the validation fails with the error Error:Field validation for 'NextState' failed on the 'recursivestate' tag"

What you expected to happen: The validation should not fail when transitioning to the same state.

How to reproduce it: Take for example this state

{
  "name": "FineUser",
  "type": "operation",
  "actions": [
    {
      "functionRef": {
        "refName": "FineUserFn"
      }
    }
  ],
  "transition": "CallExternalAPI",
  "onErrors": [
    {
      "errorRef": "ServicesNotAvailable",
      "transition": "FineUser"
    },
    {
      "errorRef": "DefaultErrorRef",
      "transition": "Reject"
    }
  ]
}

The validation here fails for onErrors[0] since it is looking to transition to the state FineUser which seems to be not handled in the SDK.

One more example of this is -

{
  "name": "UserActionSignal",
  "type": "switch",
  "eventConditions": [
    {
      "eventRef": "fine_user",
      "transition": "FineUser"
    },
    {
      "eventRef": "reward_user",
      "transition": "RewardUser"
    }
  ],
  "metadata": {
    "moduleNameTag": "USER_REWARD_INPUT",
    "moduleStatusTag": "WAIT"
  },
  "timeouts": {
    "eventTimeout": "PT5S"
  },
  "defaultCondition": {
    "transition": "UserActionSignal"
  }
}

here, the default transition is configured as UserActionSignal but since the nextState is same as the currentState, the validator throws the error.

Anything else we need to know?:

Environment:

  • SDK version: v2.2.5
  • Go version: go1.20.14 darwin/arm64

Kshitiz1403 avatar May 27 '24 11:05 Kshitiz1403

The validation here fails for onErrors[0] since it is looking to transition to the state FineUser which seems to be not handled in the SDK.

So where's the state FineUser? I can't see it in your excerpt.

here, the default transition is configured as UserActionSignal but since the nextState is same as the currentState, the validator throws the error.

So this will be an infinite loop, it's not valid.

ricardozanini avatar May 27 '24 16:05 ricardozanini

Handling for an infinite loop should be done from the application level. You can think of this as an "indefinite" retry which keeps on triggering this state till a certain result is achieved. Isn't this like just a classic recursion where you supply a base condition. The base condition in this case can be an event received, a signal received, etc. Ideally Go SDK should not be the one making the call to allow/disallow using of infinite loop.

Kshitiz1403 avatar May 27 '24 17:05 Kshitiz1403

The validation here fails for onErrors[0] since it is looking to transition to the state FineUser which seems to be not handled in the SDK.

So where's the state FineUser? I can't see it in your excerpt.

here, the default transition is configured as UserActionSignal but since the nextState is same as the currentState, the validator throws the error.

So this will be an infinite loop, it's not valid.

On line 1 of example 1, the state is called "FineUser"

Kshitiz1403 avatar May 27 '24 17:05 Kshitiz1403

@Kshitiz1403 A contribution in this scenario.

In the Monitor Job Example, it has a sleep state, avoiding a loop that can cause many calls, in a very short time for the function.

ribeiromiranda avatar May 28 '24 10:05 ribeiromiranda

@Kshitiz1403 I can see an appeal for this use case, which I overlooked yesterday. Feel free to send a PR, this is a valid DSL.

@ribeiromiranda in the case of their switch example, the engine should wait for 5 seconds and then transition again to the beginning of the node waiting for the event to arrive.

I would implement this using callbacks instead. But no matter what, the SDK should allow it.

ricardozanini avatar May 28 '24 14:05 ricardozanini

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Jul 13 '24 00:07 github-actions[bot]

Closed as stale

cdavernas avatar Jul 19 '24 06:07 cdavernas