Skip to content
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

Validating Polymorphic model types #169

Closed
amarzavery opened this issue Feb 12, 2018 · 1 comment
Closed

Validating Polymorphic model types #169

amarzavery opened this issue Feb 12, 2018 · 1 comment

Comments

@amarzavery
Copy link

Problem

Polymorphic models are not being validated correctly. For example Cat and Dog are having an allOf relationship with Animal which has a discriminator property named dtype, then at runtime the request body or the response body could be an Animal or a Cat or a Dog. If the model specified was a Cat then sway would not fail if some required properties of Cat were not provided.

A possible solution

I added"additionalProperties": false on all the models that do not have any additionalProperties set. This makes their json schema stricter. Now if any unknown property is given the validator will fail. On top of that I have changed all the references to polymorphic models to be a oneOf the parent model or it's children. That is wherever Animal is being referenced "$ref": "#/definitions/Animal", I updated that reference to be a oneOf reference

"oneOf": [ 
    {"$ref": "#/definitions/Animal"}, 
    {"$ref": "#/definitions/Cat"}, 
    {"$ref": "#/definitions/Dog"}
]

Thus when the modified swagger is given to z-schema validator (package on which sway depends for model validation), it does a crisp job of validating polymorphic model types.

  • It would be nice to have a config that in sway that customers can set to make model validation stricter.
@whitlockjc
Copy link
Member

Duplicate of #28.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants