Skip to content

Commit

Permalink
fix : JSONSchema array item reference defs
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Feb 25, 2023
1 parent 6c40d05 commit 73f81c0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public ArraySchema(JSONObject input, JSONSchema root) {
additionalItemsSupport = true;
} else {
additionalItemsSupport = true;
this.itemSchema = JSONSchema.of((JSONObject) items, root);
this.itemSchema = JSONSchema.of((JSONObject) items, root != null ? root : this);
}

if (additionalItems instanceof JSONObject) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.fastjson2.schema;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.junit.jupiter.api.Test;

import java.net.URL;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JSONSchemaTest1 {
@Test
public void test0() {
URL url = JSONSchemaTest.class.getClassLoader().getResource("schema/schema_02.json");
JSONObject object = JSON.parseObject(url, JSONObject.class);
ArraySchema schema = (ArraySchema) JSONSchema.of(object);
assertFalse(schema.isValid("aa"));
assertFalse(schema.isValid(-1));
assertFalse(schema.isValid(-1L));

assertTrue(schema.isValid(JSONArray.of()));
assertTrue(schema.isValid(JSONArray.of(1)));

assertFalse(schema.isValid(JSONArray.of("a")));
assertFalse(schema.isValid(JSONArray.of(-1)));
assertFalse(schema.isValid(JSONArray.of(-1L)));
}
}
10 changes: 10 additions & 0 deletions core/src/test/resources/schema/schema_02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "array",
"items": { "$ref": "#/$defs/positiveInteger" },
"$defs": {
"positiveInteger": {
"type": "integer",
"exclusiveMinimum": 0
}
}
}

0 comments on commit 73f81c0

Please sign in to comment.