Add support for $ref URIs containing fragments in OpenAPI definitions and JSON schemas (#1751)

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille 2024-03-19 15:50:49 +01:00 committed by GitHub
parent 0b43b5a343
commit 4d7e33ec26
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 140 additions and 163 deletions

View file

@ -18,6 +18,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import helpers
import sys
import json
import os
@ -67,23 +68,11 @@ class SchemaDirReport:
def add(self, other_report):
self.files += other_report.files
self.errors += other_report.errors
def load_file(path):
if not path.startswith("file://"):
raise Exception(f"Bad ref: {path}")
path = path[len("file://"):]
with open(path, "r") as f:
if path.endswith(".json"):
return json.load(f)
else:
# We have to assume it's YAML because some of the YAML examples
# do not have file extensions.
return yaml.safe_load(f)
def check_example(path, schema, example):
# URI with scheme is necessary to make RefResolver work.
fileurl = "file://" + os.path.abspath(path)
resolver = jsonschema.RefResolver(fileurl, schema, handlers={"file": load_file})
resolver = jsonschema.RefResolver(fileurl, schema, handlers={"file": helpers.load_file_from_uri})
validator = jsonschema.Draft202012Validator(schema, resolver)
validator.validate(example)