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

added XML check to HTTP and Auth Step #1629

Merged
merged 4 commits into from
Mar 17, 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.
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 @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion apps/andi/lib/andi_web/views/error_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 1 addition & 1 deletion apps/andi/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ defmodule Andi.InputSchemas.Ingestions.ExtractAuthStepTest do
[nil],
["[]"],
["[{}]"],
["[{\"bob\": 1}]"]
["[{\"bob\": 1}]"],
["<note><to>bob</to><heading>Reminder</heading></note>"]
])
end

Expand All @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ defmodule Andi.InputSchemas.Ingestions.ExtractHttpStepTest do
[nil],
["[]"],
["[{}]"],
["[{\"bob\": 1}]"]
["[{\"bob\": 1}]"],
["<note><to>bob</to><heading>Reminder</heading></note>"]
])
end

Expand All @@ -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],
Expand Down