Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump JSON Schema test suite #109

Merged
merged 6 commits into from
Dec 8, 2021
Merged
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
142 changes: 76 additions & 66 deletions src/jesse_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,19 @@ set_allowed_errors(#state{} = State, AllowedErrors) ->
-spec set_current_schema( State :: state()
, NewSchema :: jesse:schema()
) -> state().
set_current_schema(#state{id = Id} = State, NewSchema) ->
NewId = combine_id(Id, jesse_json_path:value(?ID, NewSchema, undefined)),
set_current_schema(#state{id = Id} = State, NewSchema0) ->
NewSchema =
case jesse_json_path:value(?REF, NewSchema0, undefined) of
undefined ->
NewSchema0;
Ref ->
%% Instead of just removing all the other fields, we put schema as
%% 1st element, so, only `ref' will be validated, while other fields
%% (say, `definitions') may still be referenced.
[{?REF, Ref} | lists:keydelete(?REF, 1, NewSchema0)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it crashes when the schema is a map. We caught this today before shipping some code to production... is there a reason not to support map format schemas here? It seems to work elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this was not intentional. I'll make a fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nalundgaard, may you please check that fix from #113 works for you?

end,
NewSchemaId = jesse_json_path:value(?ID, NewSchema, undefined),
NewId = combine_id(Id, NewSchemaId),
State#state{current_schema = NewSchema, id = NewId}.

%% @doc Setter for `error_list'.
Expand All @@ -200,43 +211,35 @@ resolve_ref(State, Reference) ->
_ ->
binary_to_list(BaseURI) =:= Id
end,
case IsLocalReference of
true ->
Path = jesse_json_path:parse(Pointer),
case load_local_schema(State#state.root_schema, Path) of
?not_found ->
jesse_error:handle_schema_invalid( { ?schema_not_found
, CanonicalReference}
, State
);
Schema ->
set_current_schema(State, Schema)
end;
false ->
case load_schema(State, BaseURI) of
?not_found ->
jesse_error:handle_schema_invalid( { ?schema_not_found
, CanonicalReference}
, State
);
RemoteSchema ->
SchemaVer =
jesse_json_path:value(?SCHEMA, RemoteSchema, ?default_schema_ver),
NewState = State#state{ root_schema = RemoteSchema
, id = BaseURI
, default_schema_ver = SchemaVer
},
Path = jesse_json_path:parse(Pointer),
case load_local_schema(RemoteSchema, Path) of
?not_found ->
jesse_error:handle_schema_invalid( { ?schema_not_found
, CanonicalReference}
, State
);
Schema ->
set_current_schema(NewState, Schema)
end
end
{State1, BaseSchema} =
case IsLocalReference of
true ->
{State, State#state.root_schema};
false ->
case load_schema(State, BaseURI) of
?not_found ->
jesse_error:handle_schema_invalid( { ?schema_not_found
, CanonicalReference}
, State
);
RemoteSchema ->
SchemaVer =
jesse_json_path:value(
?SCHEMA, RemoteSchema, State#state.default_schema_ver),
RemoteState = State#state{ root_schema = RemoteSchema
, id = BaseURI
, default_schema_ver = SchemaVer
},
{RemoteState, RemoteSchema}
end
end,
Path = jesse_json_path:parse(Pointer),
try load_local_schema(set_current_schema(State1, BaseSchema), Path)
catch throw:?not_found ->
jesse_error:handle_schema_invalid( { ?schema_not_found
, CanonicalReference}
, State1
)
end.

%% @doc Revert changes made by resolve_reference.
Expand All @@ -250,40 +253,47 @@ undo_resolve_ref(RefState, OriginalState) ->

%% @doc Retrieve a specific part of a schema
%% @private
-spec load_local_schema( Schema :: ?not_found | jesse:schema()
-spec load_local_schema( Schema :: state() | ?not_found
, Path :: [binary()]
) -> not_found | jesse:json_term().
load_local_schema(?not_found, _Path) ->
?not_found;
load_local_schema(Schema, []) ->
case jesse_lib:is_json_object(Schema) of
load_local_schema(St, []) ->
St;
load_local_schema(St, [<<>> | Keys]) ->
load_local_schema(St, Keys);
load_local_schema(St, [Key | Keys]) ->
Schema = get_current_schema(St),
SubSchema = jesse_json_path:value(Key, Schema, ?not_found),
(SubSchema =/= ?not_found) orelse throw(?not_found),
{ObjectSubSchema, Keys1} =
case jesse_lib:is_json_object(SubSchema) of
true ->
{SubSchema, Keys};
false ->
choose_array(SubSchema, Keys)
end,
load_local_schema(
set_current_schema(St, ObjectSubSchema),
Keys1).

choose_array(SubSchema, [Key | Keys] = AllKeys) ->
case jesse_lib:is_array(SubSchema) of
true ->
Schema;
false ->
?not_found
end;
load_local_schema(Schema, [<<>> | Keys]) ->
load_local_schema(Schema, Keys);
load_local_schema(Schema, [Key | Keys]) ->
case jesse_lib:is_json_object(Schema) of
true ->
SubSchema = jesse_json_path:value(Key, Schema, ?not_found),
load_local_schema(SubSchema, Keys);
try binary_to_integer(Key) of
Index ->
choose_array(lists:nth(Index + 1, SubSchema), Keys)
catch
_:_ -> throw(?not_found)
end;
false ->
case jesse_lib:is_array(Schema) of
case jesse_lib:is_json_object(SubSchema) of
true ->
%% avoiding binary_to_integer to maintain R15 compatibility
try list_to_integer(binary_to_list(Key)) of
Index ->
SubSchema = lists:nth(Index + 1, Schema),
load_local_schema(SubSchema, Keys)
catch
_:_ -> ?not_found
end;
{SubSchema, AllKeys};
false ->
?not_found
throw(?not_found)
end
end.
end;
choose_array(Schema, []) ->
{Schema, []}.

%% github.com/erlang/otp/blob/OTP-20.2.3/lib/inets/doc/src/http_uri.xml#L57
-type http_uri_uri() :: string() | unicode:unicode_binary().
Expand Down
46 changes: 28 additions & 18 deletions src/jesse_tests_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
]).

-include_lib("common_test/include/ct.hrl").
-include_lib("stdlib/include/assert.hrl").

%% JSON-Schema-Test-Suite attributes definitions
-define(DATA, <<"data">>).
Expand Down Expand Up @@ -62,21 +63,29 @@ get_tests(RelativeTestsDir, DefaultSchema, Config) ->

do_test(Key, Config) ->
{Tests, DefaultSchema} = ?config(Key, Config),
lists:foreach( fun(Test) ->
Description = get_path(?DESCRIPTION, Test),
Schema = get_path(?SCHEMA, Test),
SchemaTests = get_path(?TESTS, Test),
Options = get_path(?OPTIONS, Test),
ct:pal( "** Description: ~s~n"
"** Options: ~p~n"
"** Schema: ~p~n"
"** Schema tests: ~p~n"
, [Description, Options, Schema, SchemaTests]
),
test_schema(DefaultSchema, Options, Schema, SchemaTests)
end
, Tests
).
SkipList = ?config(skip_list, Config),
lists:foreach(
fun(Test) ->
Description = get_path(?DESCRIPTION, Test),
Schema = get_path(?SCHEMA, Test),
SchemaTests = get_path(?TESTS, Test),
Options = get_path(?OPTIONS, Test),
ct:pal( "** Description: ~s~n"
"** Options: ~p~n"
"** Schema: ~p~n"
"** Schema tests: ~p~n"
, [Description, Options, Schema, SchemaTests]
),
case lists:member({list_to_binary(Key), Description},
SkipList) of
true ->
ct:pal("In skip-list");
false ->
test_schema(DefaultSchema, Options, Schema, SchemaTests)
end
end
, Tests
).

%%% Internal functions

Expand All @@ -94,9 +103,9 @@ test_schema(DefaultSchema, Opts0, Schema, SchemaTests) ->
ct:pal("Result: ~p~n", [Result]),
case get_path(?VALID, Test) of
true ->
{ok, Instance} = Result;
?assertEqual({ok, Instance}, Result);
false ->
{error, _} = Result;
?assertMatch({error, _}, Result);
ExpectedErrors ->
{error, Errors} = Result,
GotErrors =
Expand All @@ -109,7 +118,8 @@ test_schema(DefaultSchema, Opts0, Schema, SchemaTests) ->
ct:pal( "Error: ~p:~p~n"
"Stacktrace: ~p~n"
, [C, R, Stacktrace]
)
),
erlang:raise(C, R, Stacktrace)
end
end
, SchemaTests
Expand Down
29 changes: 13 additions & 16 deletions src/jesse_validator_draft3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,8 @@
, JsonSchema :: jesse:schema()
, State :: jesse_state:state()
) -> jesse_state:state() | no_return().
check_value(Value, [{?REF, RefSchemaURI} | Attrs], State) ->
case Attrs of
[] ->
validate_ref(Value, RefSchemaURI, State);
_ ->
handle_schema_invalid(?only_ref_allowed, State)
end;
check_value(Value, [{?REF, RefSchemaURI} | _], State) ->
validate_ref(Value, RefSchemaURI, State);
check_value(Value, [{?TYPE, Type} | Attrs], State) ->
NewState = check_type(Value, Type, State),
check_value(Value, Attrs, NewState);
Expand Down Expand Up @@ -889,15 +884,17 @@ check_disallow(Value, Disallow, State) ->
%% attributes, or add other constraints.
%% @private
check_extends(Value, Extends, State) ->
case jesse_lib:is_json_object(Extends) of
true ->
check_value(Value, Extends, set_current_schema(State, Extends));
false ->
case is_list(Extends) of
true -> check_extends_array(Value, Extends, State);
false -> State %% TODO: implement handling of $ref
end
end.
TmpState =
case jesse_lib:is_json_object(Extends) of
true ->
check_value(Value, Extends, set_current_schema(State, Extends));
false ->
case is_list(Extends) of
true -> check_extends_array(Value, Extends, State);
false -> State %% TODO: implement handling of $ref
end
end,
set_current_schema(TmpState, get_current_schema(State)).

%% @private
check_extends_array(Value, Extends, State) ->
Expand Down
13 changes: 4 additions & 9 deletions src/jesse_validator_draft4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,8 @@
, JsonSchema :: jesse:schema()
, State :: jesse_state:state()
) -> jesse_state:state() | no_return().
check_value(Value, [{?REF, RefSchemaURI} | Attrs], State) ->
case Attrs of
[] ->
validate_ref(Value, RefSchemaURI, State);
_ ->
handle_schema_invalid(?only_ref_allowed, State)
end;
check_value(Value, [{?REF, RefSchemaURI} | _], State) ->
validate_ref(Value, RefSchemaURI, State);
check_value(Value, [{?TYPE, Type} | Attrs], State) ->
NewState = check_type(Value, Type, State),
check_value(Value, Attrs, NewState);
Expand Down Expand Up @@ -1235,7 +1230,7 @@ validate_schema(Value, Schema, State0) ->
, Value
, State1
),
{true, State2};
{true, set_current_schema(State2, get_current_schema(State0))};
false ->
handle_schema_invalid(?schema_invalid, State0)
end
Expand Down Expand Up @@ -1297,7 +1292,7 @@ is_equal(Value1, Value2) ->
true -> compare_objects(Value1, Value2);
false -> case is_list(Value1) andalso is_list(Value2) of
true -> compare_lists(Value1, Value2);
false -> Value1 =:= Value2
false -> Value1 == Value2
end
end.

Expand Down
2 changes: 1 addition & 1 deletion test/JSON-Schema-Test-Suite
1 change: 1 addition & 0 deletions test/jesse_tests_draft3_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ init_per_suite(Config) ->
++ get_tests( "extra"
, <<"http://json-schema.org/draft-03/schema#">>
, Config)
++ [{skip_list, []}]
++ Config.

end_per_suite(_Config) ->
Expand Down
8 changes: 8 additions & 0 deletions test/jesse_tests_draft4_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,20 @@ all() ->

init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(jesse),
SkipList =
[
{<<"ref">>, <<"Location-independent identifier">>}
, {<<"ref">>, <<"Recursive references between schemas">>}
, {<<"ref">>, <<"Location-independent identifier with base URI"
" change in subschema">>}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, 3 test-cases are skipped. Seems all 3 require "arbitrary document-local reference URI lookups" / "location indpendent identifiers", but it seems it would be very difficult to implement them efficiently. Naive way would be to just do recursive descent from #state.root_schema.

],
get_tests( "standard"
, <<"http://json-schema.org/draft-04/schema#">>
, Config)
++ get_tests( "extra"
, <<"http://json-schema.org/draft-04/schema#">>
, Config)
++ [{skip_list, SkipList}]
++ Config.

end_per_suite(_Config) ->
Expand Down