Skip to content

Patch endpoints #744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 53 commits into from
Jun 4, 2025
Merged

Patch endpoints #744

merged 53 commits into from
Jun 4, 2025

Conversation

rhysrevans3
Copy link
Contributor

@rhysrevans3 rhysrevans3 commented Aug 21, 2024

Description:
Adds PATCH endpoints to transaction extension. Adds support for RFC 6902 and RFC 7396. Pivots on header Content-Type value.

Related pull request: stac-api-extensions/transaction#14

PR Checklist:

  • pre-commit hooks pass locally
  • Tests pass (run make test)
  • Documentation has been updated to reflect changes, if applicable, and docs build successfully (run make docs)
  • Changes are added to the CHANGELOG.

@rhysrevans3 rhysrevans3 marked this pull request as ready for review August 22, 2024 08:37
@rhysrevans3
Copy link
Contributor Author

I've had to switch from stac-pydantic Item/Collection to Dict to allow for partial updates. Not sure if this is the best method or if you can switch off pydantic validation. Another option would be having separate models for partial items/collections in stac-pydantic where all attributes.

@vincentsarago
Copy link
Member

I've had to switch from stac-pydantic Item/Collection to Dict to allow for partial updates. Not sure if this is the best method or if you can switch off pydantic validation. Another option would be having separate models for partial items/collections in stac-pydantic where all attributes.

Would a typedDict make more sense?

Adding default for content_type.
@rhysrevans3
Copy link
Contributor Author

rhysrevans3 commented Apr 4, 2025

I've not tested this but this is an example of running a list of patch operations on an item:

from stac_fastapi.types.stac import PatchOperation
from stac_pydantic import Item

def patch_item(item: Item, operations: PatchOperation) -> Item:
    item_dict = item.model_dump()

    for operation in operations:
        path_parts = operation.path.split('/')
 
       if operation.op == "test":
            test_value = item_dict.copy()
            for path_part in path_parts:
                test_value = test_value[path_part]

            assert test_value == operation.value
            continue

        if operation.op == "replace":
            nest = item_dict.copy()
            for path_part in path_parts:
                assert path_part in nest
                nest = nest[path_part]

        update = {}

        if operation.op in ["add", "copy", "replace", "move"]:
            if operation.hasattr("from_"):
                from_parts = operation.from_.split('/')

                value = item_dict.copy()
                for path_part in from_parts:
                    value = value[path_part]

            else:
                value = item.value

            update = value
            for path_part in path_parts.reverse():
                update = {path_part: update}

        if operation.op in ["remove", "move"]:
            if operation.op == "move":
                path_parts = from_parts
 
            last_part = path_parts.pop(-1)

            nest = item_dict
            for path_part in path_parts:
                nest = nest[path_part]

            del nest[last_part]

        return Item.model_validate(item_dict | update)

@vincentsarago
Copy link
Member

vincentsarago commented Apr 18, 2025

FYI: sorry for letting this PR stale. Because it adds breaking changes I'll wait for the next major release to be planned before merging.

@rhysrevans3
Copy link
Contributor Author

@vincentsarago thanks for staying on top of this. Including it in the next major release sounds sensible 😃

@vincentsarago
Copy link
Member

alright it took more than a year but we can finally merge this :-)

@vincentsarago vincentsarago self-requested a review June 3, 2025 08:17
@vincentsarago
Copy link
Member

FYI I cannot merge this as is because there is an issue with the pydantic model and openapi schema.

I'll try to fix this 🙏

@vincentsarago vincentsarago merged commit 423b587 into stac-utils:main Jun 4, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants