Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

guiluko
Copy link

@guiluko guiluko commented May 3, 2023

Add support for examples Object in OpenAPI.

#248

@yedpodtrzitko
Copy link
Collaborator

Hi,
thanks for the PR. Can you please add a test to see how does this manifest? Thanks.

@guiluko
Copy link
Author

guiluko commented May 3, 2023

Hi, thanks for the PR. Can you please add a test to see how does this manifest? Thanks.

Hi thanks for the recommandation, I added some.

For the context, I did this PR to close this issue #248

Comment on lines +79 to +80
def has_examples(schema_exta: dict) -> bool:
for _, v in schema_exta.items():
Copy link
Collaborator

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:

Suggested change
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():
Copy link
Collaborator

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

Suggested change
if isinstance(v, dict) and "value" in v.keys():
if isinstance(v, dict) and "value" in v:

Comment on lines +97 to +99
schema_extra = getattr(model.__config__, "schema_extra", None)
if schema_extra and has_examples(schema_extra):
content_items["application/json"]["examples"] = schema_extra
Copy link
Collaborator

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():
Copy link
Collaborator

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:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants