Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/dev_sys.config.src
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
{pre_request, nova_request_plugin, #{decode_json_body => true,
read_urlencoded_body => true,
parse_qs => true}},
{pre_request, nova_json_schemas, #{render_errors => true}},
{pre_requeset, nova_request_old_plugin, #{}}
]},
{use_persistent_term, true},
Expand Down
12 changes: 12 additions & 0 deletions priv/schemas/create_item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"quantity": {
"type": "integer"
}
},
"required": ["name", "quantity"]
}
3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
]}.

{deps, [
{nova, ".*", {git, "https://github.com/novaframework/nova.git", {branch, "master"}}}
{nova, ".*", {git, "https://github.com/novaframework/nova.git", {branch, "master"}}},
{nova_json_schemas, ".*", {git, "https://github.com/novaframework/nova_json_schemas.git", {branch, "master"}}}
]}.


Expand Down
25 changes: 15 additions & 10 deletions rebar.config.script
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ NovaBranch = list_to_binary(os:getenv("NOVA_BRANCH", "master")),
NovaRepo = os:getenv("NOVA_REPO", "novaframework/nova"),
RepoUrl = "https://github.com/" ++ NovaRepo ++ ".git",

Deps = case NovaBranch of
<<"master">> -> {deps, [
{nova, ".*", {git, RepoUrl, {branch, "master"}}}
]};
<<"refs/tags/", Rest/binary>> ->
{deps, [{nova, ".*", {git, RepoUrl, {tag, binary_to_list(Rest)}}}]};
NovaBranch -> {deps, [
NovaDep = case NovaBranch of
<<"master">> ->
{nova, ".*", {git, RepoUrl, {branch, "master"}}};
<<"refs/tags/", Rest/binary>> ->
{nova, ".*", {git, RepoUrl, {tag, binary_to_list(Rest)}}};
NovaBranch ->
{nova, ".*", {git, RepoUrl, {branch, binary_to_list(NovaBranch)}}}
]}
end,
end,

%% Get existing deps from rebar.config (minus nova which we override)
ExistingDeps = case proplists:get_value(deps, Conf0) of
undefined -> [];
Deps0 -> [D || D <- Deps0, element(1, D) =/= nova]
end,

Deps = {deps, [NovaDep | ExistingDeps]},
Conf1 = proplists:delete(deps, Conf0),

[Deps|Conf1].

6 changes: 5 additions & 1 deletion src/controllers/nova_request_app_main_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
delete_user/1,
fallback/1,
return_view_with_ok/1,
return_view_with_view/1
return_view_with_view/1,
create_item/1
]).
-fallback_controller(nova_request_app_fallback_controller).

Expand Down Expand Up @@ -68,3 +69,6 @@ return_view_with_ok(_) ->
return_view_with_view(_) ->
{view, #{message => <<"Hello">>}}.

create_item(#{json := Json}) ->
{json, 201, #{}, Json}.

3 changes: 2 additions & 1 deletion src/nova_request_app.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
[
kernel,
stdlib,
nova
nova,
nova_json_schemas
]},
{env,[]},
{modules, []},
Expand Down
4 changes: 3 additions & 1 deletion src/nova_request_app_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ no_fun() ->
{"/internalerror", { nova_request_app_main_controller, internal_server_error}, #{methods => [get]}},
{"/fallback", { nova_request_app_main_controller, fallback}, #{methods => [get]}},
{"/viewok", { nova_request_app_main_controller, return_view_with_ok}, #{methods => [get]}},
{"/viewview", { nova_request_app_main_controller, return_view_with_view}, #{methods => [get]}}
{"/viewview", { nova_request_app_main_controller, return_view_with_view}, #{methods => [get]}},
{"/items", {nova_request_app_main_controller, create_item},
#{methods => [post], extra_state => #{json_schema => "schemas/create_item.json"}}}
]
},
#{prefix => "/json_binding",
Expand Down
20 changes: 16 additions & 4 deletions test/nova_request_app_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ all() ->
trailingslash,
fallback,
view_with_ok,
view_with_view].
view_with_view,
json_schema_valid,
json_schema_invalid].
%%--------------------------------------------------------------------
%% @spec TestCase(Config0) ->
%% ok | exit() | {skip,Reason} | {comment,Comment} |
Expand Down Expand Up @@ -292,9 +294,19 @@ websocket(Path, _Token) ->
exit(timeout)
end.

json_schema_valid(_) ->
Path = [?BASEPATH, <<"items">>],
Json = #{<<"name">> => <<"widget">>, <<"quantity">> => 5},
#{status := {201, _}, body := RespBody} = jhn_shttpc:post(Path, encode(Json), opts(json_post)),
#{<<"name">> := <<"widget">>, <<"quantity">> := 5} = decode(RespBody).

json_schema_invalid(_) ->
Path = [?BASEPATH, <<"items">>],
Json = #{<<"name">> => 123},
#{status := {400, _}} = jhn_shttpc:post(Path, encode(Json), opts(json_post)).

encode(Json) ->
thoas:encode(Json).
iolist_to_binary(json:encode(Json)).

decode(Json) ->
{ok, Result} = thoas:decode(Json),
Result.
json:decode(Json).
5 changes: 2 additions & 3 deletions test/nova_request_app_fun_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ websocket(Path, _Token) ->
end.

encode(Json) ->
thoas:encode(Json).
iolist_to_binary(json:encode(Json)).

decode(Json) ->
{ok, Result} = thoas:decode(Json),
Result.
json:decode(Json).
Loading