Skip to content

Commit acd27e8

Browse files
committed
fix: Ignoring nested examples
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent 767b39c commit acd27e8

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Changelog
44
:version:`Unreleased <v3.33.0...HEAD>` - TBD
55
--------------------------------------------
66

7+
**Fixed**
8+
9+
- Ignoring nested examples. :issue:`2358`
10+
711
.. _v3.33.0:
812

913
:version:`3.33.0 <v3.32.2...v3.33.0>` - 2024-07-19

src/schemathesis/specs/openapi/examples.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ def extract_from_schema(
241241
if examples_field_name in subsubschema and isinstance(subsubschema[examples_field_name], list):
242242
# These are JSON Schema examples, which is an array of values
243243
values.extend(subsubschema[examples_field_name])
244+
# Check nested examples as well
245+
values.extend(extract_from_schema(operation, subsubschema, example_field_name, examples_field_name))
244246
if not values:
245247
if name in required:
246248
# Defer generation to only generate these variants if at least one property has examples

test/specs/openapi/test_examples.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,3 +1116,132 @@ def test_openapi_2_example():
11161116
"media_type": "application/json",
11171117
}
11181118
]
1119+
1120+
1121+
def test_property_examples_behind_ref():
1122+
raw_schema = {
1123+
"swagger": "2.0",
1124+
"info": {"version": "0.1.0", "title": "Item List API"},
1125+
"schemes": ["http"],
1126+
"host": "localhost:8083",
1127+
"securityDefinitions": {"ApiKeyAuth": {"in": "header", "name": "Authorization", "type": "apiKey"}},
1128+
"paths": {
1129+
"/trees": {
1130+
"post": {
1131+
"responses": {"200": {"description": "Ok"}},
1132+
"parameters": [
1133+
{
1134+
"in": "body",
1135+
"name": "Tree",
1136+
"schema": {"$ref": "#/definitions/Tree"},
1137+
"description": "tree to create",
1138+
"required": True,
1139+
}
1140+
],
1141+
}
1142+
},
1143+
},
1144+
"definitions": {
1145+
"Tree": {
1146+
"properties": {
1147+
"id": {
1148+
"format": "uuid",
1149+
"type": "string",
1150+
"example": "415feabd-9114-44af-bc78-479299dadc1e",
1151+
},
1152+
"year": {"pattern": "^\\d{4}", "type": "string", "example": "2020"},
1153+
"branches": {"items": {"$ref": "#/definitions/Branch"}, "type": "array"},
1154+
"description": {"type": "string", "example": "white birch tree"},
1155+
"name": {"type": "string", "example": "Birch"},
1156+
"bird": {"$ref": "#/definitions/Bird", "type": "object"},
1157+
},
1158+
"required": ["name", "description"],
1159+
"type": "object",
1160+
"example": {"description": "Pine tree", "name": "Pine"},
1161+
},
1162+
"Nest": {
1163+
"properties": {
1164+
"eggs": {"items": {"$ref": "#/definitions/Egg"}, "type": "array"},
1165+
"description": {"type": "string", "example": "first nest"},
1166+
"name": {"type": "string", "example": "nest1"},
1167+
},
1168+
"required": ["name"],
1169+
"type": "object",
1170+
},
1171+
"Bark": {
1172+
"properties": {
1173+
"description": {"type": "string", "example": "brown bark"},
1174+
"name": {"type": "string", "example": "bark1"},
1175+
},
1176+
"required": ["name", "description"],
1177+
"type": "object",
1178+
},
1179+
"Leaf": {
1180+
"properties": {
1181+
"description": {"type": "string", "example": "main leaf"},
1182+
"name": {"type": "string", "example": "leaf1"},
1183+
},
1184+
"required": ["name", "description"],
1185+
"type": "object",
1186+
},
1187+
"Branch": {
1188+
"properties": {
1189+
"leaves": {"items": {"$ref": "#/definitions/Leaf"}, "type": "array"},
1190+
"bark": {"$ref": "#/definitions/Bark", "type": "object"},
1191+
"description": {"type": "string", "example": "main branch"},
1192+
"name": {"type": "string", "example": "branch1"},
1193+
},
1194+
"required": ["name", "description"],
1195+
"type": "object",
1196+
},
1197+
"Bird": {
1198+
"properties": {
1199+
"nest": {"$ref": "#/definitions/Nest", "type": "object"},
1200+
"description": {"type": "string", "example": "brown sparrow"},
1201+
"name": {"type": "string", "example": "sparrow"},
1202+
},
1203+
"required": ["name", "description"],
1204+
"type": "object",
1205+
},
1206+
"Trees": {"items": {"$ref": "#/definitions/Tree"}, "type": "array"},
1207+
"Egg": {
1208+
"properties": {
1209+
"description": {"type": "string", "example": "first egg"},
1210+
"name": {"type": "string", "example": "egg1"},
1211+
},
1212+
"required": ["name", "description"],
1213+
"type": "object",
1214+
},
1215+
},
1216+
}
1217+
schema = schemathesis.from_dict(raw_schema)
1218+
operation = schema["/trees"]["POST"]
1219+
extracted = [example_to_dict(example) for example in examples.extract_from_schemas(operation)]
1220+
assert extracted == [
1221+
{
1222+
"value": {
1223+
"id": "415feabd-9114-44af-bc78-479299dadc1e",
1224+
"year": "2020",
1225+
"branches": [
1226+
{
1227+
"leaves": [{"description": "main leaf", "name": "leaf1"}],
1228+
"bark": {"description": "brown bark", "name": "bark1"},
1229+
"description": "main branch",
1230+
"name": "branch1",
1231+
}
1232+
],
1233+
"description": "white birch tree",
1234+
"name": "Birch",
1235+
"bird": {
1236+
"nest": {
1237+
"eggs": [{"description": "first egg", "name": "egg1"}],
1238+
"description": "first nest",
1239+
"name": "nest1",
1240+
},
1241+
"description": "brown sparrow",
1242+
"name": "sparrow",
1243+
},
1244+
},
1245+
"media_type": "application/json",
1246+
}
1247+
]

0 commit comments

Comments
 (0)