Code Monkey home page Code Monkey logo

Comments (5)

Goldziher avatar Goldziher commented on May 13, 2024 1

So, the issue is with how dependencies are resolved. For money duplicate keys should be handled by at least deduplication, but perhaps also exception throwing during startup.

How about: deduplication if both key and value are identical. Otherwise, on key collision, raise an exception with a meaningful message?

from litestar.

peterschutt avatar peterschutt commented on May 13, 2024

So the issue is caused by the way that I'm using the dependencies in the example app.

class AbstractBaseRepository(ABC, Generic[DbType, ReturnType]):

    def __init__(
        self,
        limit_offset: LimitOffset | None = None,
        updated_filter: BeforeAfter | None = None,
    ) -> None:
        ...

I have a User repository:

class UserRepository(AbstractBaseRepository[User, UserModel]):
    db_model = User
    return_model = UserModel

And an Items repository which has specific methods that operate on a specific user items.

class ItemRepository(AbstractBaseRepository[Item, ItemModel]):
    db_model = Item
    return_model = ItemModel

    async def get_many_for_user(
        self, user: UserModel, **kwargs: Any
    ) -> list[ItemModel]:
        ...

The key thing is that the ItemRepository methods require a UserModel in their arguments.

So I include the UserRepository and the ItemRepository in the dependencies in the {user_id}/items controller:

async def get_user(user_id: UUID, user_repository: UserRepository) -> UserModel:
    return await user_repository.get_one(user_id)


router_dependencies = {
    "repository": Provide(ItemRepository),
    "user_repository": Provide(UserRepository),
    "user": Provide(get_user),
}

... and that causes it to duplicate the query params in the schema.

from litestar.

peterschutt avatar peterschutt commented on May 13, 2024

This is the MCVE

def test_dependency_schema_generation_for_nested_routes() -> None:

    class BaseDep:
        def __init__(self, query_param: str) -> None:
            ...

    class ADep(BaseDep):
        ...

    class BDep(BaseDep):
        ...

    @get("/test", dependencies={"a": Provide(ADep), "b": Provide(BDep)})
    def handler(a: int, b: int) -> str:
        return "OK"

    app = Starlite(route_handlers=[handler], openapi_config=DEFAULT_OPENAPI_CONFIG)
    open_api_path_item = app.openapi_schema.paths["/test"]
    open_api_parameters = open_api_path_item.get.parameters
    assert len(open_api_parameters) == 1  # fails with 2

from litestar.

peterschutt avatar peterschutt commented on May 13, 2024

This is a clearer example than the one above, just putting it here for clarity.

    def c_dep(other_param: float) -> float:
        ...

    def d_dep(other_param: float) -> float:
        ...

    @get("/test", dependencies={"c": Provide(c_dep), "d": Provide(d_dep)})
    def handler(c: float, d: float) -> str:
        return "OK"

    app = Starlite(route_handlers=[handler], openapi_config=DEFAULT_OPENAPI_CONFIG)
    open_api_path_item = app.openapi_schema.paths["/test"]
    open_api_parameters = open_api_path_item.get.parameters
    assert len(open_api_parameters) == 1

So, where multiple dependencies of the route declare a parameter of the same name.

from litestar.

peterschutt avatar peterschutt commented on May 13, 2024

Closed in #129

from litestar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.