Add Support for multiple examples in application/json#309
Add Support for multiple examples in application/json#309guiluko wants to merge 2 commits into0b01001001:masterfrom
Conversation
|
Hi, |
Hi thanks for the recommandation, I added some. For the context, I did this PR to close this issue #248 |
| def has_examples(schema_exta: dict) -> bool: | ||
| for _, v in schema_exta.items(): |
There was a problem hiding this comment.
this suggestion might be rendered useless by another comment below, but:
| def has_examples(schema_exta: dict) -> bool: | |
| for _, v in schema_exta.items(): | |
| def has_examples(schema_extra: dict) -> bool: | |
| for v in schema_extra.values(): |
| def parse_request(func: Any) -> Dict[str, Any]: | ||
| def has_examples(schema_exta: dict) -> bool: | ||
| for _, v in schema_exta.items(): | ||
| if isinstance(v, dict) and "value" in v.keys(): |
There was a problem hiding this comment.
nitpick: .keys() is superfluous since it's the default place where in is checking
| if isinstance(v, dict) and "value" in v.keys(): | |
| if isinstance(v, dict) and "value" in v: |
| schema_extra = getattr(model.__config__, "schema_extra", None) | ||
| if schema_extra and has_examples(schema_extra): | ||
| content_items["application/json"]["examples"] = schema_extra |
There was a problem hiding this comment.
This code checks if there's ANY dictionary with a key named value in schema_extra, and based on that it assumes that ALL items in schema_extra are examples. Which might not be necessarily the case:
class Data(BaseModel):
value: bool
class Config:
schema_extra = {
"examples": {
"example1": {"value": {"key1": "value1", "key2": "value2"}},
"example2": {"value": {"key1": "value1", "key2": "value2"}},
},
"properties": {"value": {"this is not an example": True}},
}
I think it could be better to nest all examples into schema_extra.examples, check presence of examples, and then then take just that as examples:
if schema_extra and "examples" in schema_extra:
content_items["application/json"]["examples"] = schema_extra["examples"]
What do you think?
| assert spec.get(404) is None | ||
|
|
||
|
|
||
| def test_response_spec_with_schema_extra(): |
There was a problem hiding this comment.
if possible, can you use the fixture snapshot_json and compare the whole payload instead of cherrypicked parts? See example here:
Lines 25 to 34 in 0508b70
to generate the snapshot, add the following parameter: pytest --snapshot-update
Add support for
examplesObject in OpenAPI.#248