Skip to content

Commit

Permalink
detects a path param in the right-most path segment
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolfix committed May 21, 2024
1 parent 1ce556a commit e3add05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
8 changes: 6 additions & 2 deletions dlt/sources/helpers/rest_client/detector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from typing import List, Dict, Any, Tuple, Union, Optional, Callable, Iterable
from pathlib import PurePosixPath
from typing import List, Dict, Any, Tuple, Union, Callable, Iterable
from urllib.parse import urlparse

from requests import Response
Expand Down Expand Up @@ -46,7 +47,10 @@

def single_entity_path(path: str) -> bool:
"""Checks if path ends with path param indicating that single object is returned"""
return re.search(r"\{([a-zA-Z_][a-zA-Z0-9_]*)\}/?$", path) is not None
# get last path segment
name = PurePosixPath(path).name
# alphabet for a name taken from https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#fixed-fields-6
return re.search(r"\{([a-zA-Z0-9\.\-_]+)\}", name) is not None


def matches_any_pattern(key: str, patterns: Iterable[str]) -> bool:
Expand Down
17 changes: 10 additions & 7 deletions tests/sources/helpers/rest_client/test_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,20 @@ def test_find_paginator(test_case) -> None:
[
"/users/{user_id}",
"/api/v1/products/{product_id}/",
# those are not valid paths
# "/api/v1/products/{product_id}//",
# "/api/v1/products/{product_id}?param1=value1",
# "/api/v1/products/{product_id}#section",
# "/api/v1/products/{product_id}/#section",
"/api/v1/products/{product_id}//",
"/api/v1/products/{product_id}?param1=value1",
"/api/v1/products/{product_id}#section",
"/api/v1/products/{product_id}.json",
"/api/v1/products/{product_id}.json/",
"/api/v1/products/{product_id}_data",
"/api/v1/products/{product_id}_data?param=true",
"/users/{user_id}/posts/{post_id}",
"/users/{user_id}/posts/{post_id}/comments/{comment_id}",
"{entity}",
"/{entity}",
"/{user_123}",
"/users/{user-id}",
"/users/{123}",
],
)
def test_single_entity_path_valid(path):
Expand All @@ -430,8 +434,7 @@ def test_single_entity_path_valid(path):
"/users/{user_id}/details",
"/",
"/{}",
"/users/{123}",
"/users/{user-id}",
"/api/v1/products/{product_id}/#section",
"/users/{user id}",
"/users/{user_id}/{", # Invalid ending
],
Expand Down

0 comments on commit e3add05

Please sign in to comment.