Skip to content

Commit

Permalink
feat(nomasystems#59): Pass ContType w/ schemas for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
LoisSotoLopez committed Mar 3, 2024
1 parent 3ef587b commit b3a6fbd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/erf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,13 @@ init([Name, RawConf]) ->
Reason :: term().
build_dtos([]) ->
ok;
build_dtos([{Ref, [{_ContentType, Schema}]} | Schemas]) ->
build_dtos([{Ref, Schema} | Schemas]);
build_dtos([{Ref, Schema} | Schemas]) ->
Name = erlang:binary_to_atom(Ref),
DTO = ndto:generate(Name, Schema),
io:format("Schema is ~p~n", [Schema]),
file:write_file(Name, erl_prettypr:format(DTO)),
case ndto:load(DTO) of
ok ->
build_dtos(Schemas);
Expand Down
8 changes: 5 additions & 3 deletions src/erf_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ref := ref(),
required := boolean()
}.
-type content_type() :: binary().
-type endpoint() :: #{
path := path(),
parameters := [parameter()],
Expand All @@ -57,18 +58,19 @@
-type path() :: binary().
-type ref() :: binary().
-type request() :: #{
body := body()
body := #{content_type() => body()}
}.
-type response() :: #{
body := body()
body := #{content_type() => body()}
}.
-type schema() :: ndto:schema().
-type schema() :: ndto:schema() | {content_type(), ndto:schema()}.
-type status_code() :: 100..599.

%%% TYPE EXPORTS
-export_type([
api/0,
body/0,
content_type/0,
endpoint/0,
method/0,
operation/0,
Expand Down
11 changes: 5 additions & 6 deletions src/erf_parser/erf_parser_oas_3_0.erl
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,19 @@ parse_parameter(#{<<"schema">> := RawSchema} = RawParameter, #{namespace := Name
OAS :: spec(),
CTX :: ctx(),
Result :: {RequestBody, ExtraSchemas, NewCTX},
RequestBody :: #{schema := erf_parser:schema(), required := boolean()},
RequestBody :: #{schema := [{erf_parser:content_type(), erf_parser:schema()}], required := boolean()},
ExtraSchemas :: [{erf_parser:ref(), erf_parser:schema()}],
NewCTX :: ctx().
parse_request_body(#{<<"$ref">> := Ref}, CTX) ->
{_RefResolved, RefOAS, RefCTX} = resolve_ref(Ref, CTX),
parse_request_body(RefOAS, RefCTX);
parse_request_body(#{<<"content">> := Content} = ReqBody, CTX) ->
Required = maps:get(<<"required">>, ReqBody, false),
{AnyOf, ExtraSchemas, NewCTX} = lists:foldl(
fun({_MediaType, #{<<"schema">> := RawSchema}}, {AnyOfAcc, ExtraSchemasAcc, CTXAcc}) ->
{PerContentTypeSchemas, ExtraSchemas, NewCTX} = lists:foldl(
fun({ContentType, #{<<"schema">> := RawSchema}}, {SchemaAcc, ExtraSchemasAcc, CTXAcc}) ->
{Schema, NewExtraSchemas, SchemaCTX} = parse_schemas(RawSchema, CTXAcc),
{
[Schema | AnyOfAcc],
[{ContentType, Schema} | SchemaAcc],
NewExtraSchemas ++ ExtraSchemasAcc,
CTXAcc#{
resolved => maps:get(resolved, SchemaCTX)
Expand All @@ -422,9 +422,8 @@ parse_request_body(#{<<"content">> := Content} = ReqBody, CTX) ->
{[], [], CTX},
maps:to_list(Content)
),
RequestBodySchema = #{any_of => AnyOf},
RequestBody = #{
schema => RequestBodySchema,
schema => PerContentTypeSchemas,
required => Required
},
{RequestBody, ExtraSchemas, NewCTX};
Expand Down
2 changes: 1 addition & 1 deletion src/erf_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ binarize_atoms(Map) when is_map(Map) ->
maps:fold(
fun
(K, V, Acc) when is_atom(K) ->
maps:put(atom_to_binary(K), RecursiveFun(V), Acc);
maps:put(erlang:atom_to_binary(K), RecursiveFun(V), Acc);
(K, V, Acc) ->
maps:put(K, V, Acc)
end,
Expand Down

0 comments on commit b3a6fbd

Please sign in to comment.