fix(schema registry): handle large names during lookup

Fixes https://emqx.atlassian.net/browse/EMQX-12692
This commit is contained in:
Thales Macedo Garitezi 2024-07-11 14:35:31 -03:00
parent a4cc3ba9e8
commit 04b547d6f5
2 changed files with 30 additions and 10 deletions

View File

@ -44,6 +44,12 @@
get_serde/1
]).
%%-------------------------------------------------------------------------------------------------
%% Type definitions
%%-------------------------------------------------------------------------------------------------
-define(BAD_SCHEMA_NAME, <<"bad_schema_name">>).
-type schema() :: #{
type := serde_type(),
source := binary(),
@ -87,6 +93,8 @@ get_schema(SchemaName) ->
Config ->
{ok, Config}
catch
throw:#{reason := ?BAD_SCHEMA_NAME} ->
{error, not_found};
throw:not_found ->
{error, not_found}
end.
@ -343,16 +351,20 @@ to_bin(A) when is_atom(A) -> atom_to_binary(A);
to_bin(B) when is_binary(B) -> B.
schema_name_bin_to_atom(Bin) when size(Bin) > 255 ->
throw(
iolist_to_binary(
Msg = iolist_to_binary(
io_lib:format(
"Name is is too long."
" Please provide a shorter name (<= 255 bytes)."
" The name that is too long: \"~s\"",
[Bin]
)
)
);
),
Reason = #{
kind => validation_error,
reason => ?BAD_SCHEMA_NAME,
hint => Msg
},
throw(Reason);
schema_name_bin_to_atom(Bin) ->
try
binary_to_existing_atom(Bin, utf8)

View File

@ -347,7 +347,8 @@ t_crud(Config) ->
ok.
%% Tests that we can't create names that are too long and get a decent error message.
%% Tests that we can't create or lookup names that are too long and get a decent error
%% message.
t_name_too_long(Config) ->
SerdeType = ?config(serde_type, Config),
SourceBin = ?config(schema_source, Config),
@ -368,4 +369,11 @@ t_name_too_long(Config) ->
}},
request({post, Params})
),
?assertMatch(
{ok, 404, #{
<<"code">> := <<"NOT_FOUND">>,
<<"message">> := <<"Schema not found">>
}},
request({get, SchemaName})
),
ok.