refactor #20, add remoteRef fix to draft3 as well

This commit is contained in:
Andrei Neculau (anu) 2016-06-19 01:03:42 +02:00
parent e2404b91f0
commit a1ab58c42b
No known key found for this signature in database
GPG Key ID: 3570C6DE977687AA
7 changed files with 87 additions and 17 deletions

View File

@ -38,7 +38,7 @@
, set_error_list/2
, find_schema/2
, resolve_reference/2
, leave_reference/2
, undo_resolve_reference/2
]).
-export_type([ state/0
@ -213,10 +213,13 @@ resolve_reference(State, Reference) ->
end
end.
%% @doc Revert changes, made by resolve_reference
-spec leave_reference(state(), state()) -> state().
leave_reference(RefState, #state{root_schema = RootSchema, id = Id}) ->
RefState#state{root_schema = RootSchema, id = Id}.
%% @doc Revert changes made by resolve_reference
-spec undo_resolve_reference(state(), state()) -> state().
undo_resolve_reference(RefState, OriginalState) ->
RefState#state{ root_schema = OriginalState#state.root_schema
, current_schema = OriginalState#state.current_schema
, id = OriginalState#state.id
}.
%% @doc Retrive a specific part of a schema
%% @private

View File

@ -209,8 +209,9 @@ check_value(Value, [{?EXTENDS, Extends} | Attrs], State) ->
NewState = check_extends(Value, Extends, State),
check_value(Value, Attrs, NewState);
check_value(Value, [{?REF, RefSchemaURI} | Attrs], State) ->
NewState = check_ref(Value, RefSchemaURI, State),
check_value(Value, Attrs, NewState);
NewState = resolve_ref(Value, RefSchemaURI, State),
NewState2 = check_value(Value, Attrs, NewState),
undo_resolve_ref(NewState2, State);
check_value(_Value, [], State) ->
State;
check_value(Value, [_Attr | Attrs], State) ->
@ -897,11 +898,14 @@ check_extends_array(Value, Extends, State) ->
).
%% @private
check_ref(Value, Reference, State) ->
resolve_ref(Value, Reference, State) ->
NewState = jesse_state:resolve_reference(State, Reference),
Schema = get_current_schema(NewState),
jesse_schema_validator:validate_with_state(Schema, Value, NewState).
undo_resolve_ref(State, OriginalState) ->
jesse_state:undo_resolve_reference(State, OriginalState).
%%=============================================================================
%% @doc Returns `true' if given values (instance) are equal, otherwise `false'
%% is returned.

View File

@ -247,8 +247,8 @@ check_value(Value, [{?NOT, Schema} | Attrs], State) ->
check_value(Value, Attrs, NewState);
check_value(Value, [{?REF, Reference} | Attrs], State) ->
NewState = resolve_ref(Value, Reference, State),
RefState = check_value(Value, Attrs, NewState),
leave_ref(RefState, State);
NewState2 = check_value(Value, Attrs, NewState),
undo_resolve_ref(NewState2, State);
check_value(_Value, [], State) ->
State;
check_value(Value, [_Attr | Attrs], State) ->
@ -1190,11 +1190,8 @@ resolve_ref(Value, Reference, State) ->
Schema = get_current_schema(NewState),
jesse_schema_validator:validate_with_state(Schema, Value, NewState).
%% @doc Revert some parts of state when reference goes out of scope
leave_ref(RefState, OrigState) ->
%% Replace `root_schema` and `id` in RefState from State.
jesse_state:leave_reference(RefState, OrigState).
undo_resolve_ref(State, OriginalState) ->
jesse_state:undo_resolve_reference(State, OriginalState).
%%=============================================================================
%% @doc Returns `true' if given values (instance) are equal, otherwise `false'

View File

@ -0,0 +1,63 @@
[
{
"description": "state corruption $ref in 'items'",
"schema": {
"properties": {
"0_list": {
"items": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
},
"1_any": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
}
},
"tests": [
{
"description": "will trigger state corruption before this fix",
"data": {"0_list": 1,
"1_any": 1},
"valid": true
}
]
},
{
"description": "state corruption $ref in 'properties'",
"schema": {
"properties": {
"0_object": {
"properties": {
"0_prop": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
}
},
"1_any": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
}
},
"tests": [
{
"description": "will trigger state corruption before this fix",
"data": {"0_obj": {"0_prop": 1},
"1_any": 1},
"valid": true
}
]
},
{
"description": "state corruption $ref in 'patternProperties'",
"schema": {
"properties": {
"0_object": {
"patternProperties": {
"0_prop": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
}
},
"1_any": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
}
},
"tests": [
{
"description": "will trigger state corruption before this fix",
"data": {"0_obj": {"0_prop": 1},
"1_any": 1},
"valid": true
}
]
}
]

View File

@ -137,3 +137,6 @@ uniqueItems(Config) ->
itemsExtra(Config) ->
do_test("itemsExtra", Config).
remoteRefExtra(Config) ->
do_test("remoteRefExtra", Config).

View File

@ -153,5 +153,5 @@ uniqueItems(Config) ->
itemsExtra(Config) ->
do_test("itemsExtra", Config).
remote_ref_regression_case(Config) ->
do_test("remoteRefRegression_19", Config).
remoteRefExtra(Config) ->
do_test("remoteRefExtra", Config).