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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle more parsing cases (int and str formats, additional props: fix #10, #11 and #13) #12

Merged
merged 17 commits into from
Feb 17, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add option --avoid-dangling-refs
smondet committed Nov 27, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit abd931ee0b62551e8d825c910369ac6142ab63fe
4 changes: 3 additions & 1 deletion bin/main.ml
Original file line number Diff line number Diff line change
@@ -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.")))
@@ -64,6 +65,7 @@ let main =
value (opt_all string [] (info [ "only-matching" ] ~docv:"REGEXP" ~doc:"Only output types matching REGEXP."))
)
Copy link
Collaborator

@ixzzd ixzzd Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thinking this is a nice addition, thank you @smondet
Could you please add also tests for this one and mention it in the doc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know whether I added enough tests :)

)
$ 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
13 changes: 11 additions & 2 deletions lib/generator.ml
Original file line number Diff line number Diff line change
@@ -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
@@ -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