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

fix(sfn): can't override toStateJson() from other languages #24593

Merged
merged 1 commit into from Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-stepfunctions/lib/fields.ts
@@ -1,4 +1,4 @@
import { Token, IResolvable } from '@aws-cdk/core';
import { Token, IResolvable, JsonNull } from '@aws-cdk/core';
import { findReferencedPaths, jsonPathString, JsonPathToken, renderObject, renderInExpression, jsonPathFromAny } from './private/json-path';

/**
Expand All @@ -9,9 +9,9 @@ import { findReferencedPaths, jsonPathString, JsonPathToken, renderObject, rende
*/
export class JsonPath {
/**
* Special string value to discard state input, output or result
* Special string value to discard state input, output or result.
*/
public static readonly DISCARD = 'DISCARD';
public static readonly DISCARD = Token.asString(JsonNull.INSTANCE, { displayHint: 'DISCARD (JSON `null`)' });

/**
* Instead of using a literal string, get the value from a JSON path
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-stepfunctions/lib/states/state.ts
@@ -1,3 +1,4 @@
import { Token } from '@aws-cdk/core';
import { IConstruct, Construct, Node } from 'constructs';
import { Condition } from '../condition';
import { FieldUtils, JsonPath } from '../fields';
Expand Down Expand Up @@ -579,7 +580,7 @@ export function renderJsonPath(jsonPath?: string): undefined | null | string {
if (jsonPath === undefined) { return undefined; }
if (jsonPath === JsonPath.DISCARD) { return null; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line might still need to be updated for this to work properly. Even though JsonPath.DISCARD has been updated to a JsonNull token, this line will still evaluate truthy and return null, which in other languages like Python would translate to undefined. Perhaps this should return a JsonNull token as well?


if (!jsonPath.startsWith('$')) {
if (!Token.isUnresolved(jsonPath) && !jsonPath.startsWith('$')) {
throw new Error(`Expected JSON path to start with '$', got: ${jsonPath}`);
}
return jsonPath;
Expand Down
30 changes: 30 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions/test/state.test.ts
@@ -0,0 +1,30 @@
import * as cdk from '@aws-cdk/core';
import { FakeTask } from './integ.state-machine-credentials';
import { renderGraph } from './private/render-util';
import { JsonPath } from '../lib';

test('JsonPath.DISCARD can be used to discard a state\'s output', () => {
const stack = new cdk.Stack();

const task = new FakeTask(stack, 'my-state', {
inputPath: JsonPath.DISCARD,
outputPath: JsonPath.DISCARD,
resultPath: JsonPath.DISCARD,
});

expect(renderGraph(task)).toEqual({
StartAt: 'my-state',
States: {
'my-state': {
End: true,
Type: 'Task',
Resource: expect.any(String),
Parameters: expect.any(Object),
// The important bits:
InputPath: null,
OutputPath: null,
ResultPath: null,
},
},
});
});
21 changes: 21 additions & 0 deletions packages/@aws-cdk/core/lib/token.ts
Expand Up @@ -232,6 +232,27 @@ export class Tokenization {
}
}

/**
* An object which serializes to the JSON `null` literal, and which can safely
* be passed across languages where `undefined` and `null` are not different.
*/
export class JsonNull {
/** The canonical instance of `JsonNull`. */
public static readonly INSTANCE = new JsonNull();

private constructor() { }

/** Obtains the JSON representation of this object (`null`) */
public toJSON(): any {
return null;
}

/** Obtains the string representation of this object (`'null'`) */
public toString(): string {
return 'null';
}
}

/**
* Options for the 'reverse()' operation
*/
Expand Down