Skip to content

Commit e95b2bf

Browse files
authored
Merge pull request #18 from stolen/fix-crash-on-const
openapi_schema: fix crash when validating array/map vs const
2 parents a6d3ee8 + f09a14f commit e95b2bf

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

src/openapi_schema.erl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ encode3(#{const := Value}, #{auto_convert := Convert}, Input, Path) when is_atom
462462
_ -> {error, #{error => not_const2, path => Path, input => Input, value => Value}}
463463
end;
464464

465+
encode3(#{const := Value}, #{}, Input, Path) ->
466+
{error, #{error => not_const, path => Path, input => Input, value => Value}};
467+
465468
encode3(#{enum := Choices, type := <<"string">>}, #{auto_convert := Convert}, Input, Path) ->
466469
InputValue = case Input of
467470
_ when is_binary(Input) -> Input;

test/openapi_schema_SUITE.erl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ groups() ->
3434
check_explain,
3535
check_explain_on_error,
3636
one_of_integer_const,
37+
one_of_const_wrong_type,
3738
one_of_const_default,
3839
drop_unidirectional_keys,
3940
filter_read_only_props
@@ -448,6 +449,12 @@ one_of_const_default(_) ->
448449
#{schema => FooType, apply_defaults => true}),
449450
ok.
450451

452+
one_of_const_wrong_type(_) ->
453+
FooProp = #{k1 => #{oneOf => [#{const => <<"hello">>}, #{const => <<"world">>}]}},
454+
FooType = #{type => <<"object">>, properties => FooProp},
455+
{error, #{error := not_const}} = openapi_schema:process(#{k1 => #{}}, #{schema => FooType}),
456+
{error, #{error := not_const}} = openapi_schema:process(#{k1 => []}, #{schema => FooType}),
457+
ok.
451458

452459
drop_unidirectional_keys(_) ->
453460
Schema = #{

0 commit comments

Comments
 (0)