Skip to content
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
90 changes: 85 additions & 5 deletions src/restcheck_suite.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ generate(API) ->
erf_util:to_pascal_case(Name)
)
),
Schema = schema_ast(maps:get(Ref, Schemas)),
Schema = schema_ast(maps:get(Ref, Schemas), Schemas),
Generator =
erl_syntax:application(
erl_syntax:atom(restcheck_pbt),
Expand All @@ -101,7 +101,7 @@ generate(API) ->
erf_util:to_pascal_case(RequestBody)
)
),
RequestBodySchema = schema_ast(maps:get(RequestBody, Schemas)),
RequestBodySchema = schema_ast(maps:get(RequestBody, Schemas), Schemas),
RequestBodyGenerator = erl_syntax:application(
erl_syntax:atom(restcheck_pbt),
erl_syntax:atom(noshrink),
Expand Down Expand Up @@ -651,8 +651,88 @@ prop_ast(RawPath, Method, Parameters, RequestBody, Responses) ->
)
].

-spec schema_ast(Schema) -> SchemaAST when
-spec schema_ast(Schema, Schemas) -> SchemaAST when
Schema :: erf_parser:schema(),
Schemas :: #{binary() => erf_parser:schema()},
SchemaAST :: erl_syntax:syntaxTree().
schema_ast(Schema) ->
erl_syntax:abstract(Schema).
schema_ast(Schema, Schemas) ->
erl_syntax:abstract(inline_refs(Schema, Schemas, [])).

-spec inline_refs(Schema, Schemas, Seen) -> Inlined when
Schema :: erf_parser:schema(),
Schemas :: #{binary() => erf_parser:schema()},
Seen :: [erf_parser:ref()],
Inlined :: erf_parser:schema().
inline_refs(#{ref := Ref}, Schemas, Seen) ->
case lists:member(Ref, Seen) of
true ->
#{};
false ->
inline_refs(maps:get(Ref, Schemas), Schemas, [Ref | Seen])
end;
inline_refs(#{all_of := Subschemas} = Schema, Schemas, Seen) ->
Schema#{all_of => [inline_refs(Subschema, Schemas, Seen) || Subschema <- Subschemas]};
inline_refs(#{any_of := Subschemas} = Schema, Schemas, Seen) ->
Schema#{any_of => [inline_refs(Subschema, Schemas, Seen) || Subschema <- Subschemas]};
inline_refs(#{one_of := Subschemas} = Schema, Schemas, Seen) ->
Schema#{one_of => [inline_refs(Subschema, Schemas, Seen) || Subschema <- Subschemas]};
inline_refs(#{'not' := Subschema} = Schema, Schemas, Seen) ->
Schema#{'not' => inline_refs(Subschema, Schemas, Seen)};
inline_refs(#{type := array} = Schema, Schemas, Seen) ->
inline_array_refs(Schema, Schemas, Seen);
inline_refs(#{type := object} = Schema, Schemas, Seen) ->
inline_object_refs(Schema, Schemas, Seen);
inline_refs(Schema, _Schemas, _Seen) ->
Schema.

-spec inline_array_refs(Schema, Schemas, Seen) -> Inlined when
Schema :: ndto:array_schema(),
Schemas :: #{binary() => erf_parser:schema()},
Seen :: [erf_parser:ref()],
Inlined :: ndto:array_schema().
inline_array_refs(Schema, Schemas, Seen) ->
WithItems =
case Schema of
#{items := Items} when is_list(Items) ->
Schema#{items => [inline_refs(Item, Schemas, Seen) || Item <- Items]};
#{items := Items} ->
Schema#{items => inline_refs(Items, Schemas, Seen)};
_NoItems ->
Schema
end,
case WithItems of
#{additional_items := AdditionalItems} ->
WithItems#{additional_items => inline_refs(AdditionalItems, Schemas, Seen)};
_NoAdditionalItems ->
WithItems
end.

-spec inline_object_refs(Schema, Schemas, Seen) -> Inlined when
Schema :: ndto:object_schema(),
Schemas :: #{binary() => erf_parser:schema()},
Seen :: [erf_parser:ref()],
Inlined :: ndto:object_schema().
inline_object_refs(Schema, Schemas, Seen) ->
Inline = fun(_Name, Subschema) -> inline_refs(Subschema, Schemas, Seen) end,
WithProperties =
case Schema of
#{properties := Properties} ->
Schema#{properties => maps:map(Inline, Properties)};
_NoProperties ->
Schema
end,
WithPatternProperties =
case WithProperties of
#{pattern_properties := PatternProperties} ->
WithProperties#{pattern_properties => maps:map(Inline, PatternProperties)};
_NoPatternProperties ->
WithProperties
end,
case WithPatternProperties of
#{additional_properties := AdditionalProperties} ->
WithPatternProperties#{
additional_properties => inline_refs(AdditionalProperties, Schemas, Seen)
};
_NoAdditionalProperties ->
WithPatternProperties
end.
36 changes: 35 additions & 1 deletion test/restcheck_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
-module(restcheck_SUITE).

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

%%% EXTERNAL EXPORTS
Expand All @@ -24,7 +25,8 @@
%%%-----------------------------------------------------------------------------
all() ->
[
petstore
petstore,
nested_refs
].

%%%-----------------------------------------------------------------------------
Expand Down Expand Up @@ -131,6 +133,38 @@ petstore(_Conf) ->

ok.

nested_refs(Conf) ->
meck:expect(
restcheck_client_server,
handle,
fun([<<"orders">>], 'POST', _Headers, _QueryParameters, RawBody) ->
case njson:decode(RawBody) of
{ok, #{<<"item">> := #{<<"sku">> := Sku, <<"qty">> := Qty}}} when
is_binary(Sku), is_integer(Qty)
->
{200, [{<<"Content-Type">>, <<"application/json">>}], RawBody};
_Otherwise ->
{400, [], <<>>}
end
end
),

RunConf = #{
spec_path => unicode:characters_to_binary(
filename:join(?config(data_dir, Conf), "nested_refs.json")
),
spec_format => erf_parser_oas_3_0,
pbt_backend => restcheck_triq,
host => <<"localhost">>,
port => 8080,
ssl => false,
timeout => 5000,
num_requests => 10
},

?assertMatch({ok, [{<<"create_order">>, true}]}, restcheck:run(RunConf)),

ok.
%%%-----------------------------------------------------------------------------
%%% INTERNAL FUNCTIONS
%%%-----------------------------------------------------------------------------
Expand Down
73 changes: 73 additions & 0 deletions test/restcheck_SUITE_data/nested_refs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"openapi": "3.0.0",
"info": {
"title": "Nested refs API",
"version": "1.0.0"
},
"paths": {
"/orders": {
"post": {
"summary": "Create an order",
"operationId": "createOrder",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Order"
}
}
}
},
"responses": {
"200": {
"description": "The created order",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Order"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Order": {
"type": "object",
"required": [
"item"
],
"properties": {
"item": {
"$ref": "#/components/schemas/Item"
}
},
"additionalProperties": false
},
"Item": {
"type": "object",
"required": [
"sku",
"qty"
],
"properties": {
"sku": {
"type": "string",
"minLength": 1,
"maxLength": 8
},
"qty": {
"type": "integer",
"minimum": 1,
"maximum": 10
}
},
"additionalProperties": false
}
}
}
}
Loading