Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
0cddff7
Adding patch endpoints.
rhysrevans3 Aug 20, 2024
14d18f9
Merge branch 'main' of github.com:stac-utils/stac-fastapi into patch_…
rhysrevans3 Aug 20, 2024
0c5de64
Adding annotated from main.
rhysrevans3 Aug 20, 2024
bf2ddbb
Fixing and adding tests.
rhysrevans3 Aug 21, 2024
632d5a5
Updating changelog.
rhysrevans3 Aug 21, 2024
5f2b4fa
Fixing ruff errors.
rhysrevans3 Aug 21, 2024
010e2cb
Ruff format.
rhysrevans3 Aug 21, 2024
0ccded0
Switching to List for python 3.8.
rhysrevans3 Aug 21, 2024
1b46754
Updating docs make file.
rhysrevans3 Aug 21, 2024
79c769c
Switching from Item/Collection to Dict to allow partial updates.
rhysrevans3 Aug 23, 2024
68a65a0
Ruff format fix.
rhysrevans3 Aug 23, 2024
88b40d4
Fixing broken tests.
rhysrevans3 Aug 23, 2024
b7bcbd5
Adding missing asyncs for patchs.
rhysrevans3 Aug 23, 2024
cfc31c6
Moving request to kwargs for patch item and collection.
rhysrevans3 Aug 23, 2024
81dbcad
Switching to TypedDict.
rhysrevans3 Aug 28, 2024
f053f07
Merge branch 'main' of github.com:stac-utils/stac-fastapi into patch_…
rhysrevans3 Aug 28, 2024
bce099c
Adding hearder parameter to the input models.
rhysrevans3 Aug 28, 2024
336df70
Removing print statement.
rhysrevans3 Aug 28, 2024
36b7167
Removing basemodel from patch types.
rhysrevans3 Aug 28, 2024
0ecf3e5
Fixing imports.
rhysrevans3 Aug 28, 2024
47a0b48
Moving models to correct locations.
rhysrevans3 Aug 28, 2024
13a2377
Switching from attrs to basemodel for patch operations.
rhysrevans3 Aug 28, 2024
7e59d13
Switching to stac.PartialItem etc.
rhysrevans3 Aug 28, 2024
9d011eb
Updating PatchMoveCopy model.
rhysrevans3 Sep 3, 2024
ae6bb94
Merge branch 'main' of github.com:stac-utils/stac-fastapi into patch_…
rhysrevans3 Sep 4, 2024
e325cb2
Updating type for 3.8.
rhysrevans3 Sep 4, 2024
fefd493
Switching to StacBaseModels for patch operations.
rhysrevans3 Sep 18, 2024
7611903
Merge branch 'main' of github.com:stac-utils/stac-fastapi into patch_…
rhysrevans3 Mar 25, 2025
5319c3b
pre-commits.
rhysrevans3 Mar 25, 2025
a434c25
Add json dump of operation value.
rhysrevans3 Mar 26, 2025
8863923
remove computed field decorator.
rhysrevans3 Mar 26, 2025
25ff3e1
Adding default "not implemented" for JSON Patch.
rhysrevans3 Apr 2, 2025
3d49658
pre-commit.
rhysrevans3 Apr 2, 2025
0254336
Add default raise not implement to collection JSON Patch.
rhysrevans3 Apr 2, 2025
1198000
removing json merge and patch.
rhysrevans3 Apr 2, 2025
3bdfcc7
content_type None default.
rhysrevans3 Apr 3, 2025
0ddd072
Fixing test.
rhysrevans3 Apr 3, 2025
4238ae9
back to PartialItem & PatchOperation
rhysrevans3 Apr 3, 2025
41aba4d
using openapi_extra for Content-Type.
rhysrevans3 Apr 3, 2025
46f4edf
PartialCollection not PartialItem.
rhysrevans3 Apr 3, 2025
c1003d4
Adding merge to operations.
rhysrevans3 Apr 3, 2025
53bfe47
Adding example code for patch.
rhysrevans3 Apr 3, 2025
2ce9e54
Adding default None to partials.
rhysrevans3 Apr 3, 2025
178ea59
patch not update for backend.
rhysrevans3 Apr 3, 2025
be4962f
patch_collection not patch_item.
rhysrevans3 Apr 3, 2025
4189df8
Add tests for merge to operations.
rhysrevans3 Apr 3, 2025
7f9ff4b
Switch to model_json_schema for patch schema.
rhysrevans3 Apr 22, 2025
ea8ef04
Merge branch 'main' of github.com:stac-utils/stac-fastapi into patch_…
rhysrevans3 Apr 22, 2025
bd4083f
update from main
vincentsarago Jun 2, 2025
6c0f183
update changelog
vincentsarago Jun 2, 2025
cb10173
update changelog
vincentsarago Jun 2, 2025
057c53c
Removing numberMatched & numberReturned.
rhysrevans3 Jun 3, 2025
6f3a213
fix openapi schemas
vincentsarago Jun 3, 2025
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
15 changes: 6 additions & 9 deletions stac_fastapi/types/stac_fastapi/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
from stac_fastapi.types.requests import get_base_url
from stac_fastapi.types.rfc3339 import DateTimeType
from stac_fastapi.types.search import BaseSearchPostRequest
from stac_fastapi.types.transaction import (
PartialCollection,
PartialItem,
PatchOperation,
)
from stac_fastapi.types.transaction import PartialCollection, PartialItem, PatchOperation

__all__ = [
"NumType",
Expand Down Expand Up @@ -92,6 +88,7 @@ def patch_item(
collection_id: str,
item_id: str,
patch: Union[PartialItem, List[PatchOperation]],
content_type: Optional[str],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
content_type: Optional[str],
content_type: Optional[str] = None,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should the default be None or application/json? I've set it to None for now.

Copy link
Member

Choose a reason for hiding this comment

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

I think None is fine.

we could also totally remove it from the arg list and use request: Request

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was my attempt to make it configurable in the api.html page but it doesn't seem like it actually sets the Content-Type so I'd be happy to go back to using request if that's better for stac-fastapi.

Screenshot 2025-04-03 at 09 27 50

Copy link
Member

Choose a reason for hiding this comment

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

fastapi/fastapi#7786 (comment)

I think you can achieve this with openapi customization

def register_patch_item(self):
    """Register patch item endpoint (PATCH
    /collections/{collection_id}/items/{item_id})."""
    self.router.add_api_route(
        name="Patch Item",
        path="/collections/{collection_id}/items/{item_id}",
        response_model=Item if self.settings.enable_response_models else None,
        responses={
            200: {
                "content": {
                    MimeTypes.geojson.value: {},
                },
                "model": Item,
            }
        },
        openapi_extra={
            "requestBody": {
                "content": {
                    "application/json-patch+json": {
                        "schema": ...,
                    },
                    "application/merge-patch+json": {
                        "schema": ...,
                    },
                    "application/json": {
                        "schema": ...,
                    }
                },
                "required": True,
            },
        },
        response_class=self.response_class,
        response_model_exclude_unset=True,
        response_model_exclude_none=True,
        methods=["PATCH"],
        endpoint=create_async_endpoint(
            self.client.patch_item,
            PatchItem,
        ),
    )


    

def register_patch_collection(self):
    """Register patch collection endpoint (PATCH /collections/{collection_id})."""
    self.router.add_api_route(
        name="Patch Collection",
        path="/collections/{collection_id}",
        response_model=Collection if self.settings.enable_response_models else None,
        responses={
            200: {
                "content": {
                    MimeTypes.geojson.value: {},
                },
                "model": Collection,
            }
        },
        openapi_extra={
            "requestBody": {
                "content": {
                    "application/json-patch+json": {
                        "schema": ...,
                    },
                    "application/merge-patch+json": {
                        "schema": ...,
                    },
                    "application/json": {
                        "schema": ...,
                    }
                },
                "required": True,
            },
        },
        response_class=self.response_class,
        response_model_exclude_unset=True,
        response_model_exclude_none=True,
        methods=["PATCH"],
        endpoint=create_async_endpoint(
            self.client.patch_collection,
            PatchCollection,
        ),
    )

I'll let you figure out the schemas 😅

I think if would be fine to advertise support for the 3 content-type application/json-patch+json, application/merge-patch+jsonandapplication/jsonand then let the application raiseNotImplementedErrorwithin the client method... or we make this configurable... or we just document how people could add themerge` content-type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks that's just what I was looking! I've added openapi_extra for patch item and collection and removed the content_type parameter.

**kwargs,
) -> Optional[Union[stac.Item, Response]]:
"""Update an item from a collection.
Expand All @@ -106,7 +103,6 @@ def patch_item(
Returns:
The patched item.
"""
content_type = kwargs["request"].headers.get("content-type", "application/json")
if isinstance(patch, list) and content_type == "application/json-patch+json":
return self.json_patch_item(
collection_id,
Expand Down Expand Up @@ -233,6 +229,7 @@ def patch_collection(
self,
collection_id: str,
patch: Union[PartialCollection, List[PatchOperation]],
content_type: Optional[str],
**kwargs,
) -> Optional[Union[stac.Collection, Response]]:
"""Update a collection.
Expand All @@ -246,7 +243,6 @@ def patch_collection(
Returns:
The patched collection.
"""
content_type = kwargs["request"].headers.get("content-type", "application/json")
if isinstance(patch, list) and content_type == "application/json-patch+json":
return self.json_patch_collection(collection_id, patch, **kwargs)

Expand Down Expand Up @@ -371,6 +367,7 @@ async def patch_item(
collection_id: str,
item_id: str,
patch: Union[PartialItem, List[PatchOperation]],
content_type: Optional[str],
**kwargs,
) -> Optional[Union[stac.Item, Response]]:
"""Update an item from a collection.
Expand All @@ -385,7 +382,7 @@ async def patch_item(
Returns:
The patched item.
"""
content_type = kwargs["request"].headers.get("content-type", "application/json")
print("content_type", content_type)
if isinstance(patch, list) and content_type == "application/json-patch+json":
return await self.json_patch_item(
collection_id,
Expand Down Expand Up @@ -512,6 +509,7 @@ async def patch_collection(
self,
collection_id: str,
patch: Union[PartialCollection, List[PatchOperation]],
content_type: Optional[str],
**kwargs,
) -> Optional[Union[stac.Collection, Response]]:
"""Update a collection.
Expand All @@ -525,7 +523,6 @@ async def patch_collection(
Returns:
The patched collection.
"""
content_type = kwargs["request"].headers.get("content-type", "application/json")
if isinstance(patch, list) and content_type == "application/json-patch+json":
partialCollectionValidator = TypeAdapter(PartialCollection)

Expand Down
10 changes: 9 additions & 1 deletion stac_fastapi/types/stac_fastapi/types/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Dict, List, Literal, Optional, Union

import attr
from fastapi import Body
from fastapi import Body, Header
from pydantic import BaseModel
from stac_pydantic import Collection, Item, ItemCollection
from stac_pydantic.shared import BBox
Expand Down Expand Up @@ -106,6 +106,10 @@ class PatchItem(ItemUri):
Union[PartialItem, List[PatchOperation]],
Body(),
] = attr.ib(default=None)
content_type: Annotated[
Optional[str],
Header(),
] = attr.ib(default="application/json")


@attr.s
Expand All @@ -123,3 +127,7 @@ class PatchCollection(CollectionUri):
Union[PartialCollection, List[PatchOperation]],
Body(),
] = attr.ib(default=None)
content_type: Annotated[
Optional[str],
Header(),
] = attr.ib(default="application/json")