Skip to content

Commit cae9ea7

Browse files
fabiopellegriniFabio Pellegrini
andauthored
fix: properly build open api spec for arrays (#8)
Co-authored-by: Fabio Pellegrini <fabio.pellegrini@fluida.io>
1 parent 9824300 commit cae9ea7

4 files changed

Lines changed: 14 additions & 14 deletions

File tree

lib/open_api/open_api.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ defmodule EctoCommand.OpenApi do
126126
defp parse_option({:required, _}, acc), do: acc
127127

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

132132
defp base_schema(type, _opts), do: base_schema(type)

lib/open_api/type.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule EctoCommand.OpenApi.Type do
3939
[example: true] ++ options
4040
end
4141

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

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

5555
def example_for(%Schema{type: :object, properties: properties}) do
5656
Map.new(properties, fn {name, %Schema{example: example} = schema} ->

test/unit/command/open_api/open_api_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,25 @@ defmodule Unit.EctoCommand.OpenApi.OpenApiTest do
119119
a_date: %OpenApiSpex.Schema{type: :string, format: :date, example: "2020-04-20"},
120120
a_list_of_enums: %OpenApiSpex.Schema{
121121
type: :array,
122-
items: [%OpenApiSpex.Schema{enum: ["a", "b", "c"], type: :string, example: "a"}],
122+
items: %OpenApiSpex.Schema{enum: ["a", "b", "c"], type: :string, example: "a"},
123123
example: ["a", "b"],
124124
description: "A list of enums"
125125
},
126126
a_list_of_strings_a: %OpenApiSpex.Schema{
127127
type: :array,
128-
items: [%OpenApiSpex.Schema{type: :string, example: ""}],
128+
items: %OpenApiSpex.Schema{type: :string, example: ""},
129129
default: [],
130130
example: []
131131
},
132132
a_list_of_strings_b: %OpenApiSpex.Schema{
133133
type: :array,
134-
items: [%OpenApiSpex.Schema{type: :string, example: ""}],
134+
items: %OpenApiSpex.Schema{type: :string, example: ""},
135135
description: "A list of strings A",
136136
example: [""]
137137
},
138138
a_list_of_strings_c: %OpenApiSpex.Schema{
139139
type: :array,
140-
items: [%OpenApiSpex.Schema{enum: ["a", "b", "c"], type: :string, example: "a"}],
140+
items: %OpenApiSpex.Schema{enum: ["a", "b", "c"], type: :string, example: "a"},
141141
example: ["a", "b"],
142142
description: "A list of strings B"
143143
},

test/unit/command/open_api/type_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ defmodule Unit.EctoCommand.OpenApi.TypeTest do
88

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

1515
test "for enums" do
@@ -138,16 +138,16 @@ defmodule Unit.EctoCommand.OpenApi.TypeTest do
138138
end
139139

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

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

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

153153
test "for object" do
@@ -159,7 +159,7 @@ defmodule Unit.EctoCommand.OpenApi.TypeTest do
159159
id: %OpenApiSpex.Schema{type: :string},
160160
name: %OpenApiSpex.Schema{type: :string},
161161
type: %OpenApiSpex.Schema{enum: ["a", "b"], type: :string, example: "a"},
162-
tags: %OpenApiSpex.Schema{type: :array, items: [%OpenApiSpex.Schema{type: :string}], default: []},
162+
tags: %OpenApiSpex.Schema{type: :array, items: %OpenApiSpex.Schema{type: :string}, default: []},
163163
non_required_id: %OpenApiSpex.Schema{type: :string}
164164
}
165165
}

0 commit comments

Comments
 (0)