Skip to content

Commit

Permalink
extra - reduce by operationId + keep non-op keys
Browse files Browse the repository at this point in the history
If the key is None, instead of looking based on path + method it will search for the operationId. This is slightly slower than dict comprehension, but still quick and the code is a bit more readable.

A path's non-operation objects are also kept.

(cherry picked from commit 6b58d47)
  • Loading branch information
ggpwnkthx authored and commonism committed Sep 26, 2023
1 parent fe2ee66 commit 9c7b594
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions aiopenapi3/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,44 @@ def __init__(self, operations: Dict[Union[str, Pattern], List[Union[str, Pattern
super().__init__()

def _reduced_paths(self, ctx: "Document.Context") -> dict:
return {
key: {
operation_key: operation_value
for pattern in operation_patterns
for operation_key, operation_value in ctx.document["paths"][key].items()
if isinstance(operation_value, str)
or (
(isinstance(pattern, str) and pattern == operation_key)
or (isinstance(pattern, re.Pattern) and re.match(pattern, operation_key))
)
reduced_paths = {}
for path_key, path_value in ctx.document["paths"].items():
# Extracting Non-Operation Objects
non_op_objects = {
key: val
for key, val in path_value.items()
if key in {"summary", "description", "servers", "parameters"}
}
if operation_patterns
else ctx.document["paths"][key]
for key, operation_patterns in {
path_key: operation_patterns
for pattern, operation_patterns in self.operations.items()
for path_key in ctx.document["paths"].keys()
if (isinstance(pattern, str) and pattern == path_key)
or (isinstance(pattern, re.Pattern) and re.match(pattern, path_key))
}.items()
}

for operation_key, operation_value in path_value.items():
if operation_key in non_op_objects: # Skip if the key is a Non-Operation Object
continue

for pattern, operation_patterns in self.operations.items():
# If pattern is None, look for operationId in operation_patterns
if pattern is None and isinstance(operation_value, dict):
operation_id = operation_value.get("operationId", "")
if any(
op_pattern == operation_id
or (isinstance(op_pattern, Pattern) and re.match(op_pattern, operation_id))
for op_pattern in operation_patterns
):
reduced_paths.setdefault(path_key, {}).update(non_op_objects)
reduced_paths[path_key][operation_key] = operation_value

else:
if (
(isinstance(pattern, str) and pattern == path_key)
or (isinstance(pattern, Pattern) and re.match(pattern, path_key))
) and any(
op_pattern == operation_key
or (isinstance(op_pattern, Pattern) and re.match(op_pattern, operation_key))
for op_pattern in operation_patterns
):
reduced_paths.setdefault(path_key, {}).update(non_op_objects)
reduced_paths[path_key][operation_key] = operation_value

return reduced_paths

def parsed(self, ctx: "Document.Context") -> "Document.Context":
"""Parse the given context."""
Expand Down Expand Up @@ -140,14 +157,13 @@ def parsed(self, ctx: "Document.Context") -> "Document.Context":
"""
# Exclude certain keys from the document
document = {k: v for k, v in ctx.document.items() if k not in ["components", "paths", "tags"]}

# Restore security schemes
document["components"] = {"securitySchemes": ctx.document.get("components", {}).get("securitySchemes", {})}
# Process paths in the document
document["paths"] = self._reduced_paths(ctx)

# Update references in the document
while self._update_references(ctx.document, document):
pass

# Rebuild Tags
tag_names = list(
set(
Expand Down

0 comments on commit 9c7b594

Please sign in to comment.