Imitating Named Supertype/Union Class #166
-
|
I'm working off of a JSON Schema that I and one of my coworkers are using for interprocess communication between programs in two different languages (Java and C++). We're autogenerating code using different tools (as my coworker's tool is Java-specific), and I recently realized that they were using the {
"distribution": {
"title": "Distribution",
"description": "A Probability Distribution",
"type": "object"
},
"normalDistribution": {
"title": "NormalDistribution",
"description": "A Normal Probability Distribution",
"type": "object",
"extends": {
"$ref": "#/distribution"
},
"additionalProperties": false,
"required": [
"mean",
"standardDeviation"
],
"properties": {
"mean": {
"description": "The mean (middle) of the distribution",
"type": "number"
},
"standardDeviation": {
"description": "The standard deviation (spread) of the distribution",
"type": "number"
}
}
},
"triangularDistribution": {
"title": "TriangularDistribution",
"description": "A Triangular Probability Distribution",
"type": "object",
"extends": {
"$ref": "#/distribution"
},
"additionalProperties": false,
"required": [
"maximum",
"minimum",
"peak",
"ratio"
],
"properties": {
"maximum": {
"description": "The maximum of the distribution",
"type": "number"
},
"minimum": {
"description": "The minimum of the distribution",
"type": "number"
},
"peak": {
"description": "The peak of the distribution",
"type": "number"
},
"ratio": {
"description": "The ratio of the distribution",
"type": "number"
}
}
},
"uniformDistribution": {
"title": "UniformDistribution",
"description": "A Uniform Probability Distribution",
"type": "object",
"extends": {
"$ref": "#/distribution"
},
"additionalProperties": false,
"required": [
"maximum",
"minimum"
],
"properties": {
"maximum": {
"description": "The maximum of the distribution",
"type": "number"
},
"minimum": {
"description": "The minimum of the distribution",
"type": "number"
}
}
}
}The issue is that my tool (quicktype) does not implement the {
"distribution": {
"title": "Distribution",
"description": "A Probability Distribution",
"type": "object",
"oneOf": [
{
"$ref": "#/normalDistribution"
},
{
"$ref": "#/triangularDistribution"
},
{
"$ref": "#/uniformDistribution"
}
],
"additionalProperties": false
},
"normalDistribution": {
"title": "NormalDistribution",
"description": "A Normal Probability Distribution",
"type": "object",
"additionalProperties": false,
"required": [
"mean",
"standardDeviation"
],
"properties": {
"mean": {
"description": "The mean (middle) of the distribution",
"type": "number"
},
"standardDeviation": {
"description": "The standard deviation (spread) of the distribution",
"type": "number"
}
}
},
"triangularDistribution": {
"title": "TriangularDistribution",
"description": "A Triangular Probability Distribution",
"type": "object",
"additionalProperties": false,
"required": [
"maximum",
"minimum",
"peak",
"ratio"
],
"properties": {
"maximum": {
"description": "The maximum of the distribution",
"type": "number"
},
"minimum": {
"description": "The minimum of the distribution",
"type": "number"
},
"peak": {
"description": "The peak of the distribution",
"type": "number"
},
"ratio": {
"description": "The ratio of the distribution",
"type": "number"
}
}
},
"uniformDistribution": {
"title": "UniformDistribution",
"description": "A Uniform Probability Distribution",
"type": "object",
"additionalProperties": false,
"required": [
"maximum",
"minimum"
],
"properties": {
"maximum": {
"description": "The maximum of the distribution",
"type": "number"
},
"minimum": {
"description": "The minimum of the distribution",
"type": "number"
}
}
}
}The code generated in C++ for this isn't particularly satisfactory, but I don't know whether this is an issue with my schema or something I could request from or contribute to the tool I'm using. Is there a better way to get what I want here? Notably, this question differs from all the JSON schema "inheritance" questions I could find online because I don't want a supertype with properties. I essentially just want a named union subschema. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I realize this isn't really an answer, but you may be interested in https://github.com/json-schema-org/vocab-idl, which is exploring cases exactly like this in an effort to build a vocabulary so that it can be officially supported. |
Beta Was this translation helpful? Give feedback.
-
|
I think one of the reasons that Using draft-03's However, with JSON Schema processing generally works in a parent-to-child fashion, with "child" encompassing **except actually it won't work at all, because |
Beta Was this translation helpful? Give feedback.
I think one of the reasons that
extendswas dropped gets back to the nature of JSON Schema as a constraint system for validation rather than a data definition system.Using draft-03's
extendsthe way you show here did not mean that the extended schema (#/definitions/distribution) would function like the version that usesoneOf. With thatoneOfapproach#/definitions/distributionwill validate instances that look like any of those three other schemas.**However, with
extends,#/definitions/distributionwill validate anything and everything, because it has no local constraints, and it is unware of theextendsin the other schema objects. Of course, people could implement tools that behaved …