-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -76,7 +76,14 @@ def parse_comments(func: Callable[..., Any]) -> Tuple[Optional[str], Optional[st | |||||
return summary, description | ||||||
|
||||||
|
||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. nitpick:
Suggested change
|
||||||
return True | ||||||
return False | ||||||
|
||||||
|
||||||
def parse_request(func: Any, model: Optional[Any] = None) -> Dict[str, Any]: | ||||||
""" | ||||||
get json spec | ||||||
""" | ||||||
|
@@ -86,6 +93,11 @@ def parse_request(func: Any) -> Dict[str, Any]: | |||||
"schema": {"$ref": f"#/components/schemas/{func.json}"} | ||||||
} | ||||||
|
||||||
if model: | ||||||
schema_extra = getattr(model.__config__, "schema_extra", None) | ||||||
if schema_extra and has_examples(schema_extra): | ||||||
content_items["application/json"]["examples"] = schema_extra | ||||||
Comment on lines
+97
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code checks if there's ANY dictionary with a key named
I think it could be better to nest all examples into
What do you think? |
||||||
|
||||||
if hasattr(func, "form"): | ||||||
content_items["multipart/form-data"] = { | ||||||
"schema": {"$ref": f"#/components/schemas/{func.form}"} | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,7 +7,7 @@ | |||||||||||||||||||||
from spectree.response import DEFAULT_CODE_DESC, Response | ||||||||||||||||||||||
from spectree.utils import gen_list_model | ||||||||||||||||||||||
|
||||||||||||||||||||||
from .common import JSON, DemoModel, get_model_path_key | ||||||||||||||||||||||
from .common import JSON, DemoModel, DemoModelWithSchemaExtra, get_model_path_key | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
class NormalClass: | ||||||||||||||||||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. if possible, can you use the fixture Lines 25 to 34 in 0508b70
to generate the snapshot, add the following parameter: |
||||||||||||||||||||||
resp = Response( | ||||||||||||||||||||||
"HTTP_200", | ||||||||||||||||||||||
HTTP_201=DemoModelWithSchemaExtra, | ||||||||||||||||||||||
HTTP_401=(DemoModelWithSchemaExtra, "custom code description"), | ||||||||||||||||||||||
HTTP_402=(None, "custom code description"), | ||||||||||||||||||||||
) | ||||||||||||||||||||||
resp.add_model(422, ValidationError) | ||||||||||||||||||||||
spec = resp.generate_spec() | ||||||||||||||||||||||
assert spec["200"]["description"] == DEFAULT_CODE_DESC["HTTP_200"] | ||||||||||||||||||||||
assert spec["201"]["description"] == DEFAULT_CODE_DESC["HTTP_201"] | ||||||||||||||||||||||
assert spec["422"]["description"] == DEFAULT_CODE_DESC["HTTP_422"] | ||||||||||||||||||||||
assert spec["401"]["description"] == "custom code description" | ||||||||||||||||||||||
assert spec["402"]["description"] == "custom code description" | ||||||||||||||||||||||
assert spec["201"]["content"]["application/json"]["schema"]["$ref"].split("/")[ | ||||||||||||||||||||||
-1 | ||||||||||||||||||||||
] == get_model_path_key("tests.common.DemoModelWithSchemaExtra") | ||||||||||||||||||||||
assert spec["201"]["content"]["application/json"]["examples"] == { | ||||||||||||||||||||||
"example1": {"value": {"key1": "value1", "key2": "value2"}} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
assert spec["422"]["content"]["application/json"]["schema"]["$ref"].split("/")[ | ||||||||||||||||||||||
-1 | ||||||||||||||||||||||
] == get_model_path_key("spectree.models.ValidationError") | ||||||||||||||||||||||
|
||||||||||||||||||||||
assert spec.get(200) is None | ||||||||||||||||||||||
assert spec.get(404) is None | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def test_list_model(): | ||||||||||||||||||||||
resp = Response(HTTP_200=List[JSON]) | ||||||||||||||||||||||
model = resp.find_model(200) | ||||||||||||||||||||||
|
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: