3737}
3838
3939
40- @pytest .mark .parametrize ("check_passes" , (True , False ))
41- @pytest .mark .parametrize ("casename" , ("case1" , "case2" ))
42- def test_remote_ref_resolution_simple_case (
43- run_line , check_passes , casename , tmp_path , monkeypatch
44- ):
40+ @pytest .fixture (autouse = True )
41+ def _mock_schema_cache_dir (monkeypatch , tmp_path ):
4542 def _fake_compute_default_cache_dir (self ):
4643 return str (tmp_path )
4744
@@ -51,6 +48,10 @@ def _fake_compute_default_cache_dir(self):
5148 _fake_compute_default_cache_dir ,
5249 )
5350
51+
52+ @pytest .mark .parametrize ("check_passes" , (True , False ))
53+ @pytest .mark .parametrize ("casename" , ("case1" , "case2" ))
54+ def test_remote_ref_resolution_simple_case (run_line , check_passes , casename , tmp_path ):
5455 main_schema_loc = "https://example.com/main.json"
5556 responses .add ("GET" , main_schema_loc , json = CASES [casename ]["main_schema" ])
5657 for name , subschema in CASES [casename ]["other_schemas" ].items ():
@@ -74,3 +75,69 @@ def _fake_compute_default_cache_dir(self):
7475 assert result .exit_code == 0 , output
7576 else :
7677 assert result .exit_code == 1 , output
78+
79+
80+ # this test ensures that `$id` is preferred for the base URI over
81+ # the retrieval URI
82+ @pytest .mark .parametrize ("check_passes" , (True , False ))
83+ def test_ref_resolution_prefers_id_over_retrieval_uri (run_line , tmp_path , check_passes ):
84+ main_schema = {
85+ "$id" : "https://example.org/schemas/main.json" ,
86+ "$schema" : "http://json-schema.org/draft-07/schema" ,
87+ "properties" : {
88+ "title" : {"$ref" : "./title_schema.json" },
89+ },
90+ "additionalProperties" : False ,
91+ }
92+ title_schema = {"type" : "string" }
93+
94+ retrieval_uri = "https://example.org/alternate-path-retrieval-only/schemas/main"
95+ responses .add ("GET" , retrieval_uri , json = main_schema )
96+ responses .add (
97+ "GET" , "https://example.org/schemas/title_schema.json" , json = title_schema
98+ )
99+
100+ instance_path = tmp_path / "instance.json"
101+ instance_path .write_text (json .dumps ({"title" : "doc one" if check_passes else 2 }))
102+
103+ result = run_line (
104+ ["check-jsonschema" , "--schemafile" , retrieval_uri , str (instance_path )]
105+ )
106+ output = f"\n stdout:\n { result .stdout } \n \n stderr:\n { result .stderr } "
107+ if check_passes :
108+ assert result .exit_code == 0 , output
109+ else :
110+ assert result .exit_code == 1 , output
111+
112+
113+ @pytest .mark .parametrize ("check_passes" , (True , False ))
114+ def test_ref_resolution_does_not_callout_for_absolute_ref_to_retrieval_uri (
115+ run_line , tmp_path , check_passes
116+ ):
117+ retrieval_uri = "https://example.net/schemas/main"
118+
119+ main_schema = {
120+ "$id" : "https://example.net/schemas/some-uri-which-will-never-be-used/main.json" ,
121+ "$schema" : "http://json-schema.org/draft-07/schema" ,
122+ "$defs" : {"title" : {"type" : "string" }},
123+ "properties" : {
124+ "title" : {"$ref" : f"{ retrieval_uri } #/$defs/title" },
125+ },
126+ "additionalProperties" : False ,
127+ }
128+
129+ # exactly one GET to the retrieval URI will work
130+ responses .add ("GET" , retrieval_uri , json = main_schema )
131+ responses .add ("GET" , retrieval_uri , json = {"error" : "permafrost melted" }, status = 500 )
132+
133+ instance_path = tmp_path / "instance.json"
134+ instance_path .write_text (json .dumps ({"title" : "doc one" if check_passes else 2 }))
135+
136+ result = run_line (
137+ ["check-jsonschema" , "--schemafile" , retrieval_uri , str (instance_path )]
138+ )
139+ output = f"\n stdout:\n { result .stdout } \n \n stderr:\n { result .stderr } "
140+ if check_passes :
141+ assert result .exit_code == 0 , output
142+ else :
143+ assert result .exit_code == 1 , output
0 commit comments