Skip to content

Commit 6b1a8e1

Browse files
authored
feat: added support for Parameter example/examples fields (#105)
1 parent 3b103fc commit 6b1a8e1

4 files changed

Lines changed: 69 additions & 2 deletions

File tree

src/openapi_parser/builders/parameter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def build(self, data: dict[str, Any]) -> Parameter:
8080
cast=self._content_builder.build_list,
8181
),
8282
"description": PropertyMeta(name="description", cast=str),
83+
"example": PropertyMeta(name="example", cast=None),
84+
"examples": PropertyMeta(name="examples", cast=dict),
8385
"deprecated": PropertyMeta(name="deprecated", cast=bool),
8486
"explode": PropertyMeta(name="explode", cast=bool),
8587
}

src/openapi_parser/specification.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ class Parameter:
212212
content: list[Content] | None = None
213213
required: bool | None = field(default=False)
214214
description: str | None = None
215-
# example: Optional[Any] # TODO
216-
# examples: list[Any] = field(default_factory=list) # TODO
215+
example: Any | None = None
216+
examples: dict[str, Any] = field(default_factory=dict)
217217
# allow_reserved: bool # TODO
218218
deprecated: bool | None = field(default=False)
219219
style: (

tests/builders/test_parameter_builder.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,68 @@ def _get_content_builder_mock(
129129
_get_schema_factory_mock(string_schema),
130130
_get_content_builder_mock(None),
131131
),
132+
(
133+
{
134+
"name": "limit",
135+
"in": "query",
136+
"required": True,
137+
"schema": {
138+
"type": "integer",
139+
},
140+
"example": 10,
141+
},
142+
Parameter(
143+
name="limit",
144+
location=ParameterLocation.QUERY,
145+
required=True,
146+
schema=string_schema,
147+
style=QueryParameterStyle.FORM,
148+
explode=True,
149+
example=10,
150+
),
151+
_get_schema_factory_mock(string_schema),
152+
_get_content_builder_mock(None),
153+
),
154+
(
155+
{
156+
"name": "filter",
157+
"in": "query",
158+
"required": False,
159+
"schema": {
160+
"type": "string",
161+
},
162+
"examples": {
163+
"foo": {
164+
"summary": "A foo example",
165+
"value": {"foo": "bar"},
166+
},
167+
"bar": {
168+
"summary": "A bar example",
169+
"value": {"bar": "baz"},
170+
},
171+
},
172+
},
173+
Parameter(
174+
name="filter",
175+
location=ParameterLocation.QUERY,
176+
required=False,
177+
schema=string_schema,
178+
style=QueryParameterStyle.FORM,
179+
explode=True,
180+
examples={
181+
"foo": {
182+
"summary": "A foo example",
183+
"value": {"foo": "bar"},
184+
},
185+
"bar": {
186+
"summary": "A bar example",
187+
"value": {"bar": "baz"},
188+
},
189+
},
190+
),
191+
_get_schema_factory_mock(string_schema),
192+
_get_content_builder_mock(None),
193+
),
132194
)
133195

134196
collection_data_provider = (

tests/openapi_fixture.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ def create_specification() -> Specification:
308308
required=True,
309309
explode=False,
310310
style=PathParameterStyle.SIMPLE,
311+
example="12345678-1234-5678-1234-567812345678",
311312
schema=String(
312313
type=DataType.STRING,
313314
format=StringFormat.UUID,
@@ -333,6 +334,7 @@ def create_specification() -> Specification:
333334
required=True,
334335
explode=True,
335336
style=QueryParameterStyle.FORM,
337+
example=10,
336338
schema=Integer(type=DataType.INTEGER),
337339
),
338340
Parameter(
@@ -342,6 +344,7 @@ def create_specification() -> Specification:
342344
required=True,
343345
explode=True,
344346
style=QueryParameterStyle.FORM,
347+
example=0,
345348
schema=Integer(type=DataType.INTEGER),
346349
),
347350
Parameter(

0 commit comments

Comments
 (0)