aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

aws-stepfunctions: State.jsonpath() silently converts to JSONata when state machine query language is set to JSONata

Open Kasra-G opened this issue 1 year ago • 5 comments

Describe the bug

Currently, creating any state via the JsonPath factory method, for example, sfn.Pass.jsonpath(stack, 'id') does not throw an error when it is used in a state machine with QueryLanguage.JSONATA set at the top level. This is because the state is actually being interpreted as JSONata.

This results in unintuitive situations where you think you are setting JsonPath properties but are actually creating a JSONata state. This results in time wasted waiting for the cloudformation deployment to fail or for the state machine execution to fail. CDK should throw an error if you are using the jsonpath() factory method on a 'JSONata' state machine.

Regression Issue

  • [ ] Select this option if this issue appears to be a regression.

Last Known Working CDK Library Version

No response

Expected Behavior

Creating a state via state.jsonpath() should throw an error if it is attached to a state machine with JSONata set at the top level.

Current Behavior

The state created via state.jsonpath will silently turn into a JSONata state instead.

Reproduction Steps

const stack = new cdk.Stack();
const map = sfn.DistributedMap.jsonPath(stack, 'Map', {
  // queryLanguage: sfn.QueryLanguage.JSON_PATH, // Setting this throws an error as is expected
  maxConcurrency: 1,
  itemsPath: sfn.JsonPath.stringAt('$.inputForMap'),
  itemSelector: {
    foo: 'foo',
    bar: sfn.JsonPath.stringAt('$.bar'),
  },
});
map.itemProcessor(new sfn.Pass(stack, 'State'));

// Should error but it doesn't! Silently turns our JsonPath distributed map above into a JSONata one!
new sfn.StateMachine(stack, 'StateMachine', {
  queryLanguage: sfn.QueryLanguage.JSONATA,
  definitionBody: sfn.DefinitionBody.fromChainable(map),
);

Possible Solution

Set queryLanguage: sfn.QueryLanguage.JSON_PATH instead of leaving it blank: https://github.com/aws/aws-cdk/blob/cecbabfc1944ce301d89f38dddd5f2f190c7e2d9/packages/aws-cdk-lib/aws-stepfunctions/lib/states/distributed-map.ts#L152

This goes for every other state with this factory method

This would unfortunately need to go behind a feature flag, since throwing an error when there might not have been one before is a breaking change.

Note that the logic for throwing the error already exists here: https://github.com/aws/aws-cdk/blob/cecbabfc1944ce301d89f38dddd5f2f190c7e2d9/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts#L588

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

2.214

AWS CDK CLI version

2.1029.0

Node.js Version

24

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

Kasra-G avatar Sep 11 '25 07:09 Kasra-G

A few other inconsistencies I have noticed:

  • branches do not respect top level state machine query language
  • item processors do not respect top level state machine query language

These could technically create invalid cfn templates, as mentioned here: https://github.com/aws/aws-cdk/issues/35403

Kasra-G avatar Sep 11 '25 08:09 Kasra-G

Hi @Kasra-G,

Thank you for reporting this issue! You've identified an important validation gap in our Step Functions constructs.

The problem occurs because the DistributedMap.jsonPath() factory method doesn't explicitly set queryLanguage: QueryLanguage.JSON_PATH. The existing validation logic in State.renderQueryLanguage() only triggers when there's an explicit mismatch between state-level and state machine-level query languages.

Immediate Workaround: Until this is fixed, you can work around the issue by explicitly setting the query language:

// Instead of using the factory method
const map = sfn.DistributedMap.jsonPath(stack, 'Map', { ... });

// Use the constructor with explicit queryLanguage
const map = new sfn.DistributedMap(stack, 'Map', {
  queryLanguage: sfn.QueryLanguage.JSON_PATH, // Explicit setting
  maxConcurrency: 1,
  itemsPath: sfn.JsonPath.stringAt('$.inputForMap'),
  itemSelector: {
    foo: 'foo',
    bar: sfn.JsonPath.stringAt('$.bar'),
  },
});

This will trigger the existing validation and give you the error you expect when used with a JSONata state machine.

Your suggested solution is correct - we need to modify all jsonPath() factory methods to explicitly set the query language. This would need to be implemented behind a feature flag as you mentioned, since it represents a breaking change for existing code that might be silently working.

This issue affects multiple state types beyond DistributedMap, as you noted in your comment about branches and item processors not respecting top-level query language settings.

Would you be interested in contributing a PR to fix this? The implementation would involve:

  1. Modifying factory methods to set explicit query language
  2. Adding appropriate feature flag handling
  3. Updating tests to cover the validation scenarios

We encourage community contributions and would be happy to guide you through the process!

pahud avatar Sep 11 '25 16:09 pahud

Sure, I can contribute a PR for this.

Kasra-G avatar Sep 11 '25 16:09 Kasra-G

I am currently having the opposite problem, of the same issue.

aws-cdk-lib: 2.122.0 cdk_cli:2.1020.2

I am also using distributed map but i made my first solution with clickops using JSONata.

Basically I wanted to create distributed map that would call ECS fargate task to run with some overrides to split work for multiple tasks and i am getting following error from cloudformation:

"Invalid State Machine Definition: 'SCHEMA_VALIDATION_FAILED: The QueryLanguage is set to 'JSONata', but field 'Parameters' is only supported for the 'JSONPath' QueryLanguage at /States/Map/ItemProcessor/States/Geometryupdate....

I did set my query language to JSONATA as you can see, but it still fails because somewhere something sets it to JSONPath. here is the function that is causing the issue:

export function getGeoMetryUpdaterStepFunction(
  environment: string,
  taskDefF: ecs.FargateTaskDefinition,
  container: cdk.aws_ecs.ContainerDefinition,
  cluster: ecs.ICluster,
  thisconstruct: Construct,
  subnets: cdk.aws_ec2.ISubnet[],
  securityGroup: ec2.ISecurityGroup
): cdk.aws_stepfunctions.StateMachine {
  const mapState = new sfn.Map(thisconstruct, 'Map', {
    maxConcurrency: 10,
  });
   const ecsRunTask = new sfnTasks.EcsRunTask(thisconstruct, `Geometryupdate-ECSRunTask_${environment}`, {
    cluster,
    taskDefinition: taskDefF,
    launchTarget: new sfnTasks.EcsFargateLaunchTarget(),
    cpu: '256',
    memoryMiB: '2048',
    containerOverrides: [
      {
        containerDefinition: container,
        cpu: 256,
        memoryLimit: 1800, // Container memory limit (200mb reserved for xray)
        command: [
          'my commands',
        ],
      },
    ],
    subnets: { subnetType: cdk.aws_ec2.SubnetType.PRIVATE_ISOLATED },
    securityGroups: [securityGroup],
  });
  console.log(subnets);
  const stateMachine = new sfn.StateMachine(thisconstruct, 'StateMachine', {
    definition: mapState.itemProcessor(ecsRunTask),
    stateMachineName: `V-${environment}-geometryUpdater`,
    queryLanguage: sfn.QueryLanguage.JSONATA,
  });
  addRequiredPoliciesToGeometryUpdateStepfunction(stateMachine.role, thisconstruct, taskDefF);

  return stateMachine;

}

oappicgi avatar Sep 29 '25 13:09 oappicgi

fixed with same solution described above, defining query language on ecsRunTask in my case solved the issue. Thanks @pahud

oappicgi avatar Sep 29 '25 14:09 oappicgi