diff --git a/lib/open_api_spex/schema.ex b/lib/open_api_spex/schema.ex index 390027c6..a608a85f 100644 --- a/lib/open_api_spex/schema.ex +++ b/lib/open_api_spex/schema.ex @@ -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) diff --git a/test/schema_test.exs b/test/schema_test.exs index c4cac6f8..7e67e8cb 100644 --- a/test/schema_test.exs +++ b/test/schema_test.exs @@ -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",