Skip to content

Automatically expose t_jsonschema in .mli files #13

Description

@GirardR1006

Currently, the ppx only exposes t_jsonschema function in the ml files. When adding ppx_deriving jsonschema in the .mli file, I get the following:

6 | [@@deriving yojson, jsonschema]
                ^^^^^^^^^^^^^^^^^^
Error: Ppxlib.Deriving: 'jsonschema' is not a supported signature type
       deriving generator

MWE to reproduce:

(*custom_types.ml*)

type record = {
  a : int;
  b : string;
  c : bool;
}
[@@deriving yojson, jsonschema]
(*custom_types.mli*)
type record = {
  a : int;
  b : string;
  c : bool;
}
[@@deriving yojson]

(* Commenting the below type definition results in failure*)
val record_jsonschema :
  [> `Assoc of
     (string
     * [> `Assoc of
          (string * [> `Assoc of (string * [> `String of string ]) list ]) list
       | `Bool of bool
       | `List of [> `String of string ] list
       | `String of string
       ])
     list
  ]
(* main.ml *)

open Custom_types

let write_schema f path =
  let schema = Ppx_deriving_jsonschema_runtime.json_schema f in
  let out_channel = Stdlib.open_out path in
  let schema_str = Yojson.Basic.pretty_to_string schema in
  Stdio.Out_channel.output_string out_channel schema_str;
  Stdlib.Printf.printf "Printing JSON schema to file %s\n" path;
  Stdlib.close_out out_channel

let () =
  write_schema record_jsonschema "record.schema.json"

The LSP provides the type signature, so it is just a matter of copying it back to the .mli, but it may be useful to do it by default.

The use case would be to generate Json schemas of types defined in different files that depends of each others (ex: type a in a.ml and type b = {a_r:a} in b.ml).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions