Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] schema 的 allof 无效 #3103

Open
DoctorReid opened this issue Oct 16, 2024 · 0 comments
Open

[BUG] schema 的 allof 无效 #3103

DoctorReid opened this issue Oct 16, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@DoctorReid
Copy link

DoctorReid commented Oct 16, 2024

问题描述

schema 的 allOf 无效

环境信息

请填写以下信息:

  • 版本信息:Fastjson2 2.0.53

重现步骤

根据schema教程

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

    @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
@DoctorReid DoctorReid added the bug Something isn't working label Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant