Skip to content
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

GET APIRequest attributes and converters #733

Closed
vincentsarago opened this issue Jul 15, 2024 · 1 comment · Fixed by #734
Closed

GET APIRequest attributes and converters #733

vincentsarago opened this issue Jul 15, 2024 · 1 comment · Fixed by #734
Labels
bug Something isn't working

Comments

@vincentsarago
Copy link
Member

vincentsarago commented Jul 15, 2024

in #729 and #714 we've tried using dataclass and attrs to define the APIRequest class used as GET Request model (defining QueryParameters)

With #729 we've moved back to attrs and successfully were able to use alias field (like filter-crs, #638)

While this work great, I've discovered that it won't work when an attribute will have a converter:

from typing import Optional
from typing_extensions import Annotated

from stac_fastapi.types.search import APIRequest
import attr

from fastapi import Query


@attr.s
class GetRequest(APIRequest):
    filter_crs: Annotated[
        Optional[int], 
        Query(alias="filter-crs")
    ] = attr.ib(default=None)

GetRequest?

    Init signature:
    GetRequest(
        filter_crs: Annotated[Optional[int], Query(PydanticUndefined)] = None,
    ) -> None
    Docstring:      Generic API Request base class.
    Init docstring: Method generated by attrs for class GetRequest.
    Type:           type
    Subclasses:     



@attr.s
class BadGetRequest(APIRequest):
    filter_crs: Annotated[
        Optional[int], 
        Query(alias="filter-crs")
    ] = attr.ib(default=None, converter=int)

BadGetRequest?

    Init signature: BadGetRequest(filter_crs=None) -> None
    Docstring:      Generic API Request base class.
    Init docstring: Method generated by attrs for class BadGetRequest.
    Type:           type
    Subclasses:     

from the attrs documentation

If a converter’s first argument has a type annotation, that type will appear in the signature for init. A converter will override an explicit type annotation or type argument.

This is also unfortunate because it disable adding openAPI documentation on attributes 😭

from typing import Optional
from typing_extensions import Annotated

from stac_fastapi.types.search import APIRequest, str2list
import attr

from fastapi import Query

@attr.s
class BaseSearchGetRequest(APIRequest):
    """Base arguments for GET Request."""

    collections: Annotated[
        Optional[str],
        Query(
            description="Array of collection Ids to search for items.",
            json_extra_schema={
                "examples":[
                    "collection1,collection2",
                ]
            }
        ),
    ] = attr.ib(
        default=None, converter=str2list
    )

BaseSearchGetRequest?

    Init signature: BaseSearchGetRequest(collections: str = None) -> None
    Docstring:      Base arguments for GET Request.
    Init docstring: Method generated by attrs for class BaseSearchGetRequest.
    Type:           type
    Subclasses:     
    Init signature:


@attr.s
class BaseSearchGetRequest(APIRequest):
    """Base arguments for GET Request."""

    collections: Annotated[
        Optional[str],
        Query(
            description="Array of collection Ids to search for items.",
            json_extra_schema={
                "examples":[
                    "collection1,collection2",
                ]
            }
        ),
    ] = attr.ib(
        default=None
    )

BaseSearchGetRequest?

    Init signature:
    BaseSearchGetRequest(
        collections: Annotated[Optional[str], Query(PydanticUndefined)] = None,
    ) -> None
    Docstring:      Base arguments for GET Request.
    Init docstring: Method generated by attrs for class BaseSearchGetRequest.
    Type:           type
    Subclasses:     
@vincentsarago vincentsarago changed the title GET APIRequest GET APIRequest attributes Jul 15, 2024
@vincentsarago vincentsarago added the bug Something isn't working label Jul 15, 2024
@vincentsarago vincentsarago changed the title GET APIRequest attributes GET APIRequest attributes and converters Jul 15, 2024
@vincentsarago
Copy link
Member Author

vincentsarago commented Jul 15, 2024

Solution

add type annotation in the converter

def collection_converter(
    x: Annotated[
        Optional[str],
        Query(
            description="Array of collection Ids to search for items.",
            json_extra_schema={
                "examples":[
                    "collection1,collection2",
                ]
            }
        ),
    ]
) -> Optional[List]:
    """Convert string to list base on , delimiter."""
    if x:
        return x.split(",")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant