Repository: meltano/sdk

Issue:
bug: JSON schema references are incorrectly resolved when mulitple properties use the same reference
### Singer SDK Version

0.46.4

### Is this a regression?

- [x] Yes

### Python Version

NA

### Bug scope

Taps (catalog, state, etc.)

### Operating System

NA

### Description

```python
from singer_sdk.singerlib resolve_schema_references

resolve_schema_references(
    {
        "type": "object",
        "properties": {
            "min_compute_units": {"$ref": "components#/schemas/ComputeUnit"},
             "max_compute_units": {"$ref": "components#/schemas/ComputeUnit"},
        },
    },
    {"components": {"schemas": {"ComputeUnit": {"type": "number"}}}},
)

# Expected:
# {
#     "type": "object",
#     "properties": {
#         "min_compute_units": {"type": "number"},
#         "max_compute_units": {"type": "number"},
#     },
# }

# Actual
# {
#     "type": "object",
#     "properties": {
#         "min_compute_units": {"type": "number"},
#         "max_compute_units": {},  # Presumably ComputeUnit had already been visited
#     },
# }
```


### Link to Slack/Linen

_No response_

Hints from issue comments:

Solution patch:
diff --git a/singer_sdk/singerlib/schema.py b/singer_sdk/singerlib/schema.py
index 035851fe..7754f835 100644
--- a/singer_sdk/singerlib/schema.py
+++ b/singer_sdk/singerlib/schema.py
@@ -254,33 +254,34 @@ def resolve_schema_references(
 def _resolve_schema_references(  # noqa: C901, PLR0912
     schema: dict[str, t.Any],
     resolver: Resolver,
-    visited_refs: set[str] | None = None,
+    visited_refs: tuple[str, ...] | None = None,
 ) -> dict[str, t.Any]:
     """Recursively resolve schema references while handling circular references.
 
     Args:
         schema: The schema dict to resolve references in
         resolver: The JSON Schema resolver to use
-        visited_refs: Set of already visited reference paths to prevent infinite
-            recursion
+        visited_refs: Tuple of already visited reference paths in the current call stack
+            to prevent infinite recursion
 
     Returns:
         The schema with all references resolved
     """
     if visited_refs is None:
-        visited_refs = set()
+        visited_refs = ()
 
     if _SchemaKey.ref in schema:
         reference_path = schema.pop(_SchemaKey.ref, None)
         if reference_path in visited_refs:
-            # We've already seen this reference, return the schema as-is
-            # to prevent infinite recursion
+            # We've already seen this reference in the current call stack, return
+            # the schema as-is to prevent infinite recursion
             return schema
 
-        visited_refs.add(reference_path)
+        # Add this reference to the current call stack
+        new_visited_refs = (*visited_refs, reference_path)
         resolved = resolver.lookup(reference_path)
         schema.update(resolved.contents)
-        return _resolve_schema_references(schema, resolver, visited_refs)
+        return _resolve_schema_references(schema, resolver, new_visited_refs)
 
     if _SchemaKey.properties in schema:
         for k, val in schema[_SchemaKey.properties].items():


Test patch:
diff --git a/tests/singerlib/test_schema.py b/tests/singerlib/test_schema.py
index 89c22d8d..4b3fa28c 100644
--- a/tests/singerlib/test_schema.py
+++ b/tests/singerlib/test_schema.py
@@ -415,6 +415,30 @@ def test_schema_from_dict(pydict, expected):
             },
             id="resolve_schema_references_with_circular_references",
         ),
+        pytest.param(
+            {
+                "type": "object",
+                "properties": {
+                    "min_compute_units": {"$ref": "components#/schemas/ComputeUnit"},
+                    "max_compute_units": {"$ref": "components#/schemas/ComputeUnit"},
+                },
+            },
+            {
+                "components": {
+                    "schemas": {
+                        "ComputeUnit": {"type": "number"},
+                    },
+                },
+            },
+            {
+                "type": "object",
+                "properties": {
+                    "min_compute_units": {"type": "number"},
+                    "max_compute_units": {"type": "number"},
+                },
+            },
+            id="resolve_schema_multiple_properties_with_same_reference",
+        ),
     ],
 )
 def test_resolve_schema_references(schema, refs, expected):