Skip to content

Commit f96349f

Browse files
committed
Linglings suggestions
1 parent a47dd70 commit f96349f

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

synapseclient/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,7 @@ def build_parser():
18971897
"--fix-schema-name",
18981898
action="store_true",
18991899
default=False,
1900-
help="Fixes the schema name to meet Synapse requirements by replacing dashes and underscores with periods.",
1900+
help="Replaces dashes and underscores with periods in the schema name (e.g., 'my-schema_name' becomes 'my.schema.name')",
19011901
)
19021902
parser_register_json_schema.add_argument(
19031903
"--schema-version",

synapseclient/extensions/curator/schema_management.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import json
9+
import re
910
from typing import TYPE_CHECKING, Optional
1011

1112
from synapseclient.core.async_utils import wrap_async_to_sync
@@ -130,11 +131,13 @@ async def register_jsonschema_async(
130131
from synapseclient import Synapse
131132
from synapseclient.models.schema_organization import JSONSchema
132133

133-
if fix_schema_name:
134-
schema_name = schema_name.replace("-", ".").replace("_", ".")
135-
136134
syn = Synapse.get_client(synapse_client=synapse_client)
137135

136+
if fix_schema_name:
137+
old_name = schema_name
138+
schema_name = fix_name(schema_name)
139+
syn.logger.info(f"Changed schema name from '{old_name}' to '{schema_name}' ")
140+
138141
with open(schema_path, "r") as f:
139142
schema_body = json.load(f)
140143

@@ -154,6 +157,24 @@ async def register_jsonschema_async(
154157
return json_schema
155158

156159

160+
def fix_name(name: str) -> str:
161+
"""
162+
Fixes a schema name to meet Synapse requirements by:
163+
- replacing dashes and underscores with periods.
164+
- collapsing multiple consecutive periods into a single period.
165+
166+
Arguments:
167+
name: The original schema name
168+
169+
Returns:
170+
The fixed schema name
171+
172+
"""
173+
name = name.replace("-", ".").replace("_", ".")
174+
name = re.sub(r"\.+", ".", name)
175+
return name
176+
177+
157178
def bind_jsonschema(
158179
entity_id: str,
159180
json_schema_uri: str,

tests/unit/synapseclient/extensions/test_schema_management.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from synapseclient.extensions.curator import register_jsonschema_async
7+
from synapseclient.extensions.curator.schema_management import fix_name
78

89

910
@pytest.fixture
@@ -94,3 +95,28 @@ async def test_register_jsonschema_async_fix_schema_name(
9495
)
9596

9697
assert result.uri == "syn123.456"
98+
99+
100+
@pytest.mark.parametrize(
101+
"name, expected_fixed_name",
102+
[
103+
("name", "name"),
104+
("name.name", "name.name"),
105+
("name..name", "name.name"),
106+
("name-name", "name.name"),
107+
("name--name", "name.name"),
108+
("name_name", "name.name"),
109+
("name-_name", "name.name"),
110+
],
111+
ids=[
112+
"No special characters",
113+
"One period",
114+
"Multiple periods",
115+
"One dash",
116+
"Multiple dashes",
117+
"Underscore",
118+
"Mixed special characters",
119+
],
120+
)
121+
def test_fix_name(name, expected_fixed_name):
122+
assert fix_name(name) == expected_fixed_name

0 commit comments

Comments
 (0)