Skip to content

Commit

Permalink
Fix(serializer): Account for Required and Enum Properties (#9)
Browse files Browse the repository at this point in the history
* Fix(serializer): Account for Required and Enum Properties

* format

* change test
  • Loading branch information
kyleVsteger authored May 23, 2024
1 parent ae27546 commit 47c49b6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/channel_spec/serializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ defmodule Serializer do
messages = Map.new(messages, fn {event, operation} -> {event, operation.schema} end)
{:messages, messages}

{:required, required} when is_list(required) ->
{:required, required}

{:enum, enum} when is_list(enum) ->
{:enum, enum}

{key, %Xema.Schema{} = schema} ->
{key, Xema.Schema.to_map(schema)}

Expand Down
42 changes: 42 additions & 0 deletions test/channel_spec/serializer_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule ChannelSpec.SerializerTest do
use ExUnit.Case, async: true

describe "to_schema/1" do
test "converts an elixir schema to a json schema map" do
schema = %{
type: :object,
properties: %{
string: %{type: :string},
enum: %{type: :string, enum: [:foo, :bar]},
one_of_array: %{
type: :array,
items: %{
one_of: [
%{type: :string},
%{type: :number}
]
}
}
}
}

converted_schema =
schema
|> Serializer.to_schema()
|> Serializer.to_string()
|> Jason.decode!()

assert converted_schema == %{
"properties" => %{
"enum" => %{"enum" => ["foo", "bar"], "type" => "string"},
"one_of_array" => %{
"items" => %{"one_of" => [%{"type" => "string"}, %{"type" => "number"}]},
"type" => "array"
},
"string" => %{"type" => "string"}
},
"type" => "object"
}
end
end
end

0 comments on commit 47c49b6

Please sign in to comment.