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, [
|
{application, emqx_schema_registry, [
|
||||||
{description, "EMQX Schema Registry"},
|
{description, "EMQX Schema Registry"},
|
||||||
{vsn, "0.3.1"},
|
{vsn, "0.3.2"},
|
||||||
{registered, [emqx_schema_registry_sup]},
|
{registered, [emqx_schema_registry_sup]},
|
||||||
{mod, {emqx_schema_registry_app, []}},
|
{mod, {emqx_schema_registry_app, []}},
|
||||||
{included_applications, [
|
{included_applications, [
|
||||||
|
|
|
@ -142,18 +142,26 @@ schema("/schema_registry/:name") ->
|
||||||
),
|
),
|
||||||
?OK(Response);
|
?OK(Response);
|
||||||
'/schema_registry'(post, #{body := Params0 = #{<<"name">> := Name}}) ->
|
'/schema_registry'(post, #{body := Params0 = #{<<"name">> := Name}}) ->
|
||||||
Params = maps:without([<<"name">>], Params0),
|
try
|
||||||
case emqx_schema_registry:get_schema(Name) of
|
ok = emqx_resource:validate_name(Name),
|
||||||
{error, not_found} ->
|
Params = maps:without([<<"name">>], Params0),
|
||||||
case emqx_schema_registry:add_schema(Name, Params) of
|
case emqx_schema_registry:get_schema(Name) of
|
||||||
ok ->
|
{error, not_found} ->
|
||||||
{ok, Res} = emqx_schema_registry:get_schema(Name),
|
case emqx_schema_registry:add_schema(Name, Params) of
|
||||||
{201, Res#{name => Name}};
|
ok ->
|
||||||
{error, Error} ->
|
{ok, Res} = emqx_schema_registry:get_schema(Name),
|
||||||
?BAD_REQUEST(Error)
|
{201, Res#{name => Name}};
|
||||||
end;
|
{error, Error} ->
|
||||||
{ok, _} ->
|
?BAD_REQUEST(Error)
|
||||||
?BAD_REQUEST('ALREADY_EXISTS', <<"Schema already exists">>)
|
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.
|
end.
|
||||||
|
|
||||||
'/schema_registry/:name'(get, #{bindings := #{name := Name}}) ->
|
'/schema_registry/:name'(get, #{bindings := #{name := Name}}) ->
|
||||||
|
|
|
@ -329,3 +329,26 @@ t_crud(Config) ->
|
||||||
),
|
),
|
||||||
|
|
||||||
ok.
|
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