From 7b51811cef78aaa072c72627769ca2cc643fd2f0 Mon Sep 17 00:00:00 2001 From: lostbean Date: Wed, 4 Dec 2024 10:12:16 -0300 Subject: [PATCH] use AnyOf for schema of union types --- src/json/blueprint.gleam | 2 +- test/json_blueprint_test.gleam | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/json/blueprint.gleam b/src/json/blueprint.gleam index 0b8af57..2be64ea 100644 --- a/src/json/blueprint.gleam +++ b/src/json/blueprint.gleam @@ -420,7 +420,7 @@ pub fn union_type_decoder( Some(["type", "data"]), ) }) - |> jsch.OneOf + |> jsch.AnyOf } let defs = list.flat_map(decoders, fn(dec) { { dec.1 }.defs }) diff --git a/test/json_blueprint_test.gleam b/test/json_blueprint_test.gleam index 66ba0ff..e21ffd8 100644 --- a/test/json_blueprint_test.gleam +++ b/test/json_blueprint_test.gleam @@ -342,7 +342,7 @@ pub fn constructor_type_decoder_test() { let schema = blueprint.generate_json_schema(shape_decoder()) let expected_schema_str = - "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"oneOf\":[{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"circle\"]},\"data\":{\"required\":[\"radius\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"radius\":{\"type\":\"number\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"rectangle\"]},\"data\":{\"required\":[\"width\",\"height\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"width\":{\"type\":\"number\"},\"height\":{\"type\":\"number\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"void\"]},\"data\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{}}}}]}" + "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"anyOf\":[{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"circle\"]},\"data\":{\"required\":[\"radius\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"radius\":{\"type\":\"number\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"rectangle\"]},\"data\":{\"required\":[\"width\",\"height\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"width\":{\"type\":\"number\"},\"height\":{\"type\":\"number\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"void\"]},\"data\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{}}}}]}" schema |> json.to_string @@ -814,7 +814,7 @@ pub fn tree_decoder_test() { schema |> json.to_string |> should.equal( - "{\"$defs\":{\"ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\":{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"node\"]},\"data\":{\"required\":[\"value\",\"right\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"value\":{\"type\":\"integer\"},\"left\":{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},\"right\":{\"anyOf\":[{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},{\"type\":\"null\"}]}}}}}},\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"oneOf\":[{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"list\"]},\"data\":{\"required\":[\"head\",\"tail\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"head\":{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},\"tail\":{\"$ref\":\"#\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"no_trees\"]},\"data\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{}}}}]}", + "{\"$defs\":{\"ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\":{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"node\"]},\"data\":{\"required\":[\"value\",\"right\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"value\":{\"type\":\"integer\"},\"left\":{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},\"right\":{\"anyOf\":[{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},{\"type\":\"null\"}]}}}}}},\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"anyOf\":[{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"list\"]},\"data\":{\"required\":[\"head\",\"tail\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"head\":{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},\"tail\":{\"$ref\":\"#\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"no_trees\"]},\"data\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{}}}}]}", ) }