-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added an extensible API for stream schema sources
- Loading branch information
1 parent
61a0b88
commit 282ad28
Showing
6 changed files
with
9,044 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"""Schema sources.""" | ||
|
||
from __future__ import annotations | ||
|
||
import functools | ||
import json | ||
import sys | ||
import typing as t | ||
from pathlib import Path | ||
|
||
import requests | ||
|
||
from singer_sdk._singerlib import resolve_schema_references | ||
|
||
if sys.version_info < (3, 12): | ||
from importlib.abc import Traversable | ||
else: | ||
from importlib.resources.abc import Traversable | ||
|
||
|
||
class BaseSchemaSource: | ||
"""Base schema source.""" | ||
|
||
def __init__(self) -> None: | ||
"""Initialize the schema source.""" | ||
self._registry: dict[str, dict] = {} | ||
|
||
def get_schema(self, *args: t.Any, **kwargs: t.Any) -> dict: | ||
"""Get schema from reference. | ||
Raises: | ||
NotImplementedError: If the method is not implemented by the subclass. | ||
""" | ||
msg = "Subclasses must implement this method." | ||
raise NotImplementedError(msg) | ||
|
||
def __call__(self, *args: t.Any, **kwargs: t.Any) -> dict: | ||
"""Get schema for the given stream name or reference. | ||
Returns: | ||
The schema dictionary. | ||
""" | ||
return self.get_schema(*args, **kwargs) | ||
|
||
|
||
class LocalSchemaSource(BaseSchemaSource): | ||
"""Local schema source.""" | ||
|
||
def __init__(self, path: Path | Traversable) -> None: | ||
"""Initialize the schema source.""" | ||
super().__init__() | ||
self.path = path | ||
|
||
def get_schema(self, name: str) -> dict: | ||
"""Get schema from reference. | ||
Args: | ||
name: Name of the stream. | ||
Returns: | ||
The schema dictionary. | ||
""" | ||
if name not in self._registry: | ||
schema_path = self.path / f"{name}.json" | ||
self._registry[name] = json.loads(schema_path.read_text()) | ||
|
||
return self._registry[name] | ||
|
||
|
||
class OpenAPISchemaSource(BaseSchemaSource): | ||
"""OpenAPI schema source.""" | ||
|
||
def __init__(self, path: str | Path | Traversable) -> None: | ||
"""Initialize the schema source.""" | ||
super().__init__() | ||
self.path = path | ||
|
||
@functools.cached_property | ||
def spec_dict(self) -> dict: | ||
"""OpenAPI spec dictionary. | ||
Raises: | ||
ValueError: If the path type is not supported. | ||
""" | ||
if isinstance(self.path, (Path, Traversable)): | ||
return json.loads(self.path.read_text()) # type: ignore[no-any-return] | ||
|
||
if self.path.startswith("http"): | ||
return requests.get(self.path, timeout=10).json() # type: ignore[no-any-return] | ||
|
||
msg = f"Unsupported path type: {self.path}" | ||
raise ValueError(msg) | ||
|
||
def get_schema(self, ref: str) -> dict: | ||
"""Get schema from reference. | ||
Args: | ||
ref: Reference to the schema. | ||
Returns: | ||
The schema dictionary. | ||
""" | ||
if ref not in self._registry: | ||
schema = {"$ref": f"#/components/schemas/{ref}"} | ||
schema["components"] = self.spec_dict["components"] | ||
self._registry[ref] = resolve_schema_references(schema) | ||
|
||
return self._registry[ref] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,32 @@ | ||
""" | ||
Testing that Schema can convert schemas lossless from and to dicts. | ||
Schemas are taken from these examples; | ||
https://json-schema.org/learn/miscellaneous-examples.html | ||
NOTE: The following properties are not currently supported; | ||
pattern | ||
unevaluatedProperties | ||
propertyNames | ||
minProperties | ||
maxProperties | ||
prefixItems | ||
contains | ||
minContains | ||
maxContains | ||
minItems | ||
maxItems | ||
uniqueItems | ||
enum | ||
const | ||
contentMediaType | ||
contentEncoding | ||
allOf | ||
oneOf | ||
not | ||
Some of these could be trivially added (if they are SIMPLE_PROPERTIES. | ||
Some might need more thinking if they can contain schemas (though, note that we also | ||
treat 'additionalProperties', 'anyOf' and' patternProperties' as SIMPLE even though they | ||
can contain schemas. | ||
""" | ||
"""Test the schema sources.""" | ||
|
||
from __future__ import annotations | ||
|
||
from singer_sdk._singerlib import Schema | ||
import typing as t | ||
|
||
from singer_sdk.schema import LocalSchemaSource, OpenAPISchemaSource | ||
|
||
if t.TYPE_CHECKING: | ||
import pytest | ||
|
||
def test_simple_schema(): | ||
simple_schema = { | ||
"title": "Longitude and Latitude Values", | ||
"description": "A geographical coordinate.", | ||
"required": ["latitude", "longitude"], | ||
"type": "object", | ||
"properties": { | ||
"latitude": {"type": "number", "minimum": -90, "maximum": 90}, | ||
"longitude": {"type": "number", "minimum": -180, "maximum": 180}, | ||
}, | ||
} | ||
|
||
schema_plus = Schema.from_dict(simple_schema) | ||
assert schema_plus.to_dict() == simple_schema | ||
assert schema_plus.required == ["latitude", "longitude"] | ||
assert isinstance(schema_plus.properties["latitude"], Schema) | ||
latitude = schema_plus.properties["latitude"] | ||
assert latitude.type == "number" | ||
def test_local_schema_source(pytestconfig: pytest.Config): | ||
schema_dir = pytestconfig.rootpath / "tests/fixtures/schemas" | ||
schema_source = LocalSchemaSource(schema_dir) | ||
schema = schema_source("user") | ||
assert isinstance(schema, dict) | ||
assert schema["type"] == "object" | ||
assert "items" not in schema | ||
assert "properties" in schema | ||
assert "id" in schema["properties"] | ||
|
||
|
||
def test_schema_with_items(): | ||
schema = { | ||
"description": "A representation of a person, company, organization, or place", | ||
"type": "object", | ||
"properties": {"fruits": {"type": "array", "items": {"type": "string"}}}, | ||
} | ||
schema_plus = Schema.from_dict(schema) | ||
assert schema_plus.to_dict() == schema | ||
assert isinstance(schema_plus.properties["fruits"], Schema) | ||
fruits = schema_plus.properties["fruits"] | ||
assert isinstance(fruits.items, Schema) | ||
assert fruits.items.type == "string" | ||
def test_openapi_schema_source(pytestconfig: pytest.Config): | ||
openapi_path = pytestconfig.rootpath / "tests/fixtures/openapi.json" | ||
schema_source = OpenAPISchemaSource(openapi_path) | ||
schema = schema_source("ProjectListItem") | ||
assert isinstance(schema, dict) | ||
assert schema["type"] == "object" | ||
assert "items" not in schema | ||
assert "properties" in schema | ||
assert "id" in schema["properties"] |
Oops, something went wrong.