export function diffRequestResponse()

in lib/armValidator/roundTripValidator.ts [85:148]


export function diffRequestResponse(
  payload: RequestResponsePair,
  info: OperationContext,
  jsonLoader: JsonLoader
) {
  const diffs = getJsonPatchDiff(payload.liveRequest.body ?? {}, payload.liveResponse.body ?? {}, {
    includeOldValue: true,
    minimizeDiff: false,
  });

  const rest = diffs
    .map((it: any) => {
      const jsonPath: string = it.remove || it.add || it.replace;
      if (it.replace !== undefined) {
        let isAllowed = false;
        for (let parameter of info.operationMatch?.operation.parameters ?? []) {
          if (isAllowed) {
            break;
          }
          isAllowed = checkReplacedSchemaInParameter(it.replace, parameter, jsonLoader);
        }
        for (let parameter of info.operationMatch?.operation._path.parameters ?? []) {
          if (isAllowed) {
            break;
          }
          isAllowed = checkReplacedSchemaInParameter(it.replace, parameter, jsonLoader);
        }
        if (!isAllowed) {
          return buildLiveValidationIssue("ROUNDTRIP_INCONSISTENT_PROPERTY", jsonPath, it);
        }
      } else if (it.add !== undefined && it.value !== null) {
        // IF a property is not in request but returned in response as null, ignore.
        let isAllowed = checkSchemaInResponse(
          it.add,
          info.operationMatch?.operation!,
          jsonLoader,
          payload.liveResponse.statusCode
        );
        if (!isAllowed) {
          return buildLiveValidationIssue("ROUNDTRIP_ADDITIONAL_PROPERTY", jsonPath, it);
        }
      } else if (it.remove !== undefined) {
        let isAllowed = false;
        for (let parameter of info.operationMatch?.operation.parameters ?? []) {
          if (isAllowed) {
            break;
          }
          isAllowed = checkRemovedSchemaInParameter(it.remove, parameter, jsonLoader);
        }
        for (let parameter of info.operationMatch?.operation._path.parameters ?? []) {
          if (isAllowed) {
            break;
          }
          isAllowed = checkRemovedSchemaInParameter(it.remove, parameter, jsonLoader);
        }
        if (!isAllowed) {
          return buildLiveValidationIssue("ROUNDTRIP_MISSING_PROPERTY", jsonPath, it);
        }
      }
      return undefined;
    })
    .filter((a) => a !== undefined);
  return rest;
}