-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Discriminators (not) changing the JSON Schema Validator behavior #2516
Comments
I'm not sure what value that constraint has, but it's not consistent with the processing model of JSON Schema. In 2019-09 and 2020-12, In previous JSON Schema drafts,
I don't think there is a problem here other than being a bit misleading. The "can be used to aid in" language sounds consistent with the "hint" language. It "can" (not "must") be used to "aid" (as is in help achieve the goal, not change goal) in validation. As we discussed last week, in some narrow cases, The problematic part of the spec is https://github.com/OAI/OpenAPI-Specification/blame/master/versions/3.1.0.md#L2725
It can't be both a hint and a "shortcut" because shortcutting changes the validation behavior. Given this contradiction, we decided in the TSC meeting that we wanted to interpret the "hint" language to be correct and the "shortcut" language to be wrong. |
Thanks @jdesrosiers, also for pointing out item three on the hint vs shortcut. Regarding item one on the inline schemas, you mentioned that it made sense in previous releases. Just, I don’t see the value even in the case of earlier OpenAPI releases and ignored the constraint when I was recently implementing discriminators support for networknt/json-schema-validator. I wonder whether the constraints can be safely ignored for now and would be removed (ideally also in a 3.0.4 maintenance release). |
I think the references-only constraint exists for the benefit of code-gen tools. If the schema is inline, tools don't have a class name they can use to create an instance. The reference presumably points to an entry in For validation, the references-only constraint definitely has no value. (@FWiesner, have you seen json-schema-org/json-schema-spec#1082 yet? It's a proposal for a |
@jdesrosiers I had a look at the proposal and still try to digest it in terms of what it would mean in our particular use case. We neither are building a code generator nor a JSON Schema validator. The problem we have to solve (as we build a SaaS solution) is the dynamic extension of existing OpenAPIs without the need to generate new code. Then we interpret requests based on the dynamic extensions and validate payloads. At the moment we still are on OpenAPI 3.0.3 and have not upgraded to OpenAPI 3.1 yet. Today the beauty of discriminators is that in case of As I understand the proposal, Think of the overlay proposal. With today's discriminator you just merge a new type that extends from When With I don't think your proposal (or the alternatives discussed on the thread) won't work, I'm just not yet fully clear of the impacts and whether I like it or not :) Btw it would be really cool if we could have an extension point constraining its extensions in a way. For example if Think of I don't know whether there is a namespacing proposal for OpenAPIs and JSON Schemas today, but this would be my poor-mans approach and I would raise a separate discussion if it makes sense. Also, it would be great if your base type could specify the types it supports on extensions. So, if you know that your |
Thank you for this feedback. While the purpose of Starting with the simplest case, this is effectively {
"$ref": "#/$defs/A",
"propertyDependencies": {
"type": {
"B": { "$ref": "#/$defs/B" }
}
}
} Adding a new subtype {
"$ref": "#/$defs/A",
"propertyDependencies": {
"type": {
"B": { "$ref": "#/$defs/B" },
"C": { "$ref": "#/$defs/C" }
}
}
} If you use references for all the types as we did in the previous examples, the subtype declaration using {
"$ref": "#/$defs/A",
"propertyDependencies": {
"type": {
"B": { "$ref": "#/$defs/B" },
"D": { "$ref": "#/$defs/D" }
}
}
} I think that addresses most of the use cases you brought up although I think it requires you to look at the problem from a different angle. The one thing,
It does that by assuming a location to find the schema and constructing a reference. That doesn't fit well with JSON Schema, for a number of reasons, but I'll point out one.
If I understand you correctly, this can already be done with JSON Schema. Here's an example {
"type": "object",
"properties": {
...
},
"additionalProperties": {
"type": "string",
"maxLength": 65536
}
} Any schema that extends that schema will fail validation if the properties it adds does not meet the constraints in |
Thanks @jdesrosiers for the detailed response. While you've shown ways to construct things in JSON Schema to make it work, I still think OpenAPI
In networknt/json-schema-validator I've implemented the validation in a way that implicit parent-child relationship is established by validating the child, which has the I think I now also have a grasp on why I don't truly like {
"$ref": "#/$defs/A",
"propertyDependencies": {
"type": {
"B": { "$ref": "#/$defs/B" },
"C": { "$ref": "#/$defs/C" }
}
}
} I'm coming from the Java world and the example you gave is a composition case (guess that's the very heart of JSON Schema). Here {
"type": "object",
"properties": {
...
},
"additionalProperties": {
"type": "string",
"maxLength": 65536
}
} That's actually a truly smart idea to enable what I've asked for. Unfortunately I mis-phrased my actual intent. What I was actually was intending to ask for is whether there is a chance to see forward constraints on what schema definitions are legitimate on extensions. Think of an of-the-shelf solution that has an extension base defined and the solution does not have support for ECMA 262 regular expressions evaluation on validation, so wants to disallow this constraint on strings. It might have significantly more technical restrictions, like it might lack support for floating point numbers completely, might only support two levels of nested objects added by extensions, have the given maximum string length you've solved above, etc. |
I think I can see how that could work.
I'm sure it isn't. Because it goes against the grain of how JSON Schema works, it won't easily fit into a standard implementation. I skipped that explanation in my previous comment because it was hard to explain. I'm glad to learn that you have implemented it and understand those challenges even better than I do.
Yes, that example is not idiomatic Java, but it is idiomatic JSON Schema. However, that's not the only way to use it. Perhaps something like this would be familiar to you. {
"$defs": {
"extendsA": {
"propertyDependencies": {
"type": {
"B": { "$ref": "#/$defs/B" },
"C": { "$ref": "#/$defs/C" }
}
}
},
"A": { ... },
"B": {
"$ref": "#/$defs/A",
...
},
"C": {
"$ref": "#/$defs/A",
...
}
}
}
|
I'm pretty sure that anything that could be done here was done in PR #2618, which clarified the interaction with validation. There's too much here for me to tell if there were any other specific asks. Please file them separately if necessary, although keep in mind that we are likely replacing |
As discussed on the TSC on March 25th,
discriminator
is supposed to not affect JSON Schema validation.https://github.com/OAI/OpenAPI-Specification/blame/master/versions/3.1.0.md#L2703
and the following examples puzzle me here, as to me it clearly reads that
properties
/required
in combination with polymorphic use ofallOf
are to be ignored, if they are not on a nested object definition underneathallOf
themselvesfoo
required withotherSchema
carrying thediscriminator
:foo
ignored/not required:This to me is not compatible with the intention to have
discriminator
to only be a hint.https://github.com/OAI/OpenAPI-Specification/blame/master/versions/3.1.0.md#L2701 also specifically lists deserialization, serialization and validation
The text was updated successfully, but these errors were encountered: