Description
I'm trying to use the OpenAPI JSON produced by stac-fastapi to set up the API in Azure API Management. When validating the OpenAPI JSON, I get the following error
Declared path parameter "collection_id" needs to be defined within every operation in the path (missing in "put", "post"), or moved to the path-level parameters object
This can be fixed by manually editing the OpenAPI JSON and adding:
"parameters": [
{
"description": "Collection ID",
"required": true,
"schema": {
"title": "Collection Id",
"type": "string",
"description": "Collection ID"
},
"name": "collection_id",
"in": "path"
}
],
into the definitions for the PUT and POST requests for /collections/{collection_id}/items
.
I've tried to work out how to fix this myself, and have tracked it down to the differences between the code in register_get_item_collection
in app.py
and register_create_item
and register_update_item
in transaction.py
. The former defines a request model:
request_model = create_request_model(
"ItemCollectionURI",
base_model=ItemCollectionUri,
mixins=[get_pagination_model],
)
that specifies the collection_id path parameter. But the latter only specify a request model of Item
which doesn't have that path parameter.
I've tried creating a new request model of:
class ItemWithCollectionPathParam(stac_types.Item):
collection_id: str = attr.ib(default=Path(..., description="Collection ID"))
and using that as the request model as follows:
def register_create_item(self):
"""Register create item endpoint (POST /collections/{collection_id}/items)."""
self.router.add_api_route(
name="Create Item",
path="/collections/{collection_id}/items",
response_model=Item if self.settings.enable_response_models else None,
response_class=self.response_class,
response_model_exclude_unset=True,
response_model_exclude_none=True,
methods=["POST"],
endpoint=self._create_endpoint(self.client.create_item, ItemWithCollectionPathParam),
)
But that doesn't seem to fix the OpenAPI JSON.
I'm happy to put in a PR to fix this, if someone can give me some advice on where/how to fix it.