Merge pull request #13345 from thalesmg/20240626-r572-fix-validate-schema-reg-name

fix(schema registry api): validate schema name when creating
This commit is contained in:
Thales Macedo Garitezi 2024-06-28 12:08:27 -03:00 committed by GitHub
commit 2a9c27d206
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 45 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{application, emqx_schema_registry, [
{description, "EMQX Schema Registry"},
{vsn, "0.3.1"},
{vsn, "0.3.2"},
{registered, [emqx_schema_registry_sup]},
{mod, {emqx_schema_registry_app, []}},
{included_applications, [

View File

@ -142,18 +142,26 @@ schema("/schema_registry/:name") ->
),
?OK(Response);
'/schema_registry'(post, #{body := Params0 = #{<<"name">> := Name}}) ->
Params = maps:without([<<"name">>], Params0),
case emqx_schema_registry:get_schema(Name) of
{error, not_found} ->
case emqx_schema_registry:add_schema(Name, Params) of
ok ->
{ok, Res} = emqx_schema_registry:get_schema(Name),
{201, Res#{name => Name}};
{error, Error} ->
?BAD_REQUEST(Error)
end;
{ok, _} ->
?BAD_REQUEST('ALREADY_EXISTS', <<"Schema already exists">>)
try
ok = emqx_resource:validate_name(Name),
Params = maps:without([<<"name">>], Params0),
case emqx_schema_registry:get_schema(Name) of
{error, not_found} ->
case emqx_schema_registry:add_schema(Name, Params) of
ok ->
{ok, Res} = emqx_schema_registry:get_schema(Name),
{201, Res#{name => Name}};
{error, Error} ->
?BAD_REQUEST(Error)
end;
{ok, _} ->
?BAD_REQUEST('ALREADY_EXISTS', <<"Schema already exists">>)
end
catch
throw:#{kind := Kind, reason := Reason} ->
Msg0 = ?ERROR_MSG('BAD_REQUEST', Reason),
Msg = Msg0#{kind => Kind},
{400, Msg}
end.
'/schema_registry/:name'(get, #{bindings := #{name := Name}}) ->

View File

@ -329,3 +329,26 @@ t_crud(Config) ->
),
ok.
%% Tests that we can't create 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),
SerdeTypeBin = atom_to_binary(SerdeType),
%% Too long!
SchemaName = binary:copy(<<"a">>, 256),
Params = #{
<<"type">> => SerdeTypeBin,
<<"source">> => SourceBin,
<<"name">> => SchemaName,
<<"description">> => <<"My schema">>
},
?assertMatch(
{ok, 400, #{
<<"code">> := <<"BAD_REQUEST">>,
<<"kind">> := <<"validation_error">>,
<<"message">> := <<"Name length must be less than 255">>
}},
request({post, Params})
),
ok.

View File

@ -0,0 +1 @@
Improved error message when creating a schema in Schema Registry whose name is too long or has invalid format.