Skip to content

Commit

Permalink
Add option --avoid-dangling-refs
Browse files Browse the repository at this point in the history
  • Loading branch information
smondet committed Nov 27, 2024
1 parent 42755ed commit abd931e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 3 additions & 1 deletion bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ let main =
let doc = "Generate an ATD file from a list of JSON Schema / OpenAPI document" in
let state_term =
Term.(
const (fun skip_doc pad toplevel_types ->
const (fun skip_doc pad toplevel_types avoid_dangling_refs ->
Generator.
{
with_doc = not skip_doc;
protect_against_duplicates = (if pad then Some (ref []) else None);
toplevel_types;
avoid_dangling_refs;
}
)
$ Arg.(value (flag (info [ "skip-doc" ] ~doc:"Skip documentation annotations.")))
Expand All @@ -64,6 +65,7 @@ let main =
value (opt_all string [] (info [ "only-matching" ] ~docv:"REGEXP" ~doc:"Only output types matching REGEXP."))
)
)
$ Arg.(value (flag (info [ "avoid-dangling-refs" ] ~doc:"Convert dangling refs to json.")))
)
in
let paths = Arg.(non_empty & pos_all file [] & info [] ~docv:"FILES" ~doc) in
Expand Down
13 changes: 11 additions & 2 deletions lib/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ type state = {
with_doc : bool;
protect_against_duplicates : string list ref option;
toplevel_types : [ `All | `Only of string list ];
avoid_dangling_refs : bool;
}

let default_state = { with_doc = true; protect_against_duplicates = None; toplevel_types = `All }
let default_state =
{ with_doc = true; protect_against_duplicates = None; toplevel_types = `All; avoid_dangling_refs = false }

let record_field_name _state str =
let cleaned_field_name = Utils.sanitize_name str in
Expand Down Expand Up @@ -232,7 +234,14 @@ and make_type_from_schema_or_ref state ~ancestors (schema_or_ref : schema or_ref
match schema_or_ref, ancestors with
| Obj schema, ([] | [ _ ]) -> process_schema_type state ~ancestors schema
| Obj schema, ancestors -> process_nested_schema_type state ~ancestors schema
| Ref ref_, _ -> type_name (get_ref_name ref_)
| Ref ref_, _ -> begin
match
(not state.avoid_dangling_refs)
|| List.exists (fun (name, _schema) -> String.equal (get_ref_name ref_) name) !input_toplevel_schemas
with
| true -> type_name (get_ref_name ref_)
| false -> Printf.sprintf "json (* %s *)" (String.concat "/" (List.rev ancestors))
end

and process_one_of state ~ancestors (schemas_or_refs : schema or_ref list) =
let determine_variant_name = function
Expand Down

0 comments on commit abd931e

Please sign in to comment.