Skip to content
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
2 changes: 1 addition & 1 deletion lib/open_api/open_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ defmodule EctoCommand.OpenApi do
defp parse_option({:required, _}, acc), do: acc

defp base_schema({:array, inner_type}, opts) do
%{type: :array, items: [schema_for(inner_type, Keyword.drop(opts, [:doc, :default]))]}
%{type: :array, items: schema_for(inner_type, Keyword.drop(opts, [:doc, :default]))}
end

defp base_schema(type, _opts), do: base_schema(type)
Expand Down
6 changes: 3 additions & 3 deletions lib/open_api/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule EctoCommand.OpenApi.Type do
[example: true] ++ options
end

def example_for(%Schema{type: :array, items: [%{enum: values}]} = _schema) when is_list(values),
def example_for(%Schema{type: :array, items: %{enum: values}} = _schema) when is_list(values),
do: Enum.take(values, 2)

def example_for(%Schema{default: default}) when not is_nil(default), do: default
Expand All @@ -49,8 +49,8 @@ defmodule EctoCommand.OpenApi.Type do
def example_for(%Schema{type: :string, format: :password}), do: Keyword.get(password(), :example)
def example_for(%Schema{type: :integer} = schema), do: trunc(number_example(schema))
def example_for(%Schema{type: :number} = schema), do: number_example(schema)
def example_for(%Schema{type: :array, items: [%{example: nil}]}), do: []
def example_for(%Schema{type: :array, items: [%{example: example}]}), do: [example]
def example_for(%Schema{type: :array, items: %{example: nil}}), do: []
def example_for(%Schema{type: :array, items: %{example: example}}), do: [example]

def example_for(%Schema{type: :object, properties: properties}) do
Map.new(properties, fn {name, %Schema{example: example} = schema} ->
Expand Down
8 changes: 4 additions & 4 deletions test/unit/command/open_api/open_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,25 @@ defmodule Unit.EctoCommand.OpenApi.OpenApiTest do
a_date: %OpenApiSpex.Schema{type: :string, format: :date, example: "2020-04-20"},
a_list_of_enums: %OpenApiSpex.Schema{
type: :array,
items: [%OpenApiSpex.Schema{enum: ["a", "b", "c"], type: :string, example: "a"}],
items: %OpenApiSpex.Schema{enum: ["a", "b", "c"], type: :string, example: "a"},
example: ["a", "b"],
description: "A list of enums"
},
a_list_of_strings_a: %OpenApiSpex.Schema{
type: :array,
items: [%OpenApiSpex.Schema{type: :string, example: ""}],
items: %OpenApiSpex.Schema{type: :string, example: ""},
default: [],
example: []
},
a_list_of_strings_b: %OpenApiSpex.Schema{
type: :array,
items: [%OpenApiSpex.Schema{type: :string, example: ""}],
items: %OpenApiSpex.Schema{type: :string, example: ""},
description: "A list of strings A",
example: [""]
},
a_list_of_strings_c: %OpenApiSpex.Schema{
type: :array,
items: [%OpenApiSpex.Schema{enum: ["a", "b", "c"], type: :string, example: "a"}],
items: %OpenApiSpex.Schema{enum: ["a", "b", "c"], type: :string, example: "a"},
example: ["a", "b"],
description: "A list of strings B"
},
Expand Down
12 changes: 6 additions & 6 deletions test/unit/command/open_api/type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ defmodule Unit.EctoCommand.OpenApi.TypeTest do

describe "example_for/1 generates a valid example" do
test "for arrays of enums" do
assert [1, 2] == Type.example_for(%Schema{type: :array, items: [%{type: :integer, enum: [1, 2, 3]}]})
assert ["a", "b"] == Type.example_for(%Schema{type: :array, items: [%{type: :string, enum: ["a", "b", "c"]}]})
assert [1, 2] == Type.example_for(%Schema{type: :array, items: %{type: :integer, enum: [1, 2, 3]}})
assert ["a", "b"] == Type.example_for(%Schema{type: :array, items: %{type: :string, enum: ["a", "b", "c"]}})
end

test "for enums" do
Expand Down Expand Up @@ -138,16 +138,16 @@ defmodule Unit.EctoCommand.OpenApi.TypeTest do
end

test "for arrays without inner type example and without default" do
assert [] == Type.example_for(%Schema{type: :array, items: [%Schema{type: :integer}]})
assert [] == Type.example_for(%Schema{type: :array, items: %Schema{type: :integer}})
end

test "for arrays with inner type example and without default" do
assert [1] == Type.example_for(%Schema{type: :array, items: [%Schema{type: :integer, example: 1}]})
assert [1] == Type.example_for(%Schema{type: :array, items: %Schema{type: :integer, example: 1}})
end

test "for arrays with inner type example and with default" do
assert [1, 2, 3] ==
Type.example_for(%Schema{type: :array, items: [%Schema{type: :integer, example: 1}], default: [1, 2, 3]})
Type.example_for(%Schema{type: :array, items: %Schema{type: :integer, example: 1}, default: [1, 2, 3]})
end

test "for object" do
Expand All @@ -159,7 +159,7 @@ defmodule Unit.EctoCommand.OpenApi.TypeTest do
id: %OpenApiSpex.Schema{type: :string},
name: %OpenApiSpex.Schema{type: :string},
type: %OpenApiSpex.Schema{enum: ["a", "b"], type: :string, example: "a"},
tags: %OpenApiSpex.Schema{type: :array, items: [%OpenApiSpex.Schema{type: :string}], default: []},
tags: %OpenApiSpex.Schema{type: :array, items: %OpenApiSpex.Schema{type: :string}, default: []},
non_required_id: %OpenApiSpex.Schema{type: :string}
}
}
Expand Down