Skip to content

Commit

Permalink
feat: Add tag: to ASDF schema (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ketozhang authored Jun 28, 2024
2 parents 4f8b06a + 94610cb commit 0bacf81
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ venv.bak/
# Rope project settings
.ropeproject

# ruff
.ruff_cache/

# mkdocs documentation
/site

Expand Down
3 changes: 2 additions & 1 deletion asdf_pydantic/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ def schema_asdf(
---
$schema: {metaschema}
id: {cls._tag}
tag: tag:{cls._tag.split('://', maxsplit=2)[-1]}
"""
)
body = yaml.dump(cls.schema())
body = yaml.safe_dump(cls.model_json_schema())
return header + body
Empty file added tests/examples/__init__.py
Empty file.
54 changes: 54 additions & 0 deletions tests/examples/test_rectangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import asdf
from asdf.extension import Extension
from asdf.schema import check_schema, load_schema

from asdf_pydantic import AsdfPydanticConverter
from asdf_pydantic.examples.shapes import AsdfRectangle


def setup_module():
AsdfPydanticConverter.add_models(AsdfRectangle)

class TestExtension(Extension):
extension_uri = "asdf://asdf-pydantic/examples/extensions/test-1.0.0" # type: ignore

tags = [*AsdfPydanticConverter().tags] # type: ignore
converters = [AsdfPydanticConverter()] # type: ignore

# HACK: The schema URI should be referenced from `AsdfRectangle._schema`.
# Then there should be a way to automatically add the schema to ASDF
# resources perhaps during AsdfPydanticConverter.add_models(). Further
# abstracting can be done later, perhaps defining a
# AsdfPydanticExtension.
asdf.get_config().add_resource_mapping(
{
"asdf://asdf-pydantic/shapes/schemas/rectangle-1.0.0": (
AsdfRectangle.schema_asdf().encode("utf-8")
)
}
)
asdf.get_config().add_extension(TestExtension())


def test_schema():
schema = load_schema("asdf://asdf-pydantic/shapes/schemas/rectangle-1.0.0")

check_schema(schema)

assert schema["$schema"] == "http://stsci.edu/schemas/asdf/asdf-schema-1.0.0"
assert schema["title"] == "AsdfRectangle"
assert schema["id"] == "asdf://asdf-pydantic/examples/tags/rectangle-1.0.0"
assert schema["tag"] == "tag:asdf-pydantic/examples/tags/rectangle-1.0.0"
assert schema["type"] == "object"
assert schema["properties"] == {
"width": {
"type": "number",
"title": "Width",
},
"height": {
"type": "number",
"title": "Height",
},
}

assert schema["required"] == ["width", "height"]
2 changes: 1 addition & 1 deletion tests/schema_validation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import pytest
import yaml
from asdf.extension import Extension
from asdf_pydantic import AsdfPydanticConverter

from asdf_pydantic import AsdfPydanticConverter
from asdf_pydantic.examples.shapes import AsdfRectangle
from asdf_pydantic.examples.tree import AsdfNode

Expand Down

0 comments on commit 0bacf81

Please sign in to comment.