Skip to content

Commit 73f26f2

Browse files
committed
add fix_schema_name
1 parent da9c068 commit 73f26f2

5 files changed

Lines changed: 147 additions & 0 deletions

File tree

docs/tutorials/command_line_client.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ synapse register-json-schema [-h] [--schema-version VERSION] schema_path organiz
575575
| `organization_name` | Positional | Name of the organization to register the schema under | |
576576
| `schema_name` | Positional | The name of the JSON schema | |
577577
| `--schema-version` | Named | Version of the schema to register (e.g., '0.0.1'). If not specified, auto-generated | None |
578+
| `--fix-schema-name` | Named | Replace dashes and underscores in the schema name with periods. | False |
578579
579580
### `bind-json-schema`
580581

synapseclient/__main__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ def register_json_schema(args, syn):
824824
schema_path=args.schema_path,
825825
organization_name=args.organization_name,
826826
schema_name=args.schema_name,
827+
fix_schema_name=args.fix_schema_name,
827828
schema_version=args.schema_version,
828829
synapse_client=syn,
829830
)
@@ -1892,6 +1893,12 @@ def build_parser():
18921893
type=str,
18931894
help="The name of the JSON schema",
18941895
)
1896+
parser_register_json_schema.add_argument(
1897+
"--fix-schema-name",
1898+
action="store_true",
1899+
default=False,
1900+
help="Whether to fix the schema name to meet Synapse requirements",
1901+
)
18951902
parser_register_json_schema.add_argument(
18961903
"--schema-version",
18971904
dest="schema_version",

synapseclient/extensions/curator/schema_management.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def register_jsonschema(
2020
schema_path: str,
2121
organization_name: str,
2222
schema_name: str,
23+
fix_schema_name: bool = False,
2324
schema_version: Optional[str] = None,
2425
synapse_client: Optional["Synapse"] = None,
2526
) -> "JSONSchema":
@@ -33,6 +34,8 @@ def register_jsonschema(
3334
schema_path: Path to the JSON schema file to register
3435
organization_name: Name of the organization to register the schema under
3536
schema_name: Name of the JSON schema
37+
fix_schema_name: Whether to fix the schema name to meet Synapse requirements by replacing
38+
dashes and underscores with periods. Defaults to False.
3639
schema_version: Optional version of the schema (e.g., '0.0.1').
3740
If not specified, a version will be auto-generated.
3841
synapse_client: If not passed in and caching was not disabled by
@@ -54,6 +57,7 @@ def register_jsonschema(
5457
schema_path="/path/to/schema.json",
5558
organization_name="my.org",
5659
schema_name="my.schema",
60+
fix_schema_name=True,
5761
schema_version="0.0.1",
5862
synapse_client=syn
5963
)
@@ -65,6 +69,7 @@ def register_jsonschema(
6569
coroutine=register_jsonschema_async(
6670
schema_path=schema_path,
6771
organization_name=organization_name,
72+
fix_schema_name=fix_schema_name,
6873
schema_name=schema_name,
6974
schema_version=schema_version,
7075
synapse_client=synapse_client,
@@ -76,6 +81,7 @@ async def register_jsonschema_async(
7681
schema_path: str,
7782
organization_name: str,
7883
schema_name: str,
84+
fix_schema_name: bool = False,
7985
schema_version: Optional[str] = None,
8086
synapse_client: Optional["Synapse"] = None,
8187
) -> "JSONSchema":
@@ -89,6 +95,8 @@ async def register_jsonschema_async(
8995
schema_path: Path to the JSON schema file to register
9096
organization_name: Name of the organization to register the schema under
9197
schema_name: The name of the JSON schema
98+
fix_schema_name: Whether to fix the schema name to meet Synapse requirements by replacing
99+
dashes and underscores with periods. Defaults to False.
92100
schema_version: Optional version of the schema (e.g., '0.0.1').
93101
If not specified, a version will be auto-generated.
94102
synapse_client: If not passed in and caching was not disabled by
@@ -111,6 +119,7 @@ async def register_jsonschema_async(
111119
schema_path="/path/to/schema.json",
112120
organization_name="my.org",
113121
schema_name="my.schema",
122+
fix_schema_name=True,
114123
schema_version="0.0.1",
115124
synapse_client=syn
116125
))
@@ -121,6 +130,9 @@ async def register_jsonschema_async(
121130
from synapseclient import Synapse
122131
from synapseclient.models.schema_organization import JSONSchema
123132

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

126138
with open(schema_path, "r") as f:

tests/integration/synapseclient/test_command_line_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,31 @@ def test_register_json_schema(self, test_state, schema_organization, schema_file
13331333
assert "Successfully registered schema" in output
13341334
assert schema_name in output
13351335
assert schema_organization.name in output
1336+
assert False
1337+
1338+
def test_register_json_schema_fix_schema_name(
1339+
self, test_state, schema_organization, schema_file
1340+
):
1341+
"""Test register-json-schema CLI command"""
1342+
schema_name = f"test-schema_id{str(uuid.uuid4())[:8]}"
1343+
fixed_schema_name = schema_name.replace("-", ".").replace("_", ".")
1344+
1345+
output = run(
1346+
test_state,
1347+
"synapse",
1348+
"--skip-checks",
1349+
"register-json-schema",
1350+
schema_file,
1351+
schema_organization.name,
1352+
schema_name,
1353+
"--schema-version",
1354+
"1.0.0",
1355+
"--fix-schema-name",
1356+
)
1357+
1358+
assert "Successfully registered schema" in output
1359+
assert fixed_schema_name in output
1360+
assert schema_organization.name in output
13361361

13371362
def test_bind_json_schema(self, test_state, schema_organization, schema_file):
13381363
"""Test bind-json-schema CLI command"""
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import json
2+
from unittest.mock import AsyncMock, MagicMock, mock_open, patch
3+
4+
import pytest
5+
6+
from synapseclient.extensions.curator import register_jsonschema_async
7+
8+
9+
@pytest.fixture
10+
def mock_synapse_client():
11+
mock_client = MagicMock()
12+
mock_client.logger = MagicMock()
13+
return mock_client
14+
15+
16+
@pytest.fixture
17+
def mock_jsonschema():
18+
with patch("synapseclient.models.schema_organization.JSONSchema") as MockSchema:
19+
instance = MockSchema.return_value
20+
instance.store_async = AsyncMock()
21+
instance.uri = "syn123.456"
22+
yield MockSchema
23+
24+
25+
@pytest.mark.asyncio
26+
async def test_register_jsonschema_async(mock_synapse_client, mock_jsonschema):
27+
# Setup
28+
schema_path = "mock_path.json"
29+
schema_content = {"$id": "test_schema", "type": "object"}
30+
org_name = "test.org"
31+
schema_name = "my-schema_name"
32+
version = "1.0.0"
33+
34+
# Mock file reading and the Synapse client retrieval
35+
m_open = mock_open(read_data=json.dumps(schema_content))
36+
37+
with patch("builtins.open", m_open), patch(
38+
"synapseclient.Synapse.get_client", return_value=mock_synapse_client
39+
), patch("json.load", return_value=schema_content):
40+
result = await register_jsonschema_async(
41+
schema_path=schema_path,
42+
organization_name=org_name,
43+
schema_name=schema_name,
44+
schema_version=version,
45+
synapse_client=mock_synapse_client,
46+
)
47+
48+
# Verify the name was used as-is
49+
mock_jsonschema.assert_called_once_with(
50+
name=schema_name, organization_name=org_name
51+
)
52+
53+
# Verify the async store was called with the right data
54+
result.store_async.assert_awaited_once_with(
55+
schema_body=schema_content,
56+
version=version,
57+
synapse_client=mock_synapse_client,
58+
)
59+
60+
assert result.uri == "syn123.456"
61+
62+
63+
@pytest.mark.asyncio
64+
async def test_register_jsonschema_async_fix_schema_name(
65+
mock_synapse_client, mock_jsonschema
66+
):
67+
# Setup
68+
schema_path = "mock_path.json"
69+
schema_content = {"$id": "test_schema", "type": "object"}
70+
org_name = "test.org"
71+
schema_name = "my-schema_name"
72+
fixed_schema_name = "my.schema.name"
73+
version = "1.0.0"
74+
75+
# Mock file reading and the Synapse client retrieval
76+
m_open = mock_open(read_data=json.dumps(schema_content))
77+
78+
with patch("builtins.open", m_open), patch(
79+
"synapseclient.Synapse.get_client", return_value=mock_synapse_client
80+
), patch("json.load", return_value=schema_content):
81+
result = await register_jsonschema_async(
82+
schema_path=schema_path,
83+
organization_name=org_name,
84+
schema_name=schema_name,
85+
fix_schema_name=True,
86+
schema_version=version,
87+
synapse_client=mock_synapse_client,
88+
)
89+
90+
# Verify the name was fixed (dashes/underscores to dots)
91+
mock_jsonschema.assert_called_once_with(
92+
name=fixed_schema_name, organization_name=org_name
93+
)
94+
95+
# Verify the async store was called with the right data
96+
result.store_async.assert_awaited_once_with(
97+
schema_body=schema_content,
98+
version=version,
99+
synapse_client=mock_synapse_client,
100+
)
101+
102+
assert result.uri == "syn123.456"

0 commit comments

Comments
 (0)