Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSon schema and Inheritance

Tags:

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

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.

like image 912
user3748879 Avatar asked Dec 10 '14 20:12

user3748879


People also ask

Does JSON Schema support inheritance?

Because there is no such thing as schema inheritance currently defined. 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.

What is JSON Schema?

JSON Schema is an IETF standard providing a format for what JSON data is required for a given application and how to interact with it. Applying such standards for a JSON document lets you enforce consistency and data validity across similar JSON data. .

What does allOf mean in JSON Schema?

By the definition of this keyword, it meant that the instance had to be valid against the current schema and all schemas specified in extends ; basically, draft v4's allOf is draft v3's extends .

What is JSON Schema properties?

Properties. The properties (key-value pairs) on an object are defined using the properties keyword. The value of properties is an object, where each key is the name of a property and each value is a schema used to validate that property.


1 Answers

OK, well, I am the author of both:

  • the current JSON Schema validation spec;
  • and the Java library which is the most used for JSON Schema validation in Java today: 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.

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.

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.

In short:

  • 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.
like image 189
fge Avatar answered Sep 18 '22 15:09

fge