-
Notifications
You must be signed in to change notification settings - Fork 206
Convert string to atom when casting enum schema #101
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f256bc5
Convert string to atom when casting enum containing atoms
mbuhot 67de070
Add test for enum of object type with schema
mbuhot 72bb633
Cast from string keyed map to atom keyed enum map
mbuhot 004abcc
Simplify Cast.Enum by generalizing equivalent?/2 function
mbuhot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| defmodule OpenApiSpex.Cast.Enum do | ||
| @moduledoc false | ||
| alias OpenApiSpex.Cast | ||
|
|
||
| def cast(ctx = %Cast{schema: %{enum: enum}, value: value}) do | ||
| case Enum.find(enum, {:error, :invalid_enum}, &equivalent?(&1, value)) do | ||
| {:error, :invalid_enum} -> Cast.error(ctx, {:invalid_enum}) | ||
| found -> {:ok, found} | ||
| end | ||
| end | ||
|
|
||
| defp equivalent?(x, x), do: true | ||
|
|
||
| # Special case: atoms are equivalent to their stringified representation | ||
| defp equivalent?(left, right) when is_atom(left) and is_binary(right) do | ||
| to_string(left) == right | ||
| end | ||
|
|
||
| # an explicit schema should be used to cast to enum of structs | ||
| defp equivalent?(_x, %_struct{}), do: false | ||
|
|
||
| # Special case: Atom-keyed maps are equivalent to their string-keyed representation | ||
| defp equivalent?(left, right) when is_map(left) and is_map(right) do | ||
| Enum.all?(left, fn {k, v} -> | ||
| equivalent?(v, Map.get(right, to_string(k))) | ||
| end) | ||
| end | ||
|
|
||
| defp equivalent?(_left, _right), do: false | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| defmodule OpenApiSpex.Cast.EnumTest do | ||
| use ExUnit.Case | ||
| alias OpenApiSpex.{Cast, Schema} | ||
| alias OpenApiSpex.Cast.Error | ||
|
|
||
| defp cast(ctx), do: Cast.cast(ctx) | ||
|
|
||
| defmodule User do | ||
| require OpenApiSpex | ||
| alias __MODULE__ | ||
|
|
||
| defstruct [:age] | ||
|
|
||
| def schema() do | ||
| %OpenApiSpex.Schema{ | ||
| type: :object, | ||
| required: [:age], | ||
| properties: %{ | ||
| age: %Schema{type: :integer}, | ||
| }, | ||
| enum: [%User{age: 32}, %User{age: 45}], | ||
| "x-struct": __MODULE__ | ||
| } | ||
| end | ||
| end | ||
|
|
||
| describe "Enum of strings" do | ||
| setup do | ||
| {:ok, %{schema: %Schema{type: :string, enum: ["one"]}}} | ||
| end | ||
|
|
||
| test "error on invalid string", %{schema: schema} do | ||
| assert {:error, [error]} = cast(schema: schema, value: "two") | ||
| assert %Error{} = error | ||
| assert error.reason == :invalid_enum | ||
| end | ||
|
|
||
| test "OK on valid string", %{schema: schema} do | ||
| assert {:ok, "one"} = cast(schema: schema, value: "one") | ||
| end | ||
| end | ||
|
|
||
| describe "Enum of atoms" do | ||
| setup do | ||
| {:ok, %{schema: %Schema{type: :string, enum: [:one, :two, :three]}}} | ||
| end | ||
|
|
||
| test "string will be converted to atom", %{schema: schema} do | ||
| assert {:ok, :three} = cast(schema: schema, value: "three") | ||
| end | ||
|
|
||
| test "error on invalid string", %{schema: schema} do | ||
| assert {:error, [error]} = cast(schema: schema, value: "four") | ||
| assert %Error{} = error | ||
| assert error.reason == :invalid_enum | ||
| end | ||
| end | ||
|
|
||
| describe "Enum with explicit schema" do | ||
| test "converts string keyed map to struct" do | ||
| assert {:ok, %User{age: 32}} = cast(schema: User.schema(), value: %{"age" => 32}) | ||
| end | ||
|
|
||
| test "Must be a valid enum value" do | ||
| assert {:error, [error]} = cast(schema: User.schema(), value: %{"age" => 33}) | ||
| assert %Error{} = error | ||
| assert error.reason == :invalid_enum | ||
| end | ||
| end | ||
|
|
||
| describe "Enum without explicit schema" do | ||
| setup do | ||
| schema = %Schema{ | ||
| type: :object, | ||
| enum: [%{age: 55}, %{age: 66}, %{age: 77}] | ||
| } | ||
| {:ok, %{schema: schema}} | ||
| end | ||
|
|
||
| test "casts from string keyed map", %{schema: schema} do | ||
| assert {:ok, %{age: 55}} = cast(value: %{"age" => 55}, schema: schema) | ||
| end | ||
|
|
||
| test "value must be a valid enum value", %{schema: schema} do | ||
| assert {:error, [error]} = cast(value: %{"age" => 56}, schema: schema) | ||
| assert %Error{} = error | ||
| assert error.reason == :invalid_enum | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests moved into the new test/cast/enum_test.exs file