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 1 commit
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", validation: :format)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add a blurb about not being able to parse xml as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't what the UI shows, due to the error_tag function, but still good catch

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", validation: :format)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as other comment

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.67",
version: "2.5.68",
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 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 Down