-
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
Better support for polymorphism(replacement for discriminator) #707
Comments
I like the basic idea, but wonder how to express the multiple levels of inheritance: how would I specialize dogs into hunting dogs and shepherd dogs? Would I have to "flatten" the inheritance hierarchy by adding members valueSwitch:
# Name of the property which values are used to determinate apropriate subschema
property: petType
# Map of property values on Schema Objects
values:
Cat:
$ref: '#/definitions/CatMixin'
Dog:
$ref: '#/definitions/DogMixin'
HuntingDog:
- $ref: '#/definitions/DogMixin'
- $ref: '#/definitions/HuntingDogMixin'
ShepherdDog:
- $ref: '#/definitions/DogMixin'
- $ref: '#/definitions/ShepherdDogMixin' |
This feels strange. Strong-typed languages will most likely have a problem with this sort of specification, given that on-the-fly framework conversions will fail. For example what should the request object type actually be? (Or what is the response object type?) Although, it would be nice to be able to specify different validations--i.e. |
@ralfhandl You just need to use valueSwitch:
# Name of the property which values are used to determinate apropriate subschema
property: petType
# Map of property values on Schema Objects
values:
Cat:
$ref: '#/definitions/CatMixin'
Dog:
$ref: '#/definitions/DogMixin'
HuntingDog:
allOf:
- $ref: '#/definitions/DogMixin'
- $ref: '#/definitions/HuntingDogMixin'
ShepherdDog:
allOf:
- $ref: '#/definitions/DogMixin'
- $ref: '#/definitions/ShepherdDogMixin'
@wparad It possible to implement in all typed-languages I know of, most of them natively support polymorphism and the rest support "union"-like types. You can even implement it in pure C, using
In really most of the time developers used only two patterns for dynamic data in JSON:
To handle exotic cases you need to add something very similar to |
I'd like to suggest another approach to handling the general idea here. How about we get some examples of different modeling challenges and then put together a comprehensive solution from them? Maybe it's been done but It would be great to start linking to them here. |
I believe this has already been done quite thoroughly, resulting in the widely accepted json-schema draft 4. |
Tackling PR: #741 |
There have been several developments related to this in more recent drafts of JSON Schema. draft-07's The vocabulary definition and management proposals for draft-08 are primarily intended to allow adding a set of code generation keywords to augment the validation keywords. The two use cases are quite different, which has resulted in very awkward attempts to cram data definition into the validation constraint system. See json-schema-org/json-schema-spec#561 if you want to upvote the vocabulary support proposal. Note that here in the OAS repo, #1443 is tracking whether/how to support newer drafts. |
Many people including myself asked for full support of JSON Schema.
But I totally understand why
oneOf
&anyOf
are causing a lot of problems:It was never intended to use for anything beyond validation.
discriminator
was good attempt to solve most of the common use cases.We tried it with our customers and even have added support for it in our documentation engine.
But we have discovered a few issues that limit usability of
discriminator
:discriminator
field should match one of the keys indefinitions
object.If you use multiple
discriminator
inside multiple payloads you will experience name clashes.It also breaks namestyle, since a lot of people name schema in camelcase and field values using underscores.
And with added clarification on namespaces, characters in definitions name #634 merged(schema names limited to
a-zA-Z0-9.-_\
) it creates a big problem for non-English field values.discriminator
.You need to go through all schemas and inspect
allOf
s and it becomes especially problematic when inherited schemas exist in separate files.discriminator
. A solution would be to provide translator ofdiscriminator
intooneOf
and use standard JSON Schema validator afterwards.But translation is hard to implement because of the third point.
My proposal is to replace
discriminator
with a newvalueSwitch
keyword.Here is modifiend discriminator example:
In the example I also added support for user-defined petType to illustrate usage of
default
keyword.And here is how it solves problems mentioned above:
values
object.default
, see above example.valueSwitch
into pure JSON Schema Draft4 using straightforward algorithm, for example:Moreover it is possible to convert
discriminator
intovalueSwitch
for all existing Swagger specs.The text was updated successfully, but these errors were encountered: