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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error and bump jesse #18

Merged
merged 4 commits into from Feb 20, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion rebar.config
@@ -1,4 +1,4 @@
{erl_opts, [debug_info]}.

{deps, [ yamerl,
{jesse, {git, "https://github.com/for-GET/jesse.git", {tag, "1.5.5"}}} ]}.
{jesse, {git, "https://github.com/for-GET/jesse.git", {ref, "cf075d2"}}} ]}.
2 changes: 1 addition & 1 deletion rebar.lock
@@ -1,7 +1,7 @@
{"1.2.0",
[{<<"jesse">>,
{git,"https://github.com/for-GET/jesse.git",
{ref,"1b5a16cc40c9b299ec825abcaf54a4bf140c4ba9"}},
{ref,"cf075d213ae9e9c54a748c93cc64d5350e646f9a"}},
0},
{<<"jsx">>,{pkg,<<"jsx">>,<<"2.9.0">>},0},
{<<"rfc3339">>,{pkg,<<"rfc3339">>,<<"0.2.2">>},1},
Expand Down
73 changes: 35 additions & 38 deletions src/swagger_endpoints.erl
Expand Up @@ -3,7 +3,7 @@
-export([init/1, from_yaml/2]).

-define(OAS2, "2.0").
-define(OAS3, "3.0.0").
-define(OAS3, "3.0").

%% Yaml parser outputs strings (list of characters).
%% Both jsx and jesse expect eitehr atoms or binaries
Expand All @@ -16,32 +16,32 @@ init(State) ->
{ok, State1}.

from_yaml(Doc, Options0) ->
OASVersion = oas_base_version(get_version(Doc)),
BaseUri =
case OASVersion of
?OAS2 -> proplists:get_value("basePath", Doc, "/");
?OAS3 ->
[[{"url", Prefix}]] = proplists:get_value("servers", Doc, [[{"url", "/"}]]),
Prefix
end,
Endpoints = proplists:get_value("paths", Doc, []),
Definitions0 =
case OASVersion of
?OAS2 -> proplists:get_value("definitions", Doc, []);
?OAS3 ->
Components = proplists:get_value("components", Doc, []),
proplists:get_value("schemas", Components, [])
end,
Parameters =
case OASVersion of
?OAS2 -> proplists:get_value("parameters", Doc, []);
?OAS3 ->
Cs = proplists:get_value("components", Doc, []),
proplists:get_value("parameters", Cs, [])
end,
Definitions = build_definitions(Definitions0, [], Options0, OASVersion),
Options = [{params, Parameters}, {defs, Definitions}, {baseuri, BaseUri} | Options0],
{Definitions, endpoint_map(Endpoints, #{}, Options, OASVersion), definitions_prefix(OASVersion)}.
OASVersion = get_version(Doc),
BaseUri =
case OASVersion of
?OAS2 -> proplists:get_value("basePath", Doc, "/");
?OAS3 ->
[[{"url", Prefix}]] = proplists:get_value("servers", Doc, [[{"url", "/"}]]),
Prefix
end,
Endpoints = proplists:get_value("paths", Doc, []),
Definitions0 =
case OASVersion of
?OAS2 -> proplists:get_value("definitions", Doc, []);
?OAS3 ->
Components = proplists:get_value("components", Doc, []),
proplists:get_value("schemas", Components, [])
end,
Parameters =
case OASVersion of
?OAS2 -> proplists:get_value("parameters", Doc, []);
?OAS3 ->
Cs = proplists:get_value("components", Doc, []),
proplists:get_value("parameters", Cs, [])
end,
Definitions = build_definitions(Definitions0, [], Options0, OASVersion),
Options = [{params, Parameters}, {defs, Definitions}, {baseuri, BaseUri} | Options0],
{Definitions, endpoint_map(Endpoints, #{}, Options, OASVersion), definitions_prefix(OASVersion)}.

build_definitions([], Definitions, _Options, _OASVersion) ->
Definitions;
Expand Down Expand Up @@ -155,7 +155,7 @@ params_to_json_schema(Params, Options, OASVersion) ->
[ case proplists:get_value("$ref", Param, none) of
none ->
case proplists:get_value("schema", Param, none) of
none when OASVersion =:= ?OAS2 ->
none when OASVersion =:= ?OAS2 ->
Param;
Schema when OASVersion =:= ?OAS2-> %% [{"$ref", Ref}]
lists:keydelete("schema", 1, Param) ++ [{"schema", to_json_schema(Schema, Options)}];
Expand Down Expand Up @@ -216,16 +216,13 @@ jesse_json_schema(Schema, _Options) ->
#{error => Schema}.

get_version(Doc) ->
case oas_base_version(proplists:get_value("swagger", Doc, not_set)) of
?OAS2 -> ?OAS2;
not_set ->
case oas_base_version(proplists:get_value("openapi", Doc)) of
?OAS3 -> ?OAS3
end
end.
case proplists:get_value("swagger", Doc, not_set) of
?OAS2 -> ?OAS2;
not_set ->
case proplists:get_value("openapi", Doc) of
?OAS3 ++ _ -> ?OAS3
end
end.

definitions_prefix(?OAS2) -> "/definitions/";
definitions_prefix(?OAS3) -> "/components/schemas/".

oas_base_version("2.0") -> ?OAS2;
oas_base_version("3.0." ++ _) -> ?OAS3.
20 changes: 10 additions & 10 deletions test/swagger_endpoints_tests.erl
Expand Up @@ -315,7 +315,7 @@ yaml(Opts) ->
, " email: " ++ G(email, "apiteam@aeternity.com")
, "basePath: " ++ G(basePath, "/v2")
, "tags:\n" ++ Tags
, "schemes:\n" ++ Schemes
, "schemes:\n" ++ Schemes
, Paths
, "definitions:\n" ++ Definitions
]);
Expand All @@ -341,7 +341,7 @@ yaml(Opts) ->
[Yaml] =yamerl_constr:string(Str),
Yaml.

multiline(L) -> lists:concat(lists:join("\n", lists:filter(fun(E) -> E =/= skip end, L))).
multiline(L) -> lists:concat(lists:join("\n", lists:filter(fun(E) -> E =/= skip end, L))).

encode_method({Method, #{ tags := Tags0
, operationid := OperationId
Expand Down Expand Up @@ -382,12 +382,12 @@ encode_method({Method, #{ tags := Tags0
?OAS2 ->
multiline(
[ " '" ++ integer_to_list(Code) ++ "':"
, " description: '" ++ RespDesr ++ "'"
, " description: '" ++ RespDesr ++ "'"
, MaybeSchema(" schema:\n", "")]);
?OAS3 ->
multiline(
[ " '" ++ integer_to_list(Code) ++ "':"
, " description: '" ++ RespDesr ++ "'"
, " description: '" ++ RespDesr ++ "'"
, MaybeSchema(multiline([ " content:\n"
, " application/json:\n"
, " schema:\n"]), " ")])
Expand Down Expand Up @@ -474,15 +474,15 @@ enc_definition(Spec, OASVersion) ->
VPrefix ++ " type: " ++ atom_to_list(Type)
end,
maps:to_list(Properties0))),
Required =
Required =
multiline(lists:map(
fun(K) -> VPrefix ++ " - " ++ atom_to_list(K) end,
Required0)),
multiline(
[ VPrefix ++ " " ++ Name ++ ":"
, VPrefix ++ " type: object"
, VPrefix ++ " properties:\n" ++ Properties
, VPrefix ++ " required:\n" ++ Required
, VPrefix ++ " required:\n" ++ Required
] ++ Optionals)
end.

Expand Down Expand Up @@ -521,7 +521,7 @@ req_get_keyblock_by_hash(OASVersion) ->
[#{ in => "path"
, name => "hash"
, description => "The hash of the block"
, required => "true"
, required => "true"
, type => "string" }],
Get =
request_method("get", ["external", "chain"],
Expand All @@ -537,7 +537,7 @@ req_post_keyblock(OASVersion) ->
[#{ in => "body"
, name => "body"
, description => "Mined key block"
, required => "true"
, required => "true"
, ref => definitions(OASVersion) ++ "KeyBlock" }],
Post =
request_method("post", ["internal", "chain"],
Expand All @@ -546,7 +546,7 @@ req_post_keyblock(OASVersion) ->
[ success_response(none),
error_response(400, "Invalid block", OASVersion)]),
{"/key-blocks", [Post]}.

success_response(RespObj) ->
response(200, "Successful operation", RespObj).

Expand Down Expand Up @@ -600,7 +600,7 @@ d_keyblock(OASVersion) ->
, time => "#" ++ definitions(OASVersion) ++ "UInt64"
, version => "#" ++ definitions(OASVersion) ++ "UInt32"
, info => "#" ++ definitions(OASVersion) ++ "EncodedByteArray"},
Required = maps:keys(Properties),
Required = lists:sort(maps:keys(Properties)),
{"KeyBlock", object, Properties, Required, #{}}.

definitions(?OAS2) -> "/definitions/";
Expand Down