Skip to content

Commit 61c6bf1

Browse files
committed
Generative use case guidance
This is going to be a powerful technique for generative use cases, which have been observed either requiring references (as opposed to subschemas) in certain places, or implementing various sorts of inferencing around references on their own or in conjunction with other keywords. For example, one OpenAPI documentation tool assumes that in an "allOf" of several references, the first is a base class of some sort. While this is not correct behavior by either JSON Schema or OpenAPI, it demonstrates a feature built by making implicit assumptions about certain references. This section provides a recommendation for making such behavior explicit, and also guides writers of such tools to the concept of extension vocabularies and annotations.
1 parent 332abe6 commit 61c6bf1

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

jsonschema-core.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,6 +3610,68 @@ User-Agent: product-name/5.4.1 so-cool-json-schema/1.0.2 curl/7.43.0
36103610
</section>
36113611
</section>
36123612

3613+
<section title="References and generative use cases">
3614+
<t>
3615+
While the presence of references is expected to be transparent
3616+
to validation results, generative use cases such as code generators
3617+
and UI renderers often consider references to be semantically significant.
3618+
</t>
3619+
<t>
3620+
To make such use case-specific semantics explicit, the best practice
3621+
is to create an annotation keyword can be created for use in the same
3622+
schema object alongside of a reference keyword such as "$ref".
3623+
</t>
3624+
<figure>
3625+
<preamble>
3626+
For example, here is a hypothetical keyword for determining
3627+
whether a code generator should consider the reference
3628+
target to be a distinct class, and how those classes are related.
3629+
Note that this example is solely for illustrative purposes, and is
3630+
not intended to propose a functional code generation keyword.
3631+
</preamble>
3632+
<artwork>
3633+
<![CDATA[
3634+
{
3635+
"allOf": [
3636+
{
3637+
"classRelation": "is-a",
3638+
"$ref": "classes/base.json"
3639+
},
3640+
{
3641+
"$ref": "fields/common.json"
3642+
}
3643+
],
3644+
"properties": {
3645+
"foo": {
3646+
"classRelation": "has-a",
3647+
"$ref": "classes/foo.json"
3648+
},
3649+
"date": {
3650+
"$ref": "types/dateStruct.json",
3651+
}
3652+
}
3653+
}
3654+
]]>
3655+
</artwork>
3656+
</figure>
3657+
<t>
3658+
Here, this schema represents some sort of object-oriented class.
3659+
The first reference in the "allOf" is noted as the base class.
3660+
The second is not assigned a class relationship, meaning that the
3661+
code generator should combine the target's definition with this
3662+
one as if no reference were involved.
3663+
</t>
3664+
<t>
3665+
Looking at the properties, "foo" is flagged as object composition,
3666+
while the "date" property is not. It is simply a field with
3667+
sub-fields, rather than an instance of a distinct class.
3668+
</t>
3669+
<t>
3670+
This style of usage requires the annotation to be in the same object
3671+
as the reference, which must be recognizable as a reference.
3672+
</t>
3673+
</section>
3674+
36133675
<section title="Acknowledgments">
36143676
<t>
36153677
Thanks to

0 commit comments

Comments
 (0)