Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions openapi_schema_validator/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,10 @@ def _build_oas31_validator() -> Any:
Draft202012Validator,
{
# adjusted to OAS
"allOf": oas_keywords.allOf,
"oneOf": oas_keywords.oneOf,
"anyOf": oas_keywords.anyOf,
"pattern": oas_keywords.pattern,
"description": oas_keywords.not_implemented,
# fixed OAS fields
# discriminator is annotation-only in OAS 3.1+
"discriminator": oas_keywords.not_implemented,
"xml": oas_keywords.not_implemented,
"externalDocs": oas_keywords.not_implemented,
Expand Down
73 changes: 73 additions & 0 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,79 @@ def test_array_prefixitems_invalid(self, validator_class, value):
]
assert any(error in str(excinfo.value) for error in errors)

def test_discriminator_is_annotation_only(self, validator_class):
schema = {
"components": {
"schemas": {
"A": {
"type": "object",
"properties": {"kind": {"type": "string"}},
"required": ["kind"],
},
"B": {
"type": "object",
"properties": {
"kind": {"type": "string"},
"other": {"type": "string"},
},
"required": ["kind"],
},
}
},
"oneOf": [
{"$ref": "#/components/schemas/A"},
{"$ref": "#/components/schemas/B"},
],
"discriminator": {"propertyName": "kind"},
}

validator = validator_class(schema)

# A payload valid for both schemas A and B
instance = {"kind": "B"}

# oneOf fails because it matches BOTH A and B, discriminator does not restrict it
with pytest.raises(ValidationError):
validator.validate(instance)

@pytest.mark.parametrize(
"mapping_ref",
[
"#/components/schemas/Missing",
"#missing-anchor",
"#bad/frag",
],
)
def test_discriminator_unresolvable_reference_ignored(
self, validator_class, mapping_ref
):
schema = {
"oneOf": [{"$ref": "#/components/schemas/MountainHiking"}],
"discriminator": {
"propertyName": "discipline",
"mapping": {"mountain_hiking": mapping_ref},
},
"components": {
"schemas": {
"MountainHiking": {
"type": "object",
"properties": {
"discipline": {"type": "string"},
"length": {"type": "integer"},
},
"required": ["discipline", "length"],
},
},
},
}

validator = validator_class(schema)

# It should not raise any referencing errors because discriminator mapping is annotation-only
validator.validate(
{"discipline": "mountain_hiking", "length": 10},
)


class TestOAS32ValidatorValidate(TestOAS31ValidatorValidate):
"""OAS 3.2 uses the OAS 3.2 published dialect resources."""
Expand Down
Loading