alibaba / alibaba/fastjson2

[BUG] schema 的 allof 无效

Open
#3,103 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
schema 的 allOf 无效

### 环境信息
*请填写以下信息:*

- 版本信息:Fastjson2 2.0.53

### 重现步骤
根据schema教程

https://tour.json-schema.org/content/06-Combining-Subschemas/03-Valid-Against-allOf-the-Subschemas(AND)

```java
@Test
public void testSp() {
String objStr = "{\"name\":\"John Doe\",\"age\":41.5}";
String schemaStr = "{\"$defs\":{\"ageLimit\":{\"minimum\":18,\"maximum\":60},\"ageType\":{\"type\":\"integer\"}},\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"},\"age\":{\"allOf\":[{\"$ref\":\"#/$defs/ageLimit\"},{\"$ref\":\"#/$defs/ageType\"}]}},\"required\":[\"name\"]}";

JSONObject schemaJson = JSON.parseObject(schemaStr);
JSONSchema schema = JSONSchema.of(schemaJson);
// 验证JSON数据
ValidateResult result = schema.validate(JSONObject.parseObject(objStr));
Assertions.assertFalse(result.isSuccess());
}
```
### 期待的正确结果
可以兼容 json-schema tour 里的所有场景

#### 附加信息

经过 debug,发现问题

1. com.alibaba.fastjson2.schema.AllOf#AllOf(com.alibaba.fastjson2.JSONObject, com.alibaba.fastjson2.schema.JSONSchema) 中,仅有 type==null 的时候,构造 JSONSchema 会传入 parent,其余情况均不会传入,导致递归构造时无法取到具体的 sub schema
2. 部分类型的 schema 并没有支持 allOf,例如 NumberSchema
3. 部分类型的 schema 支持 allOf,但没有传入 parent,例如 ObjectSchema

Contributor guide

Open the contributing guide

Research direction

Start with the provided testSp reproduction and read com.alibaba.fastjson2.schema.AllOf, NumberSchema, and ObjectSchema. Trace how parent is passed while constructing referenced subschemas, then run the schema validation tests. Done means allOf resolves the referenced definitions and rejects the sample age value that violates the integer constraint, with regression coverage for the reported cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.