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

Bug: Msgspec constraints ignored in MsgspecDTO #3026

Closed
9128305 opened this issue Jan 25, 2024 Discussed in #3025 · 6 comments · Fixed by #3113
Closed

Bug: Msgspec constraints ignored in MsgspecDTO #3026

9128305 opened this issue Jan 25, 2024 Discussed in #3025 · 6 comments · Fixed by #3113
Assignees
Labels
Bug 🐛 This is something that is not working as expected DTOs This is related to our DTO feature

Comments

@9128305
Copy link

9128305 commented Jan 25, 2024

Discussed in https://github.com/orgs/litestar-org/discussions/3025

Originally posted by 9128305 January 25, 2024
Msgpec constraint here is ignored when sending {"foo": "t"}. What am I doing wrong?

from typing import Annotated

import msgspec
from litestar import post, Litestar
from litestar.dto import MsgspecDTO


class Request(msgspec.Struct):
    foo:  Annotated[str, msgspec.Meta(min_length=3,)]


@post("/example/", dto=MsgspecDTO[Request],sync_to_thread=False)
def example(data: Request) -> Request:
    return data


app = Litestar([example])
@9128305
Copy link
Author

9128305 commented Jan 25, 2024

I checked code and DTOFieldDefinition skips Msgpec.Meta(...) for annotation, so new struct field has only str type

@peterschutt
Copy link
Contributor

Thanks @9128305 - we should be copying these constraints onto the transfer models.

@peterschutt peterschutt added Bug 🐛 This is something that is not working as expected DTOs This is related to our DTO feature labels Jan 25, 2024
@9128305
Copy link
Author

9128305 commented Jan 25, 2024

Should be Annotated type annotation splitted to unwrapped and metadata? Or is viable only for msgspec?

unwrapped, metadata, wrappers = unwrap_annotation(annotation if annotation is not Empty else Any)

@provinzkraut provinzkraut changed the title Why are msgspec constraints ignored? Bug: Msgspec constraints ignored in MsgspecDTO Jan 25, 2024
@peterschutt
Copy link
Contributor

Should be Annotated type annotation splitted to unwrapped and metadata? Or is viable only for msgspec?

unwrapped, metadata, wrappers = unwrap_annotation(annotation if annotation is not Empty else Any)

Sorry, I don't understand the question, can you rephrase or give an example?

Copy link

This issue has been closed in #3113. The change will be included in the upcoming patch release.

Copy link

github-actions bot commented Mar 2, 2024

A fix for this issue has been released in v2.6.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected DTOs This is related to our DTO feature
Projects
Status: Closed
Development

Successfully merging a pull request may close this issue.

3 participants