Skip to content

Commit

Permalink
Fix calls to PathOrUrl.resolve_ref and OAS 30 downcompiling
Browse files Browse the repository at this point in the history
  • Loading branch information
avillar committed Jun 6, 2024
1 parent 6509bb2 commit 4f88cb1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ogc/bblocks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def _resolve_bblock_deps(self, bblock: BuildingBlock) -> set[str]:
elif not is_url(ref):
if source_fn.is_path:
# Check if target path in local bblock schemas
rel_ref = str(os.path.relpath(source_fn.parent.resolve_ref(ref).resolve()))
rel_ref = str(os.path.relpath(source_fn.resolve_ref(ref).resolve()))
if not Path(rel_ref).is_file():
raise ValueError(f"Invalid reference to {rel_ref}"
f" from {bblock.identifier} ({source_fn}) - target file does not exist"
Expand Down
21 changes: 12 additions & 9 deletions ogc/bblocks/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,18 @@ def do_postprocess(bblock: BuildingBlock, light: bool = False) -> bool:
dump_yaml(openapi_resolved, building_block.output_openapi)
print(f" - {os.path.relpath(building_block.output_openapi)}", file=sys.stderr)

try:
if openapi_resolved.get('openapi', '').startswith('3.1'):
print(f"Downcompiling OpenAPI document to 3.0 for {building_block.identifier}", file=sys.stderr)
oas30_doc_fn = building_block.output_openapi_30
oas30_doc = oas31_to_oas30(openapi_resolved,
PathOrUrl(oas30_doc_fn).with_base_url(base_url),
bbr)
dump_yaml(oas30_doc, oas30_doc_fn)
print(f" - {os.path.relpath(oas30_doc_fn)}", file=sys.stderr)
except Exception as e:
print(f"WARNING: {type(e).__name__} while downcompiling OpenAPI to 3.0:", e)

if building_block.ontology.exists:
building_block.metadata.pop('ontology', None)
if building_block.ontology.is_path and building_block.ontology_graph:
Expand All @@ -286,15 +298,6 @@ def do_postprocess(bblock: BuildingBlock, light: bool = False) -> bool:
elif building_block.ontology.is_url:
building_block.metadata['ontology'] = building_block.ontology.value

if openapi_resolved.get('openapi', '').startswith('3.1'):
print(f"Downcompiling OpenAPI document to 3.0 for {building_block.identifier}", file=sys.stderr)
oas30_doc_fn = building_block.output_openapi_30
oas30_doc = oas31_to_oas30(openapi_resolved,
PathOrUrl(oas30_doc_fn).with_base_url(base_url),
bbr)
dump_yaml(oas30_doc, oas30_doc_fn)
print(f" - {os.path.relpath(oas30_doc_fn)}", file=sys.stderr)

child_bblocks.append(building_block)

if not steps or 'jsonld' in steps:
Expand Down
6 changes: 3 additions & 3 deletions ogc/bblocks/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ def resolve_schema_reference(ref: str,
target_bblock_id = bblocks_register.local_bblock_files[check_ref]
if not target_bblock_id:
if from_document.is_url:
return f"{from_document.parent.resolve_ref(ref).url}{fragment}"
return f"{from_document.resolve_ref(ref).url}{fragment}"
elif base_url:
# Return the URL to the $ref
return f"{base_url}{os.path.relpath(str(from_document.parent.resolve_ref(ref).value))}{fragment}"
return f"{base_url}{os.path.relpath(str(from_document.resolve_ref(ref).value))}{fragment}"
else:
# Relativize from annotated schema path
# TODO: OpenAPI?
return os.path.relpath(from_document.parent.resolve_ref(ref).resolve(),
return os.path.relpath(from_document.resolve_ref(ref).resolve(),
from_bblock.annotated_schema.parent) + fragment
else:
# URL -> search in both local and imported bblock schemas
Expand Down
2 changes: 1 addition & 1 deletion ogc/bblocks/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ def validate_test_resources(bblock: BuildingBlock,
schema_uri = (f"{bblock.schema.parent.joinpath(path).with_name(random_fn).as_uri()}"
f"#{fragment}")
else:
schema_uri = bblock.schema.parent.resolve_ref(schema_ref).with_name(random_fn).as_uri()
schema_uri = bblock.schema.resolve_ref(schema_ref).with_name(random_fn).as_uri()
snippet_schema = {'$ref': schema_ref}
snippet_schema_validator = get_json_validator(snippet_schema,
schema_uri,
Expand Down

0 comments on commit 4f88cb1

Please sign in to comment.