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

Implement TypedPath for WithRejection<TypedPath, _> #2489

Closed
1 task done
gpluscb opened this issue Jan 3, 2024 · 1 comment · Fixed by #2491
Closed
1 task done

Implement TypedPath for WithRejection<TypedPath, _> #2489

gpluscb opened this issue Jan 3, 2024 · 1 comment · Fixed by #2491
Labels
A-axum-extra C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@gpluscb
Copy link

gpluscb commented Jan 3, 2024

  • I have looked for existing issues (including closed) about this

Feature Request

Allow customisation of TypedPath rejections using WithRejection as an alternative to the rejection customisation of the TypedPath macro.

Motivation

Rejections for types implementing TypedPath can be customised already when using the TypedPath macro. However, that requires the desired rejection type is available where the struct implementing TypedPath is located.
This might not always be the case, especially when paths are defined in a different place than the rest of the server. For example, I have an API server crate and an API client crate in a workspace that share some common types in a third crate in the same workspace. To make sure I don't typo urls in the client crate, and to make the interaction between the crates more robust in general, I'd like to put TypedPath structs in the common crate, such that both the client and server crate can make use of them.
I do not have access to the server error type, which I would like to use as the rejection type, in this common crate.
Usually I'd work around this by just using WithRejection in the handlers in the server crate, but this doesn't work in this case because WithRejection does not implement TypedPath.

Proposal

WithRejection<E, R> should implement TypedPath where E: TypedPath. I believe this shouldn't be breaking. There might be subtleties that I'm not familiar with that make this harder.

Alternatives

For the example given above, it is possible to move the rejection type to the common crate and to utilise the custom rejection capabilities of the macro. However, this would pull in types dependencies into the common crate and therefore the API client that are not needed there.
Similarly, a custom rejection type could be added, but this also has the issue of doing work that should be done in the server crate (deciding what rejection responses should look like) in the common crate.
Another workaround could be to implement TypedPath and Display by hand in the common crate and then implement FromRequest in the server crate, but I think orphan rules could rule that option out. Either way it would be very cumbersome and error-prone compared to the macro and WithRejection.

@davidpdrsn
Copy link
Member

Sure! I've submitted #2491

@davidpdrsn davidpdrsn added C-feature-request Category: A feature request, i.e: not implemented / a PR. A-axum-extra labels Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-axum-extra C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants