fix(schema registry api): validate schema name when creating
Fixes https://emqx.atlassian.net/browse/EMQX-10958
This commit is contained in:
parent
7e089dce6b
commit
6f00df6452
|
@ -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, [
|
||||
|
|
|
@ -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}}) ->
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Improved error message when creating a schema in Schema Registry whose name is too long or has invalid format.
|
Loading…
Reference in New Issue