Skip to content

Commit 41aba4d

Browse files
committed
using openapi_extra for Content-Type.
1 parent 4238ae9 commit 41aba4d

File tree

3 files changed

+142
-14
lines changed

3 files changed

+142
-14
lines changed

stac_fastapi/extensions/stac_fastapi/extensions/core/transaction.py

Lines changed: 104 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from typing import List, Optional, Type, Union
55

66
import attr
7-
from fastapi import APIRouter, Body, FastAPI, Header
7+
from fastapi import APIRouter, Body, FastAPI
8+
from pydantic import TypeAdapter
89
from stac_pydantic import Collection, Item, ItemCollection
910
from stac_pydantic.shared import MimeTypes
1011
from starlette.responses import JSONResponse, Response
@@ -51,10 +52,6 @@ class PatchItem(ItemUri):
5152
Union[PartialItem, List[PatchOperation]],
5253
Body(),
5354
] = attr.ib(default=None)
54-
content_type: Annotated[
55-
Optional[str],
56-
Header(),
57-
] = attr.ib(default="application/json")
5855

5956

6057
@attr.s
@@ -72,10 +69,6 @@ class PatchCollection(CollectionUri):
7269
Union[PartialCollection, List[PatchOperation]],
7370
Body(),
7471
] = attr.ib(default=None)
75-
content_type: Annotated[
76-
Optional[str],
77-
Header(),
78-
] = attr.ib(default="application/json")
7972

8073

8174
@attr.s
@@ -170,6 +163,57 @@ def register_patch_item(self):
170163
"model": Item,
171164
}
172165
},
166+
openapi_extra={
167+
"requestBody": {
168+
"content": {
169+
"application/json-patch+json": {
170+
"schema": TypeAdapter(List[PatchOperation]).json_schema()
171+
| {
172+
"examples": [
173+
[
174+
{
175+
"op": "add",
176+
"path": "/properties/foo",
177+
"value": "bar",
178+
},
179+
{
180+
"op": "replace",
181+
"path": "/properties/foo",
182+
"value": "bar",
183+
},
184+
{
185+
"op": "test",
186+
"path": "/properties/foo",
187+
"value": "bar",
188+
},
189+
{
190+
"op": "copy",
191+
"path": "/properties/foo",
192+
"from": "/properties/bar",
193+
},
194+
{
195+
"op": "move",
196+
"path": "/properties/foo",
197+
"from": "/properties/bar",
198+
},
199+
{
200+
"op": "remove",
201+
"path": "/properties/foo",
202+
},
203+
]
204+
]
205+
},
206+
},
207+
"application/merge-patch+json": {
208+
"schema": TypeAdapter(PartialItem).json_schema(),
209+
},
210+
"application/json": {
211+
"schema": TypeAdapter(PartialItem).json_schema(),
212+
},
213+
},
214+
"required": True,
215+
},
216+
},
173217
response_class=self.response_class,
174218
response_model_exclude_unset=True,
175219
response_model_exclude_none=True,
@@ -259,6 +303,57 @@ def register_patch_collection(self):
259303
"model": Collection,
260304
}
261305
},
306+
openapi_extra={
307+
"requestBody": {
308+
"content": {
309+
"application/json-patch+json": {
310+
"schema": TypeAdapter(List[PatchOperation]).json_schema()
311+
| {
312+
"examples": [
313+
[
314+
{
315+
"op": "add",
316+
"path": "/summeries/foo",
317+
"value": "bar",
318+
},
319+
{
320+
"op": "replace",
321+
"path": "/summeries/foo",
322+
"value": "bar",
323+
},
324+
{
325+
"op": "test",
326+
"path": "/summeries/foo",
327+
"value": "bar",
328+
},
329+
{
330+
"op": "copy",
331+
"path": "/summeries/foo",
332+
"from": "/summeries/bar",
333+
},
334+
{
335+
"op": "move",
336+
"path": "/summeries/foo",
337+
"from": "/summeries/bar",
338+
},
339+
{
340+
"op": "remove",
341+
"path": "/summeries/foo",
342+
},
343+
]
344+
]
345+
},
346+
},
347+
"application/merge-patch+json": {
348+
"schema": TypeAdapter(PartialCollection).json_schema(),
349+
},
350+
"application/json": {
351+
"schema": TypeAdapter(PartialCollection).json_schema(),
352+
},
353+
},
354+
"required": True,
355+
},
356+
},
262357
response_class=self.response_class,
263358
response_model_exclude_unset=True,
264359
response_model_exclude_none=True,

stac_fastapi/types/stac_fastapi/types/core.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ def patch_item(
8686
collection_id: str,
8787
item_id: str,
8888
patch: Union[stac.PartialItem, List[stac.PatchOperation]],
89-
content_type: Optional[str] = None,
9089
**kwargs,
9190
) -> Optional[Union[stac.Item, Response]]:
9291
"""Update an item from a collection.
@@ -161,7 +160,6 @@ def patch_collection(
161160
self,
162161
collection_id: str,
163162
patch: Union[stac.PartialItem, List[stac.PatchOperation]],
164-
content_type: Optional[str] = None,
165163
**kwargs,
166164
) -> Optional[Union[stac.Collection, Response]]:
167165
"""Update a collection.
@@ -243,7 +241,6 @@ async def patch_item(
243241
collection_id: str,
244242
item_id: str,
245243
patch: Union[stac.PartialItem, List[stac.PatchOperation]],
246-
content_type: Optional[str] = None,
247244
**kwargs,
248245
) -> Optional[Union[stac.Item, Response]]:
249246
"""Update an item from a collection.
@@ -318,7 +315,6 @@ async def patch_collection(
318315
self,
319316
collection_id: str,
320317
patch: Union[stac.PartialItem, List[stac.PatchOperation]],
321-
content_type: Optional[str] = None,
322318
**kwargs,
323319
) -> Optional[Union[stac.Collection, Response]]:
324320
"""Update a collection.

stac_fastapi/types/stac_fastapi/types/stac.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ class PartialItem(TypedDict, total=False):
119119
class PatchAddReplaceTest(StacBaseModel):
120120
"""Add, Replace or Test Operation."""
121121

122+
model_config = ConfigDict(
123+
json_schema_extra={
124+
"examples": [
125+
{"op": "add", "path": "/properties/foo", "value": "bar"},
126+
{"op": "replace", "path": "/properties/foo", "value": "bar"},
127+
{"op": "test", "path": "/properties/foo", "value": "bar"},
128+
]
129+
}
130+
)
131+
122132
path: str
123133
op: Literal["add", "replace", "test"]
124134
value: Any
@@ -136,14 +146,41 @@ def json_value(self) -> str:
136146
class PatchRemove(StacBaseModel):
137147
"""Remove Operation."""
138148

149+
model_config = ConfigDict(
150+
json_schema_extra={
151+
"examples": [
152+
{
153+
"op": "remove",
154+
"path": "/properties/foo",
155+
}
156+
]
157+
}
158+
)
159+
139160
path: str
140161
op: Literal["remove"]
141162

142163

143164
class PatchMoveCopy(StacBaseModel):
144165
"""Move or Copy Operation."""
145166

146-
model_config = ConfigDict(populate_by_name=True)
167+
model_config = ConfigDict(
168+
populate_by_name=True,
169+
json_schema_extra={
170+
"examples": [
171+
{
172+
"op": "copy",
173+
"path": "/properties/foo",
174+
"from": "/properties/bar",
175+
},
176+
{
177+
"op": "move",
178+
"path": "/properties/foo",
179+
"from": "/properties/bar",
180+
},
181+
]
182+
},
183+
)
147184

148185
path: str
149186
op: Literal["move", "copy"]

0 commit comments

Comments
 (0)