From 20055fb1823d0330002ed60d1a8799ddad03c136 Mon Sep 17 00:00:00 2001 From: Douglas Rioux Date: Mon, 13 May 2024 11:35:24 -0400 Subject: [PATCH 1/4] Add more schema parsing functions --- dcicutils/schema_utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/dcicutils/schema_utils.py b/dcicutils/schema_utils.py index b5b1bf425..4c98ea9e9 100644 --- a/dcicutils/schema_utils.py +++ b/dcicutils/schema_utils.py @@ -24,6 +24,7 @@ class JsonSchemaConstants: class EncodedSchemaConstants: + DESCRIPTION = "description" IDENTIFYING_PROPERTIES = "identifyingProperties" LINK_TO = "linkTo" MERGE_REF = "$merge" @@ -187,6 +188,21 @@ def get_one_of_formats(schema: Dict[str, Any]) -> List[str]: ] +def is_link(property_schema: Dict[str, Any]) -> bool: + """Is property schema a link?""" + return property_schema.get(SchemaConstants.LINK_TO, False) + + +def get_enum(property_schema: Dict[str, Any]) -> List[str]: + """Return the enum of a property schema.""" + return property_schema.get(SchemaConstants.ENUM, []) + + +def get_description(schema: Dict[str, Any]) -> str: + """Return the description of a schema.""" + return schema.get(SchemaConstants.DESCRIPTION, "") + + class Schema: def __init__(self, schema: dict, type: Optional[str] = None) -> None: From 511c8fc9f66a1bc9a4a3f96c931f0dd2dab62871 Mon Sep 17 00:00:00 2001 From: Douglas Rioux Date: Mon, 13 May 2024 11:45:28 -0400 Subject: [PATCH 2/4] Bump beta version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e34d933d0..7dd32cf1e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dcicutils" -version = "8.8.4" +version = "8.9.0.0b0" description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources" authors = ["4DN-DCIC Team "] license = "MIT" From 6943868e5d6ab96265d6ab12a62da351e7404c54 Mon Sep 17 00:00:00 2001 From: Douglas Rioux Date: Tue, 14 May 2024 10:34:15 -0400 Subject: [PATCH 3/4] Add tests + fix bug --- dcicutils/schema_utils.py | 2 +- test/test_schema_utils.py | 47 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/dcicutils/schema_utils.py b/dcicutils/schema_utils.py index 4c98ea9e9..82aa6bb12 100644 --- a/dcicutils/schema_utils.py +++ b/dcicutils/schema_utils.py @@ -190,7 +190,7 @@ def get_one_of_formats(schema: Dict[str, Any]) -> List[str]: def is_link(property_schema: Dict[str, Any]) -> bool: """Is property schema a link?""" - return property_schema.get(SchemaConstants.LINK_TO, False) + return bool(property_schema.get(SchemaConstants.LINK_TO)) def get_enum(property_schema: Dict[str, Any]) -> List[str]: diff --git a/test/test_schema_utils.py b/test/test_schema_utils.py index 6b948b8ed..8a928ee3b 100644 --- a/test/test_schema_utils.py +++ b/test/test_schema_utils.py @@ -1,4 +1,4 @@ -from typing import Any, Dict +from typing import Any, Dict, List import pytest from dcicutils import schema_utils @@ -41,7 +41,16 @@ } FORMAT = "email" PATTERN = "some_regex" -STRING_SCHEMA = {"type": "string", "format": FORMAT, "pattern": PATTERN} +ENUM = ["foo", "bar"] +DESCRIPTION = "foo" +STRING_SCHEMA = { + "type": "string", + "format": FORMAT, + "pattern": PATTERN, + "linkTo": "foo", + "enum": ENUM, + "description": DESCRIPTION, +} ARRAY_SCHEMA = {"type": "array", "items": [STRING_SCHEMA]} OBJECT_SCHEMA = {"type": "object", "properties": {"foo": STRING_SCHEMA}} NUMBER_SCHEMA = {"type": "number"} @@ -320,3 +329,37 @@ def test_get_conditional_formats( schema: Dict[str, Any], expected: Dict[str, Any] ) -> None: assert schema_utils.get_conditional_formats(schema) == expected + + +@pytest.mark.parametrize( + "schema,expected", + [ + ({}, False), + (STRING_SCHEMA, True), + (FORMAT_SCHEMA, False), + ], +) +def test_is_link(schema: Dict[str, Any], expected: bool) -> None: + assert schema_utils.is_link(schema) == expected + + +@pytest.mark.parametrize( + "schema,expected", + [ + ({}, []), + (STRING_SCHEMA, ENUM), + ], +) +def test_get_enum(schema: Dict[str, Any], expected: List[str]) -> None: + assert schema_utils.get_enum(schema) == expected + + +@pytest.mark.parametrize( + "schema,expected", + [ + ({}, ""), + (STRING_SCHEMA, DESCRIPTION), + ], +) +def test_get_description(schema: Dict[str, Any], expected: str) -> None: + assert schema_utils.get_description(schema) == expected From 8f5af3bbd9b448d460f81a368a025ac8e887cb5b Mon Sep 17 00:00:00 2001 From: Douglas Rioux Date: Thu, 16 May 2024 14:01:07 -0400 Subject: [PATCH 4/4] De-beta + bump changelog --- CHANGELOG.rst | 5 +++++ pyproject.toml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 1054c4c08..e8cbc342e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,11 @@ Change Log ---------- +8.9.0 +===== +* Add more schema parsing functions to `schema_utils`. + + 8.8.4 ===== * Minor fix in structured_data to not try to resolve empty refs in norefs mode; diff --git a/pyproject.toml b/pyproject.toml index 7dd32cf1e..60d2516bc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dcicutils" -version = "8.9.0.0b0" +version = "8.9.0" description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources" authors = ["4DN-DCIC Team "] license = "MIT"