Skip to content

Commit 965e02e

Browse files
authored
fix: inlines nested refs in generated suites (#53)
1 parent 46dd216 commit 965e02e

3 files changed

Lines changed: 193 additions & 6 deletions

File tree

‎src/restcheck_suite.erl‎

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ generate(API) ->
7474
erf_util:to_pascal_case(Name)
7575
)
7676
),
77-
Schema = schema_ast(maps:get(Ref, Schemas)),
77+
Schema = schema_ast(maps:get(Ref, Schemas), Schemas),
7878
Generator =
7979
erl_syntax:application(
8080
erl_syntax:atom(restcheck_pbt),
@@ -101,7 +101,7 @@ generate(API) ->
101101
erf_util:to_pascal_case(RequestBody)
102102
)
103103
),
104-
RequestBodySchema = schema_ast(maps:get(RequestBody, Schemas)),
104+
RequestBodySchema = schema_ast(maps:get(RequestBody, Schemas), Schemas),
105105
RequestBodyGenerator = erl_syntax:application(
106106
erl_syntax:atom(restcheck_pbt),
107107
erl_syntax:atom(noshrink),
@@ -651,8 +651,88 @@ prop_ast(RawPath, Method, Parameters, RequestBody, Responses) ->
651651
)
652652
].
653653

654-
-spec schema_ast(Schema) -> SchemaAST when
654+
-spec schema_ast(Schema, Schemas) -> SchemaAST when
655655
Schema :: erf_parser:schema(),
656+
Schemas :: #{binary() => erf_parser:schema()},
656657
SchemaAST :: erl_syntax:syntaxTree().
657-
schema_ast(Schema) ->
658-
erl_syntax:abstract(Schema).
658+
schema_ast(Schema, Schemas) ->
659+
erl_syntax:abstract(inline_refs(Schema, Schemas, [])).
660+
661+
-spec inline_refs(Schema, Schemas, Seen) -> Inlined when
662+
Schema :: erf_parser:schema(),
663+
Schemas :: #{binary() => erf_parser:schema()},
664+
Seen :: [erf_parser:ref()],
665+
Inlined :: erf_parser:schema().
666+
inline_refs(#{ref := Ref}, Schemas, Seen) ->
667+
case lists:member(Ref, Seen) of
668+
true ->
669+
#{};
670+
false ->
671+
inline_refs(maps:get(Ref, Schemas), Schemas, [Ref | Seen])
672+
end;
673+
inline_refs(#{all_of := Subschemas} = Schema, Schemas, Seen) ->
674+
Schema#{all_of => [inline_refs(Subschema, Schemas, Seen) || Subschema <- Subschemas]};
675+
inline_refs(#{any_of := Subschemas} = Schema, Schemas, Seen) ->
676+
Schema#{any_of => [inline_refs(Subschema, Schemas, Seen) || Subschema <- Subschemas]};
677+
inline_refs(#{one_of := Subschemas} = Schema, Schemas, Seen) ->
678+
Schema#{one_of => [inline_refs(Subschema, Schemas, Seen) || Subschema <- Subschemas]};
679+
inline_refs(#{'not' := Subschema} = Schema, Schemas, Seen) ->
680+
Schema#{'not' => inline_refs(Subschema, Schemas, Seen)};
681+
inline_refs(#{type := array} = Schema, Schemas, Seen) ->
682+
inline_array_refs(Schema, Schemas, Seen);
683+
inline_refs(#{type := object} = Schema, Schemas, Seen) ->
684+
inline_object_refs(Schema, Schemas, Seen);
685+
inline_refs(Schema, _Schemas, _Seen) ->
686+
Schema.
687+
688+
-spec inline_array_refs(Schema, Schemas, Seen) -> Inlined when
689+
Schema :: ndto:array_schema(),
690+
Schemas :: #{binary() => erf_parser:schema()},
691+
Seen :: [erf_parser:ref()],
692+
Inlined :: ndto:array_schema().
693+
inline_array_refs(Schema, Schemas, Seen) ->
694+
WithItems =
695+
case Schema of
696+
#{items := Items} when is_list(Items) ->
697+
Schema#{items => [inline_refs(Item, Schemas, Seen) || Item <- Items]};
698+
#{items := Items} ->
699+
Schema#{items => inline_refs(Items, Schemas, Seen)};
700+
_NoItems ->
701+
Schema
702+
end,
703+
case WithItems of
704+
#{additional_items := AdditionalItems} ->
705+
WithItems#{additional_items => inline_refs(AdditionalItems, Schemas, Seen)};
706+
_NoAdditionalItems ->
707+
WithItems
708+
end.
709+
710+
-spec inline_object_refs(Schema, Schemas, Seen) -> Inlined when
711+
Schema :: ndto:object_schema(),
712+
Schemas :: #{binary() => erf_parser:schema()},
713+
Seen :: [erf_parser:ref()],
714+
Inlined :: ndto:object_schema().
715+
inline_object_refs(Schema, Schemas, Seen) ->
716+
Inline = fun(_Name, Subschema) -> inline_refs(Subschema, Schemas, Seen) end,
717+
WithProperties =
718+
case Schema of
719+
#{properties := Properties} ->
720+
Schema#{properties => maps:map(Inline, Properties)};
721+
_NoProperties ->
722+
Schema
723+
end,
724+
WithPatternProperties =
725+
case WithProperties of
726+
#{pattern_properties := PatternProperties} ->
727+
WithProperties#{pattern_properties => maps:map(Inline, PatternProperties)};
728+
_NoPatternProperties ->
729+
WithProperties
730+
end,
731+
case WithPatternProperties of
732+
#{additional_properties := AdditionalProperties} ->
733+
WithPatternProperties#{
734+
additional_properties => inline_refs(AdditionalProperties, Schemas, Seen)
735+
};
736+
_NoAdditionalProperties ->
737+
WithPatternProperties
738+
end.

‎test/restcheck_SUITE.erl‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
-module(restcheck_SUITE).
1515

1616
%%% INCLUDE FILES
17+
-include_lib("common_test/include/ct.hrl").
1718
-include_lib("stdlib/include/assert.hrl").
1819

1920
%%% EXTERNAL EXPORTS
@@ -24,7 +25,8 @@
2425
%%%-----------------------------------------------------------------------------
2526
all() ->
2627
[
27-
petstore
28+
petstore,
29+
nested_refs
2830
].
2931

3032
%%%-----------------------------------------------------------------------------
@@ -131,6 +133,38 @@ petstore(_Conf) ->
131133

132134
ok.
133135

136+
nested_refs(Conf) ->
137+
meck:expect(
138+
restcheck_client_server,
139+
handle,
140+
fun([<<"orders">>], 'POST', _Headers, _QueryParameters, RawBody) ->
141+
case njson:decode(RawBody) of
142+
{ok, #{<<"item">> := #{<<"sku">> := Sku, <<"qty">> := Qty}}} when
143+
is_binary(Sku), is_integer(Qty)
144+
->
145+
{200, [{<<"Content-Type">>, <<"application/json">>}], RawBody};
146+
_Otherwise ->
147+
{400, [], <<>>}
148+
end
149+
end
150+
),
151+
152+
RunConf = #{
153+
spec_path => unicode:characters_to_binary(
154+
filename:join(?config(data_dir, Conf), "nested_refs.json")
155+
),
156+
spec_format => erf_parser_oas_3_0,
157+
pbt_backend => restcheck_triq,
158+
host => <<"localhost">>,
159+
port => 8080,
160+
ssl => false,
161+
timeout => 5000,
162+
num_requests => 10
163+
},
164+
165+
?assertMatch({ok, [{<<"create_order">>, true}]}, restcheck:run(RunConf)),
166+
167+
ok.
134168
%%%-----------------------------------------------------------------------------
135169
%%% INTERNAL FUNCTIONS
136170
%%%-----------------------------------------------------------------------------
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Nested refs API",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/orders": {
9+
"post": {
10+
"summary": "Create an order",
11+
"operationId": "createOrder",
12+
"requestBody": {
13+
"required": true,
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"$ref": "#/components/schemas/Order"
18+
}
19+
}
20+
}
21+
},
22+
"responses": {
23+
"200": {
24+
"description": "The created order",
25+
"content": {
26+
"application/json": {
27+
"schema": {
28+
"$ref": "#/components/schemas/Order"
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
36+
},
37+
"components": {
38+
"schemas": {
39+
"Order": {
40+
"type": "object",
41+
"required": [
42+
"item"
43+
],
44+
"properties": {
45+
"item": {
46+
"$ref": "#/components/schemas/Item"
47+
}
48+
},
49+
"additionalProperties": false
50+
},
51+
"Item": {
52+
"type": "object",
53+
"required": [
54+
"sku",
55+
"qty"
56+
],
57+
"properties": {
58+
"sku": {
59+
"type": "string",
60+
"minLength": 1,
61+
"maxLength": 8
62+
},
63+
"qty": {
64+
"type": "integer",
65+
"minimum": 1,
66+
"maximum": 10
67+
}
68+
},
69+
"additionalProperties": false
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)