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

corrects single entity path detection #1394

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading