From 402719953ed773d5f913c53f78db5518d16d9b83 Mon Sep 17 00:00:00 2001 From: Luca Dei Zotti Date: Wed, 26 Jan 2022 00:06:06 +0100 Subject: [PATCH 1/2] [change] allow empty schemas to be validated as wildcards Fix #418 --- lib/open_api_spex/cast.ex | 7 +++++++ test/cast_test.exs | 4 +--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/open_api_spex/cast.ex b/lib/open_api_spex/cast.ex index f8a97a54..9a448f47 100644 --- a/lib/open_api_spex/cast.ex +++ b/lib/open_api_spex/cast.ex @@ -170,6 +170,13 @@ defmodule OpenApiSpex.Cast do def cast(%__MODULE__{schema: %{type: :array}} = ctx), do: Array.cast(ctx) + # Explicit nil types are considered as wildcards, as in + # properties + # value: {} + # See #418 + def cast(%__MODULE__{schema: %{type: nil}, value: value} = _ctx), + do: {:ok, value} + def cast(%__MODULE__{schema: %{type: _other}} = ctx), do: error(ctx, {:invalid_schema_type}) diff --git a/test/cast_test.exs b/test/cast_test.exs index 0fcc1075..82542d35 100644 --- a/test/cast_test.exs +++ b/test/cast_test.exs @@ -12,9 +12,7 @@ defmodule OpenApiSpec.CastTest do assert error.reason == :invalid_schema_type assert error.type == :nope - assert {:error, [error]} = cast(value: "string", schema: %Schema{type: nil}) - assert error.reason == :invalid_schema_type - assert error.type == nil + assert {:ok, "string"} = cast(value: "string", schema: %Schema{type: nil}) end # Note: full tests for primitives are covered in Cast.PrimitiveTest From 2eff0c2ffffa3e2aa2e96f2abab1ea24d4cdff8b Mon Sep 17 00:00:00 2001 From: zoten Date: Thu, 27 Jan 2022 08:38:19 +0100 Subject: [PATCH 2/2] Update lib/open_api_spex/cast.ex Co-authored-by: Mike Buhot --- lib/open_api_spex/cast.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/open_api_spex/cast.ex b/lib/open_api_spex/cast.ex index 9a448f47..7d24e524 100644 --- a/lib/open_api_spex/cast.ex +++ b/lib/open_api_spex/cast.ex @@ -173,7 +173,7 @@ defmodule OpenApiSpex.Cast do # Explicit nil types are considered as wildcards, as in # properties # value: {} - # See #418 + # See https://json-schema.org/understanding-json-schema/basics.html#id1 def cast(%__MODULE__{schema: %{type: nil}, value: value} = _ctx), do: {:ok, value}