Skip to content

Commit 4b30de5

Browse files
committed
fix parsing for 'format' as direct schema URI
1 parent bb9e069 commit 4b30de5

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Fixes:
2020
operations to extract applicable media-types. These cases will be ignored, since media-types cannot be inferred
2121
from them. The `WPS` or `OAS` I/O definitions should instead provide the applicable media-types
2222
(relates to `common-workflow-language/cwl-v1.3#52 <https://github.com/common-workflow-language/cwl-v1.3/issues/52>`_).
23+
- Fix ``format`` parsing when trying to infer media-types from various I/O definition representations using a
24+
reference provided as an URI schema from an ontology. Parsing caused the URI to be split, causing an invalid
25+
resolution. If no appropriate media-type is provided, JSON will be used by default, while preserving the submitted
26+
schema URI.
2327

2428
.. _changes_5.7.0:
2529

tests/test_formats.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,21 @@ def test_get_format_media_type_no_extension(test_extension):
156156
fmt = f.get_format(test_extension)
157157
assert fmt == Format(test_extension, extension=None)
158158
assert fmt.extension == ""
159+
assert fmt.schema == ""
160+
161+
162+
@pytest.mark.parametrize(
163+
"test_format",
164+
[
165+
"http://www.opengis.net/def/glossary/term/FeatureCollection",
166+
"https://geojson.org/schema/FeatureCollection.json",
167+
]
168+
)
169+
def test_get_format_media_type_no_extension_with_schema(test_format):
170+
fmt = f.get_format(test_format)
171+
assert fmt.extension == ".json"
172+
assert fmt.mime_type == f.ContentType.APP_JSON
173+
assert fmt.schema == test_format
159174

160175

161176
@pytest.mark.parametrize(

weaver/formats.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,10 @@ def get_format(media_type, default=None):
669669
if not ctype:
670670
return None
671671
ext = get_extension(ctype)
672-
fmt = Format(ctype, extension=ext)
672+
if ctype.startswith("http") and ctype.endswith(ext.strip(".")):
673+
fmt = Format(ContentType.APP_JSON, extension=".json", schema=ctype)
674+
else:
675+
fmt = Format(ctype, extension=ext)
673676
return fmt
674677

675678

@@ -966,14 +969,20 @@ def clean_media_type_format(media_type, suffix_subtype=False, strip_parameters=F
966969
media_type = f"{typ}/{sub}{parts[1]}"
967970
for v in FORMAT_NAMESPACE_DEFINITIONS.values():
968971
if v in media_type:
969-
media_type = media_type.replace(v, "")
970-
break
972+
maybe_type = media_type.replace(v, "").strip("/")
973+
# ignore if URI was partial prefix match, not sufficiently specific
974+
# allow 1 '/' for '<type>/<subtype>', or 0 for an explicit named schema reference
975+
if maybe_type.count("/") < 2:
976+
media_type = maybe_type
977+
break
971978
for v in FORMAT_NAMESPACE_DEFINITIONS:
972979
if media_type.startswith(f"{v}:"):
973-
media_type = media_type.replace(f"{v}:", "")
974-
break
980+
maybe_type = media_type.replace(f"{v}:", "")
981+
if maybe_type.count("/") < 2:
982+
media_type = maybe_type
983+
break
975984
search = True
976-
for _map in [EDAM_MAPPING, OGC_MAPPING, OPENGIS_MAPPING]:
985+
for _map in FORMAT_NAMESPACE_MAPPINGS.values():
977986
if not search:
978987
break
979988
for v in _map.values():

0 commit comments

Comments
 (0)