-
-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Support for multiple examples in application/json #309
base: master
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
@@ -107,6 +107,34 @@ def test_response_spec(): | |||
assert spec.get(404) is None | |||
|
|||
|
|||
def test_response_spec_with_schema_extra(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
def test_plugin_spec(api, snapshot_json): | |
models = { | |
get_model_key(model=m): get_model_schema(model=m) | |
for m in (Query, JSON, Resp, Cookies, Headers) | |
} | |
for name, schema in models.items(): | |
schema.pop("definitions", None) | |
assert api.spec["components"]["schemas"][name] == schema | |
assert api.spec == snapshot_json(name="full_spec") |
to generate the snapshot, add the following parameter: pytest --snapshot-update
Add support for
examples
Object in OpenAPI.#248