Skip to content

Commit

Permalink
Fix Schema.example/2 for anyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
zorbash committed May 26, 2023
1 parent acc845b commit 3046c68
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/open_api_spex/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ defmodule OpenApiSpex.Schema do

def example(%Schema{enum: [example | _]}, _schemas), do: example
def example(%Schema{oneOf: [schema | _]}, schemas), do: example(schema, schemas)
def example(%Schema{anyOf: [schema | _]}, schemas), do: example(schema, schemas)

def example(%Schema{allOf: schemas}, all_schemas) when is_list(schemas) do
example_for(schemas, :allOf, all_schemas)
Expand Down
11 changes: 11 additions & 0 deletions test/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,17 @@ defmodule OpenApiSpex.SchemaTest do
assert Schema.example(schema, spec) == "one"
end

test "example for anyOf schema", %{spec: spec} do
schema = %Schema{
anyOf: [
%Schema{type: :string, example: "one"},
%Schema{type: :string, example: "two"}
]
}

assert Schema.example(schema, spec) == "one"
end

test "example for reference", %{spec: spec} do
assert %{
"email" => "joe@gmail.com",
Expand Down

0 comments on commit 3046c68

Please sign in to comment.