Repository: datacontract/datacontract-cli

Issue:
When using $ref to definitions with file paths and definitions objects, json paths aren't respected.
I expected to be able to use `$ref` for a data model's fields from another file, however, this doesn't seem to work. I created a test to show the desired / expected behavior, and what the current behavior seems to be.

```py
def test_resolve_data_contract_relative_refrence():
    with tempfile.TemporaryDirectory() as temp_dir:
        # create temp file with content
        with open(f"{temp_dir}/order.yaml", "w") as temp_file:
            temp_file.write("""
definitions:
  order_id:
    title: order id
    type: text
    examples:
      - O1234
    pii: True
    classification: restricted
    tags:
      - policy
            """)
            temp_file.flush()
            print(temp_file.name)

        datacontract = resolve_data_contract(
            data_contract_str=f"""
        dataContractSpecification: 1.1.0
        id: my-id
        info:
          title: My Title
          version: 1.0.0
        models:
          orders:
            fields:
              order_id:
                $ref: "file://{temp_dir}/order.yaml#/definitions/order_id"
        """,
            inline_definitions=True,
        )
        assert datacontract.models["orders"].fields["order_id"].type == "text"
```

Discovered this when I was trying to figure out how to use file and http refs in
#385

Changing the refed file select down for us yields the output to work as expected, eg:

```py
def test_resolve_data_contract_relative_refrence_ignores_spec():
    with tempfile.TemporaryDirectory() as temp_dir:
        # create temp file with content
        with open(f"{temp_dir}/order.yaml", "w") as temp_file:
            temp_file.write("""
title: order id
type: text
examples:
  - O1234
pii: True
classification: restricted
tags:
  - policy
            """)
            temp_file.flush()
            print(temp_file.name)

        datacontract = resolve_data_contract(
            data_contract_str=f"""
        dataContractSpecification: 1.1.0
        id: my-id
        info:
          title: My Title
          version: 1.0.0
        models:
          orders:
            fields:
              order_id:
                $ref: "file://{temp_dir}/order.yaml#/definitions/order_id"
        """,
            inline_definitions=True,
        )
        # THIS Passes!
        assert datacontract.models["orders"].fields["order_id"].type == "text"
```

Hints from issue comments:

Solution patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c66add6..bbf4941 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 - Unicode Encode Error when exporting data contract YAML to HTML
   (#652)
+- Incorrectly parsing $ref values in definitions (#664)
 
 ## [0.10.22] - 2025-02-20
 
diff --git a/datacontract/lint/resolve.py b/datacontract/lint/resolve.py
index ab95a3b..78eb5f1 100644
--- a/datacontract/lint/resolve.py
+++ b/datacontract/lint/resolve.py
@@ -125,11 +125,24 @@ def _resolve_definition_ref(ref, spec) -> Definition:
         path = path.replace("file://", "")
         definition_str = _fetch_file(path)
         definition_dict = _to_yaml(definition_str)
+        if definition_path:
+            path_parts = [part for part in definition_path.split("/") if part != ""]
+            for path_part in path_parts:
+                definition_dict = definition_dict.get(path_part, None)
+                if not definition_dict:
+                    raise DataContractException(
+                        type="lint",
+                        result="failed",
+                        name="Check that data contract YAML is valid",
+                        reason=f"Cannot resolve definition {definition_path}, {path_part} not found",
+                        engine="datacontract",
+                    )
+        # this assumes that definitions_dict is a definitions dict, however,
+        # all we know is that it is a file!
         definition = Definition(**definition_dict)
-        if definition_path is not None:
-            return _find_by_path_in_definition(definition_path, definition)
-        else:
-            return definition
+        # if definition_path is not None:
+        #     definition = _find_by_path_in_definition(definition_path, definition)
+        return definition
     elif ref.startswith("#"):
         logging.info(f"Resolving definition local path {path}")
 


Test patch:
diff --git a/tests/test_resolve.py b/tests/test_resolve.py
index 56c1f9e..5d91a8e 100644
--- a/tests/test_resolve.py
+++ b/tests/test_resolve.py
@@ -169,3 +169,40 @@ def test_resolve_data_contract_complex_definition_file():
             inline_definitions=True,
         )
         assert datacontract.models["orders"].fields["order_id"].type == "int"
+
+
+def test_resolve_data_contract_relative_refrence():
+    with tempfile.TemporaryDirectory() as temp_dir:
+        # create temp file with content
+        with open(f"{temp_dir}/order.yaml", "w") as temp_file:
+            temp_file.write("""
+            definitions:
+              order_id:
+                title: order id
+                type: text
+                examples:
+                  - O1234
+                pii: True
+                classification: restricted
+                tags:
+                  - policy
+            """)
+            temp_file.flush()
+            print(temp_file.name)
+
+        datacontract = resolve_data_contract(
+            data_contract_str=f"""
+        dataContractSpecification: 1.1.0
+        id: my-id
+        info:
+          title: My Title
+          version: 1.0.0
+        models:
+          orders:
+            fields:
+              order_id:
+                $ref: "file://{temp_dir}/order.yaml#/definitions/order_id"
+        """,
+            inline_definitions=True,
+        )
+        assert datacontract.models["orders"].fields["order_id"].type == "text"