JSon 架构和继承 [英] JSon schema and Inheritance

查看:28
本文介绍了JSon 架构和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了带有继承的 java 绑定的 json 模式,所有搜索都让我使用了allOf".

I have searched on json schema with java bindings with inheritance and all searches led me to the usage of "allOf".

使用 allOf 可能会解决我的问题,但我想知道是否可以使用 json 模式中的构造来生成具有真正的 Java 继承B 扩展 A"的 Java 代码 - 而不是从 A 内联所有属性B里面?

Using allOf would potentially solve my problem, but I am wondering if there is a construct in json schema that I can use which will generate my java code with real java inheritance "B extends A" - rather than inlining all properties from A inside B ?

我想知道这是否得到支持/可行,或者我只是在做梦.如果不支持,我很想知道原因.

I am wondering if this is even supported / doable or I am just dreaming. If not supported, I would be curious to know the reason.

推荐答案

好吧,我是两者的作者:

OK, well, I am the author of both:

  • 当前的 JSON Schema 验证规范;
  • 以及当今 Java 中最常用于 JSON Schema 验证的 Java 库:json-schema-validator.

所以我可以回答你的问题,基本答案是否定的.

So I can answer your question, and the basic answer is no.

为什么?因为目前没有定义模式继承这样的东西.

Why? Because there is no such thing as schema inheritance currently defined.

当使用allOf时,你要求allOf中的所有模式都匹配;如果您对这个或那个 JSON 中可以存在的内容很严格,那么您将把 additionalProperties 添加到 false.因此,您不能继承.

When using allOf, you require that all schemas in allOf match; and if you are strict about what can exist in this or that JSON, you'll have added additionalProperties to false. As such, you cannot inherit.

真正的解决方案是我为 v5 草案提出的一种机制:$merge$patch 关键字.这些将允许使用 RFC 7386 或 RFC 6902 修补模式(有关更多信息,请参阅此处)并确实实现模式继承.

The real solution is a mechanism I proposed for draft v5: the $merge and $patch keywords. These would allow to patch schemas with either of RFC 7386 or RFC 6902 (see here for more info) and indeed implement schema inheritance.

简而言之:

  • 如果您将 additionalProperties 设置为 false,并且您的基本 JSON 是一个对象,您将无法定义其他对象成员;
  • 使用这两个新关键字,您可以.
  • if you set additionalProperties to false, and your basic JSON is an object, you won't be able to define additional object members;
  • with these two new keywords, you can.

这篇关于JSon 架构和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆