Skip to content

Commit

Permalink
TMP jsonschema: support external references
Browse files Browse the repository at this point in the history
  • Loading branch information
Khady committed Feb 27, 2024
1 parent 12356db commit 2a4d1ca
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
27 changes: 18 additions & 9 deletions lib/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ let process_int_type schema =
| _ -> failwith "int has unextected format"

let get_ref_name ref =
match String.split_on_char '/' ref with
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
(* OpenAPI defs *)
| [ "#"; "components"; "schemas"; type_name ] -> type_name
| [ ""; "components"; "schemas"; type_name ] -> type_name
(* JSON Schema defs *)
| [ "#"; ("$defs" | "definitions"); type_name ] -> type_name
| _ ->
failwith
(Printf.sprintf
"Unsupported ref value: %s. Supported ref URI are: #/components/schemas/* and #/$defs/* and #/definitions/*"
ref
)
| [ ""; ("$defs" | "definitions"); type_name ] -> type_name
| _ -> name_of_path pointer

let output = Buffer.create 16
let input_toplevel_schemas = ref []
Expand Down
42 changes: 42 additions & 0 deletions tests/mocks/jsonschema_refs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$id": "https://example.com/schemas/customer",
"type": "object",
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"shipping_address": {
"$ref": "/schemas/address"
},
"billing_address": {
"$ref": "/schemas/address"
},
"aa": {
"$ref": "https://example.com/schemas/address"
},
"bb": {
"$ref": "http://example.com/schemas/address"
},
"cc": {
"$ref": "http://example.com/schemas#/definitions/address"
},
"dd": {
"$ref": "http://example.com/schemas#/definitions/address"
},
"ee": {
"$ref": "#/definitions/schemas/address"
},
"ff": {
"$ref": "#/definitions/address"
}
},
"required": [
"first_name",
"last_name",
"shipping_address",
"billing_address"
]
}
19 changes: 19 additions & 0 deletions tests/smoke.t
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,22 @@ Generate ATD out of JSON Schema that contains definitions (legacy support)
first_name : name;
last_name : name;
}

Generate ATD out of JSON Schema that uses references
$ jsonschema2atd --format=jsonschema ./mocks/jsonschema_refs.json
(* Generated by jsonschema2atd *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

type root = {
first_name : string;
last_name : string;
shipping_address : address;
billing_address : address;
?aa : address option;
?bb : address option;
?cc : address option;
?dd : address option;
?ee : address option;
?ff : address option;
}

0 comments on commit 2a4d1ca

Please sign in to comment.