Skip to content

Commit 346e999

Browse files
committed
Clean up
1 parent 8c47c3c commit 346e999

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

openapi_schema_validator/_validators.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,34 @@ def handle_discriminator(validator, _, instance, schema):
99
prop_name = discriminator['propertyName']
1010
prop_value = instance.get(prop_name)
1111
if not prop_value:
12-
# instance is missing discriminator value, this is a schema error
12+
# instance is missing $propertyName
1313
yield ValidationError(
14-
"%r does not contain discriminating property" % (instance,),
14+
"%r does not contain discriminating property %r" % (instance, prop_name),
1515
context=[],
1616
)
1717
return
1818

1919
# FIXME: handle implicit refs and missing mapping field
20-
subschema = discriminator['mapping'].get(prop_value)
21-
if not subschema:
20+
explicitRef = discriminator['mapping'].get(prop_value)
21+
if not explicitRef:
2222
yield ValidationError(
2323
"%r is not a valid discriminator value, expected one of %r" % (
2424
instance, discriminator['mapping'].keys()),
2525
context=[],
2626
)
2727
return
2828

29-
if not isinstance(subschema, str):
29+
if not isinstance(explicitRef, str):
3030
# this is a schema error
3131
yield ValidationError(
3232
"%r mapped value for %r should be a string, was %r" % (
33-
instance, prop_value, subschema),
33+
instance, prop_value, explicitRef),
3434
context=[],
3535
)
3636
return
3737

3838
yield from validator.descend(instance, {
39-
"$ref": subschema
39+
"$ref": explicitRef
4040
})
4141

4242

tests/integration/test_validators.py

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -241,41 +241,47 @@ def test_oneof_required(self):
241241

242242

243243
def test_oneof_discriminator(self):
244-
schema = {
245-
"$ref": "#/$defs/Route",
246-
"$defs": {
247-
"MountainHiking": {
248-
"type": "object",
249-
"properties": {
250-
"discipline": {
251-
"type": "string",
252-
"enum": ["mountain_hiking"]
253-
},
254-
"length": {
255-
"type": "integer",
256-
}
244+
# We define a few components schemas
245+
components = {
246+
"MountainHiking": {
247+
"type": "object",
248+
"properties": {
249+
"discipline": {
250+
"type": "string",
251+
"enum": ["mountain_hiking"]
257252
},
258-
"required": ["discipline", "length"]
253+
"length": {
254+
"type": "integer",
255+
}
259256
},
260-
"AlpineClimbing": {
261-
"type": "object",
262-
"properties": {
263-
"discipline": {
264-
"type": "string",
265-
"enum": ["alpine_climbing"]
266-
},
267-
"height": {
268-
"type": "integer",
269-
},
257+
"required": ["discipline", "length"]
258+
},
259+
"AlpineClimbing": {
260+
"type": "object",
261+
"properties": {
262+
"discipline": {
263+
"type": "string",
264+
"enum": ["alpine_climbing"]
265+
},
266+
"height": {
267+
"type": "integer",
270268
},
271-
"required": ["discipline", "height"]
272269
},
273-
"Route": {
274-
"oneOf": [
275-
{"$ref": "#/$defs/MountainHiking"},
276-
{"$ref": "#/$defs/AlpineClimbing"},
277-
]
278-
}
270+
"required": ["discipline", "height"]
271+
},
272+
"Route": {
273+
"oneOf": [
274+
{"$ref": "#/components/schemas/MountainHiking"},
275+
{"$ref": "#/components/schemas/AlpineClimbing"},
276+
]
277+
}
278+
}
279+
280+
# Add the compoments in a minimalis schema
281+
schema = {
282+
"$ref": "#/components/schemas/Route",
283+
"components": {
284+
"schemas": components
279285
}
280286
}
281287

@@ -290,11 +296,11 @@ def test_oneof_discriminator(self):
290296
discriminator = {
291297
"propertyName": "discipline",
292298
"mapping": {
293-
"mountain_hiking": "#/$defs/MountainHiking",
294-
"alpine_climbing": "#/$defs/AlpineClimbing",
299+
"mountain_hiking": "#/components/schemas/MountainHiking",
300+
"alpine_climbing": "#/components/schemas/AlpineClimbing",
295301
}
296302
}
297-
schema['$defs']['Route']['discriminator'] = discriminator
303+
schema['components']['schemas']['Route']['discriminator'] = discriminator
298304
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
299305
with pytest.raises(ValidationError, match="does not contain discriminating property"):
300306
validator.validate({

0 commit comments

Comments
 (0)