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

[FEATURE] Support the discriminator keyword properly #1589

Open
Stranger6667 opened this issue Aug 9, 2022 · 0 comments
Open

[FEATURE] Support the discriminator keyword properly #1589

Stranger6667 opened this issue Aug 9, 2022 · 0 comments
Assignees
Labels
Status: Needs Triage Requires initial assessment to categorize and prioritize

Comments

@Stranger6667
Copy link
Member

Is your feature request related to a problem? Please describe.
Schemathesis does not use discriminator at all, but it has certain behavior in OAS 3.0. Specifically:

If the discriminator value does not match an implicit or explicit mapping,
no schema can be determined and validation SHOULD fail

and

In this case, a discriminator MAY act as a "hint" to shortcut validation and selection of 
the matching schema which may be a costly operation, depending on the complexity of the schema

If these aspects are applied, then some responses previously considered invalid will be valid. Though, as the latter case uses "MAY", the current behavior of Schemathesis is OK in this regard (the former case, should be implemented anyway)

Describe the solution you'd like
Schemathesis uses discriminator to shortcut to the right schema instead of running the regular JSON Schema validation. It would be hard to do it dynamically, as schemas are recursive and I don't want to hook into the validation process. Instead, I think it could be emulated with if & anyOf:

"anyOf": [
    {
        "if": {
            "properties": { "petType": { "const": "Cat" } }
        },
        "then": {
            "$ref": "#/components/schemas/Cat"
        },
    },
    {
        "if": {
            "properties": { "petType": { "const": "Dog" } }
        },
        "then": {
            "$ref": "#/components/schemas/Dog"
        },
    }
]

Or just with if / then / else, which may produce better error messages. Not sure yet

Additional context
I missed a couple of aspects of the Open API spec in this regard

@Stranger6667 Stranger6667 self-assigned this Aug 9, 2022
@Stranger6667 Stranger6667 changed the title [FEATURE] Support discriminator keyword properly [FEATURE] Support the discriminator keyword properly Aug 10, 2022
@Stranger6667 Stranger6667 added Status: Needs Triage Requires initial assessment to categorize and prioritize and removed Type: Enhancement labels Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Triage Requires initial assessment to categorize and prioritize
Projects
None yet
Development

No branches or pull requests

1 participant