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: stringify existing AWSJSON field value #754

Merged
merged 1 commit into from
Nov 10, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3414,7 +3414,11 @@ export default function MyPostForm(props) {
setUsername(cleanValues.username);
setProfile_url(cleanValues.profile_url);
setPost_url(cleanValues.post_url);
setMetadata(cleanValues.metadata);
setMetadata(
typeof cleanValues.metadata === \\"string\\"
? cleanValues.metadata
: JSON.stringify(cleanValues.metadata)
);
setErrors({});
};
const [postRecord, setPostRecord] = React.useState(post);
Expand Down
60 changes: 48 additions & 12 deletions packages/codegen-ui-react/lib/forms/form-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import {
PropertyAccessChain,
PropertyAssignment,
PropertyName,
PropertyAccessExpression,
ElementAccessExpression,
ConditionalExpression,
} from 'typescript';

// used just to sanitize nested array field names
Expand Down Expand Up @@ -269,18 +272,24 @@ export const resetStateFunction = (fieldConfigs: Record<string, FieldConfigMetad
factory.createStringLiteral(stateName),
);

acc.push(
setStateExpression(
renderedName,
isArray && recordOrInitialValues === 'cleanValues'
? factory.createBinaryExpression(
accessExpression,
factory.createToken(SyntaxKind.QuestionQuestionToken),
factory.createArrayLiteralExpression([], false),
)
: accessExpression,
),
);
// Initial values should have the correct values and not need a modifier
if (dataType === 'AWSJSON' && !isArray && recordOrInitialValues !== 'initialValues') {
const awsJSONAccessModifier = stringifyAWSJSONFieldValue(accessExpression);
acc.push(setStateExpression(renderedName, awsJSONAccessModifier));
} else {
acc.push(
setStateExpression(
renderedName,
isArray && recordOrInitialValues === 'cleanValues'
? factory.createBinaryExpression(
accessExpression,
factory.createToken(SyntaxKind.QuestionQuestionToken),
factory.createArrayLiteralExpression([], false),
)
: accessExpression,
),
);
}
if (isArray) {
acc.push(
setStateExpression(
Expand Down Expand Up @@ -347,6 +356,33 @@ export const resetStateFunction = (fieldConfigs: Record<string, FieldConfigMetad
);
};

/**
* Datastore allows JSON strings and normal JSON so check for a string
* before stringifying or else the string will return with escaped quotes
*
* Example output:
* typeof cleanValues.metadata === 'string' ? cleanValues.metadata : JSON.stringify(cleanValues.metadata)
*/
const stringifyAWSJSONFieldValue = (
value: PropertyAccessExpression | ElementAccessExpression,
): ConditionalExpression => {
return factory.createConditionalExpression(
factory.createBinaryExpression(
factory.createTypeOfExpression(value),
factory.createToken(SyntaxKind.EqualsEqualsEqualsToken),
factory.createStringLiteral('string'),
),
factory.createToken(SyntaxKind.QuestionToken),
value,
factory.createToken(SyntaxKind.ColonToken),
factory.createCallExpression(
factory.createPropertyAccessExpression(factory.createIdentifier('JSON'), factory.createIdentifier('stringify')),
undefined,
[value],
),
);
};

/**
* const [name, setName] = React.useState({default_expression});
*
Expand Down