Skip to content

Commit

Permalink
Enforce only JSONPath executed on output transforms
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <[email protected]>
  • Loading branch information
ohltyler committed Nov 15, 2024
1 parent 71eaf07 commit 7b21036
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
4 changes: 4 additions & 0 deletions common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ export enum PROCESSOR_CONTEXT {
SEARCH_REQUEST = 'search_request',
SEARCH_RESPONSE = 'search_response',
}
export enum TRANSFORM_CONTEXT {
INPUT = 'input',
OUTPUT = 'output',
}
export const START_FROM_SCRATCH_WORKFLOW_NAME = 'Start From Scratch';
export const DEFAULT_NEW_WORKFLOW_NAME = 'new_workflow';
export const DEFAULT_NEW_WORKFLOW_DESCRIPTION = 'My new workflow';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
PROCESSOR_CONTEXT,
SearchHit,
SimulateIngestPipelineResponse,
TRANSFORM_CONTEXT,
WorkflowConfig,
WorkflowFormValues,
customStringify,
Expand Down Expand Up @@ -186,12 +187,14 @@ export function InputTransformModal(props: InputTransformModalProps) {
? generateArrayTransform(
sampleSourceInput as [],
tempInputMap[selectedTransformOption],
props.context
props.context,
TRANSFORM_CONTEXT.INPUT
)
: generateTransform(
sampleSourceInput,
tempInputMap[selectedTransformOption],
props.context
props.context,
TRANSFORM_CONTEXT.INPUT
);

setTransformedInput(customStringify(output));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
SearchHit,
SearchPipelineConfig,
SimulateIngestPipelineResponse,
TRANSFORM_CONTEXT,
WorkflowConfig,
WorkflowFormValues,
customStringify,
Expand Down Expand Up @@ -158,7 +159,8 @@ export function OutputTransformModal(props: OutputTransformModalProps) {
const output = generateTransform(
sampleSourceOutput,
reverseKeysAndValues(tempOutputMap[selectedTransformOption]),
props.context
props.context,
TRANSFORM_CONTEXT.OUTPUT
);
setTransformedOutput(customStringify(output));
} catch {}
Expand Down
30 changes: 21 additions & 9 deletions public/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
PROCESSOR_CONTEXT,
SimulateIngestPipelineDoc,
SimulateIngestPipelineResponse,
TRANSFORM_CONTEXT,
WORKFLOW_RESOURCE_TYPE,
WORKFLOW_STEP_TYPE,
Workflow,
Expand Down Expand Up @@ -186,7 +187,8 @@ export function unwrapTransformedDocs(
export function generateTransform(
input: {} | [],
map: MapFormValue,
context: PROCESSOR_CONTEXT
context: PROCESSOR_CONTEXT,
transformContext: TRANSFORM_CONTEXT
): {} {
let output = {};
map.forEach((mapEntry) => {
Expand All @@ -195,7 +197,8 @@ export function generateTransform(
mapEntry,
input,
mapEntry.value,
context
context,
transformContext
);
output = {
...output,
Expand All @@ -213,13 +216,20 @@ export function generateTransform(
export function generateArrayTransform(
input: [],
map: MapFormValue,
context: PROCESSOR_CONTEXT
context: PROCESSOR_CONTEXT,
transformContext: TRANSFORM_CONTEXT
): {}[] {
let output = [] as {}[];
map.forEach((mapEntry) => {
try {
const transformedResult = input.map((inputEntry) =>
getTransformedResult(mapEntry, inputEntry, mapEntry.value, context)
getTransformedResult(
mapEntry,
inputEntry,
mapEntry.value,
context,
transformContext
)
);
output = {
...output,
Expand All @@ -234,14 +244,16 @@ function getTransformedResult(
mapEntry: MapEntry,
input: {},
path: string,
context: PROCESSOR_CONTEXT
context: PROCESSOR_CONTEXT,
transformContext: TRANSFORM_CONTEXT
): any {
// Rregular dot notation can only be executed if 1/ the JSONPath selector is not explicitly defined,
// and 2/ it is in the context of ingest. For search request / search response parsing,
// it can only be JSONPath, due to backend parsing limitations.
// Regular dot notation can only be executed if 1/ the JSONPath selector is not explicitly defined,
// and 2/ it is in the context of ingest, and 3/ it is transforming the input (the source document).
// For all other scenarios, it can only be JSONPath, due to backend parsing limitations.
if (
!mapEntry.value.startsWith(JSONPATH_ROOT_SELECTOR) &&
context === PROCESSOR_CONTEXT.INGEST
context === PROCESSOR_CONTEXT.INGEST &&
transformContext === TRANSFORM_CONTEXT.INPUT
) {
// sub-edge case: if the path is ".", it implies returning
// the entire value. This may happen if full_response_path=false
Expand Down

0 comments on commit 7b21036

Please sign in to comment.