From 3ed22a91892bf4fb260c8bf2a47f742e5652630f Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 15 Sep 2024 17:36:52 +0800 Subject: [PATCH] jsonschema support array type, for issue #2931 --- .../com/alibaba/fastjson2/JSONObject.java | 5 ++++ .../com/alibaba/fastjson2/schema/AnyOf.java | 4 ++++ .../alibaba/fastjson2/schema/JSONSchema.java | 9 +++++++ .../fastjson2/issues_2900/Issue2931.java | 24 +++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2931.java diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONObject.java b/core/src/main/java/com/alibaba/fastjson2/JSONObject.java index eff470fffe..76d66f148f 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONObject.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONObject.java @@ -2157,4 +2157,9 @@ public static JSONObject from(Object obj) { public static JSONObject from(Object obj, JSONWriter.Feature... writeFeatures) { return (JSONObject) JSON.toJSON(obj, writeFeatures); } + + public boolean isArray(Object key) { + Object object = super.get(key); + return object instanceof JSONArray || object != null && object.getClass().isArray(); + } } diff --git a/core/src/main/java/com/alibaba/fastjson2/schema/AnyOf.java b/core/src/main/java/com/alibaba/fastjson2/schema/AnyOf.java index ec570114ba..e6b3aef793 100644 --- a/core/src/main/java/com/alibaba/fastjson2/schema/AnyOf.java +++ b/core/src/main/java/com/alibaba/fastjson2/schema/AnyOf.java @@ -46,4 +46,8 @@ public ValidateResult validate(Object value) { } return FAIL_ANY_OF; } + + public JSONObject toJSONObject() { + return JSONObject.of("anyOf", this.items); + } } diff --git a/core/src/main/java/com/alibaba/fastjson2/schema/JSONSchema.java b/core/src/main/java/com/alibaba/fastjson2/schema/JSONSchema.java index feb0220580..77204dd3da 100644 --- a/core/src/main/java/com/alibaba/fastjson2/schema/JSONSchema.java +++ b/core/src/main/java/com/alibaba/fastjson2/schema/JSONSchema.java @@ -373,6 +373,15 @@ static JSONSchema of(java.lang.reflect.Type type, JSONSchema root) { @JSONCreator public static JSONSchema of(JSONObject input, JSONSchema parent) { + if (input.size() == 1 && input.isArray("type")) { + JSONArray types = input.getJSONArray("type"); + JSONSchema[] items = new JSONSchema[types.size()]; + for (int i = 0; i < types.size(); i++) { + items[i] = JSONSchema.of(JSONObject.of("type", types.get(i))); + } + return new AnyOf(items); + } + Type type = Type.of( input.getString("type") ); diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2931.java b/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2931.java new file mode 100644 index 0000000000..c2ba8ecf0a --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2931.java @@ -0,0 +1,24 @@ +package com.alibaba.fastjson2.issues_2900; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import com.alibaba.fastjson2.schema.JSONSchema; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class Issue2931 { + @Test + public void test() { + JSONObject object = JSON.parseObject("{\"type\":\"object\",\"properties\":{\"longitude\":{\"type\":[\"string\", \"number\"]},\"latitude\":{\"type\":\"number\",\"minimum\":-90,\"maximum\":90}},\"required\":[\"longitude\",\"latitude\"]}"); + System.out.println(object); + JSONSchema schema = JSONSchema.of(object); + assertTrue(schema.isValid( + JSONObject.of("longitude", "123.45", "latitude", 21.31) + )); + assertFalse(schema.isValid( + JSONObject.of("longitude", false, "latitude", 21.31) + )); + } +}