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

dojo quick changes #5

Merged
merged 6 commits into from
Feb 28, 2024
Merged

dojo quick changes #5

merged 6 commits into from
Feb 28, 2024

Conversation

Khady
Copy link
Contributor

@Khady Khady commented Feb 27, 2024

This PR is based on the work done during the recent dojo. It should probably have been divided into different pull requests, as commits are mostly independent from each other. Most of the changes are relatively small. 2 of them could be controversial though:

  • support for external references (URI with or without a http prefix)
  • generation of one atd file from a batch of schemas

Those two features kind of go hand in hand and are going in the same direction.

I was trying jsonschema2atd on schemas that have http uri references to other types. In the past I had to update all of those by hands to turn them into #/$defs/typename. Now those references don't trigger an error. But it means that we can generate a atd file that wouldn't compile without manual fixes.

The generation from a batch of schemas helps with that, as I now can work with 3 schemas that related to each other. Schema A has http references to schemas B and C. So I can download those 3 files and then run jsonschema2atd --format=jsonschema c.json b.json a.json. There are cases which won't work, for example if 2 of the schemas have types named the same. But I'm not sure how to fix that without creating too much noise.

lib/generator.ml Outdated Show resolved Hide resolved
Copy link
Collaborator

@ixzzd ixzzd left a comment

Choose a reason for hiding this comment

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

Looks great, thank you 👍

@Khady Khady changed the title Louis/misc dojo quick changes Feb 28, 2024
@Khady Khady marked this pull request as ready for review February 28, 2024 09:33
| `String s -> s
| value ->
failwith
(sprintf "Invalid value %s in string enum %s" (Yojson.Basic.to_string value)
Copy link
Collaborator

@ixzzd ixzzd Feb 28, 2024

Choose a reason for hiding this comment

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

👍 this check could be useful

let root =
match root with
| `Default -> None
| `Per_file -> Some (path |> Filename.basename |> Filename.remove_extension |> Utils.sanitize_name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

minor: wondering if it does make sense to use filename for root time always (even if only one input scheme file passed)
I used jsonscheme2atd for openapi files only, so I didn't have issues with root type name. @Khady if you think it is handy, feel free to change the default root type to filename

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also it would simplify code (we can get rid of Default and Per_file variants)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went back and forth on this one. My problem with file names is that it can become very noisy when they are long. No strong opinion at this time

Copy link
Collaborator

@ixzzd ixzzd left a comment

Choose a reason for hiding this comment

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

Thank you @Khady 👏 Feel free to merge the PR
Having LSP support (through odoc annotations) is super useful for complex schemas (like grafana types) 🎉

There are cases which won't work, for example if 2 of the schemas have types named the same. But I'm not sure how to fix that without creating too much noise.

I guess the relatively easy way would be to use global input_toplevel_schemas to resolve typename collisions (convert it to map probably? with input filenames as a key). Please open an issue if you face the this problem in real world usage

@Khady Khady merged commit cfe98f8 into master Feb 28, 2024
16 checks passed
@Khady Khady deleted the louis/misc branch February 28, 2024 11:22
@Khady
Copy link
Contributor Author

Khady commented Feb 28, 2024

I guess the relatively easy way would be to use global input_toplevel_schemas to resolve typename collisions (convert it to map probably? with input filenames as a key). Please open an issue if you face the this problem in real world usage

the problem with this solution is that one needs to create some kind of connection between the http uri references in the schemas and the actual json files that are passed to the cli. I was scared that doing it explicitly through the cli would make the invocations too noisy. We could build something automatic based on the id/$id of a schema probably.

Comment on lines +26 to +40
let uri, pointer =
match String.split_on_char '#' ref with
| [ uri; pointer ] -> uri, Some pointer
| [ uri ] -> uri, None
| _ -> failwith (sprintf "Unsupported remote ref value: %s. The URI contains multiple '#'." ref)
in
let name_of_path path =
match path |> String.split_on_char '/' |> List.rev |> List.hd with
| exception _ -> failwith (sprintf "Unsupported ref value: %s" ref)
| name -> name
in
match pointer with
| None -> name_of_path uri
| Some pointer ->
match String.split_on_char '/' pointer with
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm guessing this is not completely correct. If the pointer is to a subschema then the name shouldn't be just the last part of the pointer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants