Skip to content

Commit 4a3f7a3

Browse files
authoredMar 7, 2025··
Add examples property to Schema (#654)
Fixes #653
1 parent abe90e3 commit 4a3f7a3

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed
 

‎lib/open_api_spex/schema.ex

+6
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ defmodule OpenApiSpex.Schema do
183183
:xml,
184184
:externalDocs,
185185
:example,
186+
:examples,
186187
:deprecated,
187188
:"x-struct",
188189
:"x-validate",
@@ -250,6 +251,7 @@ defmodule OpenApiSpex.Schema do
250251
xml: Xml.t() | nil,
251252
externalDocs: ExternalDocumentation.t() | nil,
252253
example: any,
254+
examples: [any] | nil,
253255
deprecated: boolean | nil,
254256
"x-struct": module | nil,
255257
"x-validate": module | nil,
@@ -369,6 +371,10 @@ defmodule OpenApiSpex.Schema do
369371
schema.example
370372
end
371373

374+
def example(%Schema{examples: [example | _]}) when not is_nil(example) do
375+
example
376+
end
377+
372378
def example(%Schema{enum: [example | _]}) do
373379
example
374380
end

‎test/schema_test.exs

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ defmodule OpenApiSpex.SchemaTest do
103103
assert Schema.example(%Schema{type: :string, example: "foo"}) == "foo"
104104
end
105105

106+
test "uses the first value in `examples` property when not nil" do
107+
assert Schema.example(%Schema{type: :string, examples: ["foo", "bar"]}) == "foo"
108+
end
109+
106110
test "defaults to type-appropriate value for :string" do
107111
assert Schema.example(%Schema{type: :string}) == ""
108112
end

0 commit comments

Comments
 (0)
Please sign in to comment.