Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

## Unreleased

- Remove restriction on valid media types for links.

## 3.3.1 (2025-06-01)

- Add back `SearchDatetime` in `stac_pydantic.api.search` to avoid breaking change
Expand Down
4 changes: 2 additions & 2 deletions stac_pydantic/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pydantic import ConfigDict, Field, RootModel

from stac_pydantic.shared import MimeTypes, StacBaseModel
from stac_pydantic.shared import StacBaseModel
from stac_pydantic.utils import AutoValueEnum


Expand All @@ -15,7 +15,7 @@ class Link(StacBaseModel):

href: str = Field(..., alias="href", min_length=1)
rel: str = Field(..., alias="rel", min_length=1)
type: Optional[MimeTypes] = None
type: Optional[str] = None
title: Optional[str] = None

# Label extension
Expand Down
7 changes: 6 additions & 1 deletion tests/api/test_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from stac_pydantic.api import ItemCollection
from stac_pydantic.api.links import Link, Links, PaginationLink, Relations, SearchLink
from stac_pydantic.api.version import STAC_API_VERSION
from stac_pydantic.links import MimeTypes
from stac_pydantic.shared import MimeTypes
from stac_pydantic.version import STAC_VERSION

from ..conftest import request
Expand Down Expand Up @@ -80,3 +80,8 @@ def test_resolve_pagination_link():
for link in links.link_iterator():
if isinstance(link, PaginationLink):
assert link.href == "http://base_url.com/next/page"


def test_link_types():
for type_ in (MimeTypes.xml, "some random string", None):
Link(href="/hello/world", type=type_, rel="test")
Comment on lines +86 to +87
Copy link
Contributor

@fmigneault fmigneault Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this test is inappropriate, or at least should not "encourage" it with explicitly validating erroneous values.

Ideally, href should have a protocol, specifically file:// if local files makes any sense here.
Though, that has other implications in STAC (eg: stac-utils/pystac#1347), so I would let them decide about this.

Media-type should at least validate that [\w-]+/[\w-.]+.* are handled as bare minimum (ie: RFC6838 section 4.2).
It could be more strict to consider only valid RFC2046 types if desired, and could define the specific ; and parameters regexes if really invested in validating the specific pattern, but they have to minimally be permitted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicitly validating erroneous values

I don't think there's anything technically "erroneous" about some random string ... while the provided RFC6838 link provides a syntax requirement for "registered media types", we're explicitly allowing non-registered media types in this library, so the syntax requirement doesn't apply.

I'd be ok w/ warning if the media type doesn't fit some sort of expectation (either in our list, or that regex, or whatever), but I think an error is too strong.

W.r.t. the href, there's a lot of "absolute file system" hrefs in the wild, so I'm ok with including it here. If we wanted to be "correct" we could update it to ./hello/world.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't application/x-[something] or application/vdn.[something] be required for unregistered types? Won't most libraries break if there is not at least a <type>/<subtype> split?

Copy link
Member

@gadomski gadomski Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? I guess I'm just curious what we're trying to gain by validating others' media types. I generally favor "permissive reads, strict writes" for STAC tooling

For this library, I think that means warning on "surprising" (not erroring) media types and providing string constants for "correct" ones (which we do).

Regarding the split question, I've seen a lot of libraries do direct string matching (not splitting) , which means subtypes or profiles (like cloud-optimized) break matching anyways.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine if that's the decision. My understanding was that it was the opposite with strict validation given that the (too)specific set of MediaTypes enum was used instead of str from the start. As mentioned, I think the bare minimum would be [\w-]+/[\w-.]+.* that would allow most flexibility while at least avoiding basic mistakes of malformed media-types. Note that the pattern includes .* specifically to allow the profile=cloud-optimized or other similar parameters.