From fa2fe39ae6b25cd73ec09004311b1fb15048ce03 Mon Sep 17 00:00:00 2001
From: nicholas-goodwin <111888703+nicholas-goodwin@users.noreply.github.com>
Date: Fri, 17 Mar 2023 10:22:40 -0500
Subject: [PATCH] added XML check to HTTP and Auth Step (#1629)
* added XML check to HTTP and Auth Step
* updated blorb
* version bump and format
---
.../ingestions/extract_steps/extract_auth_step.ex | 13 +++++++++++--
.../ingestions/extract_steps/extract_http_step.ex | 14 ++++++++++++--
apps/andi/lib/andi_web/views/error_helpers.ex | 2 +-
apps/andi/mix.exs | 2 +-
.../datasets/extract_auth_step_test.exs | 5 +++--
.../datasets/extract_http_step_test.exs | 5 +++--
6 files changed, 31 insertions(+), 10 deletions(-)
diff --git a/apps/andi/lib/andi/input_schemas/ingestions/extract_steps/extract_auth_step.ex b/apps/andi/lib/andi/input_schemas/ingestions/extract_steps/extract_auth_step.ex
index b004ae5a6..11166abf5 100644
--- a/apps/andi/lib/andi/input_schemas/ingestions/extract_steps/extract_auth_step.ex
+++ b/apps/andi/lib/andi/input_schemas/ingestions/extract_steps/extract_auth_step.ex
@@ -86,8 +86,17 @@ defmodule Andi.InputSchemas.Ingestions.ExtractAuthStep do
defp validate_body_format(%{changes: %{body: body}} = changeset) do
case Jason.decode(body) do
- {:ok, _} -> changeset
- {:error, _} -> Changeset.add_error(changeset, :body, "could not parse json", validation: :format)
+ {:ok, _} ->
+ changeset
+
+ {:error, _} ->
+ try do
+ SweetXml.parse(body)
+ changeset
+ catch
+ :exit, _ ->
+ Changeset.add_error(changeset, :body, "could not parse json or xml", validation: :format)
+ end
end
end
diff --git a/apps/andi/lib/andi/input_schemas/ingestions/extract_steps/extract_http_step.ex b/apps/andi/lib/andi/input_schemas/ingestions/extract_steps/extract_http_step.ex
index a3b6aec0a..9685c7b34 100644
--- a/apps/andi/lib/andi/input_schemas/ingestions/extract_steps/extract_http_step.ex
+++ b/apps/andi/lib/andi/input_schemas/ingestions/extract_steps/extract_http_step.ex
@@ -6,6 +6,7 @@ defmodule Andi.InputSchemas.Ingestions.ExtractHttpStep do
alias Andi.InputSchemas.Ingestions.ExtractHeader
alias Andi.InputSchemas.StructTools
alias Ecto.Changeset
+ alias SweetXml
@primary_key false
embedded_schema do
@@ -95,8 +96,17 @@ defmodule Andi.InputSchemas.Ingestions.ExtractHttpStep do
defp validate_body_format(%{changes: %{body: body}} = changeset) do
case Jason.decode(body) do
- {:ok, _} -> changeset
- {:error, _} -> Changeset.add_error(changeset, :body, "could not parse json", validation: :format)
+ {:ok, _} ->
+ changeset
+
+ {:error, _} ->
+ try do
+ SweetXml.parse(body)
+ changeset
+ catch
+ :exit, _ ->
+ Changeset.add_error(changeset, :body, "could not parse json or xml", validation: :format)
+ end
end
end
diff --git a/apps/andi/lib/andi_web/views/error_helpers.ex b/apps/andi/lib/andi_web/views/error_helpers.ex
index 2557b52ba..388a3df7c 100644
--- a/apps/andi/lib/andi_web/views/error_helpers.ex
+++ b/apps/andi/lib/andi_web/views/error_helpers.ex
@@ -137,7 +137,7 @@ defmodule AndiWeb.ErrorHelpers do
defp interpret_error_message(message, :datasetLink, _), do: message
defp interpret_error_message("is required", field, _), do: default_error_message(field)
defp interpret_error_message(message, :format, _), do: "Error: " <> get_format_error_message(message)
- defp interpret_error_message(_message, :body, _), do: "Please enter valid JSON"
+ defp interpret_error_message(_message, :body, _), do: "Please enter valid JSON or XML"
defp interpret_error_message(message, field, _) when field in [:topLevelSelector, :cadence, :dataName, :license, :orgName],
do: "Error: #{message}"
diff --git a/apps/andi/mix.exs b/apps/andi/mix.exs
index c232e7f13..f29074648 100644
--- a/apps/andi/mix.exs
+++ b/apps/andi/mix.exs
@@ -4,7 +4,7 @@ defmodule Andi.MixProject do
def project do
[
app: :andi,
- version: "2.5.68",
+ version: "2.5.69",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
diff --git a/apps/andi/test/unit/andi/input_schemas/datasets/extract_auth_step_test.exs b/apps/andi/test/unit/andi/input_schemas/datasets/extract_auth_step_test.exs
index ee31055b6..86c40265b 100644
--- a/apps/andi/test/unit/andi/input_schemas/datasets/extract_auth_step_test.exs
+++ b/apps/andi/test/unit/andi/input_schemas/datasets/extract_auth_step_test.exs
@@ -45,7 +45,8 @@ defmodule Andi.InputSchemas.Ingestions.ExtractAuthStepTest do
[nil],
["[]"],
["[{}]"],
- ["[{\"bob\": 1}]"]
+ ["[{\"bob\": 1}]"],
+ ["bobReminder"]
])
end
@@ -56,7 +57,7 @@ defmodule Andi.InputSchemas.Ingestions.ExtractAuthStepTest do
ExtractAuthStep.changeset(ExtractAuthStep.get_module(), changes)
|> ExtractAuthStep.validate()
- assert changeset.errors[:body] == {"could not parse json", [validation: :format]}
+ assert changeset.errors[:body] == {"could not parse json or xml", [validation: :format]}
where([
[:value],
diff --git a/apps/andi/test/unit/andi/input_schemas/datasets/extract_http_step_test.exs b/apps/andi/test/unit/andi/input_schemas/datasets/extract_http_step_test.exs
index fecf9d0d3..32e14f4b5 100644
--- a/apps/andi/test/unit/andi/input_schemas/datasets/extract_http_step_test.exs
+++ b/apps/andi/test/unit/andi/input_schemas/datasets/extract_http_step_test.exs
@@ -35,7 +35,8 @@ defmodule Andi.InputSchemas.Ingestions.ExtractHttpStepTest do
[nil],
["[]"],
["[{}]"],
- ["[{\"bob\": 1}]"]
+ ["[{\"bob\": 1}]"],
+ ["bobReminder"]
])
end
@@ -46,7 +47,7 @@ defmodule Andi.InputSchemas.Ingestions.ExtractHttpStepTest do
ExtractHttpStep.changeset(ExtractHttpStep.get_module(), changes)
|> ExtractHttpStep.validate()
- assert changeset.errors[:body] == {"could not parse json", [validation: :format]}
+ assert changeset.errors[:body] == {"could not parse json or xml", [validation: :format]}
where([
[:value],