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

Supporting sqlx Json<T> #952

Open
ElysaSrc opened this issue May 27, 2024 · 5 comments
Open

Supporting sqlx Json<T> #952

ElysaSrc opened this issue May 27, 2024 · 5 comments
Labels
enhancement New feature or request investigate Futher investigation needed before other action

Comments

@ElysaSrc
Copy link

I think it'd be nice to support (behind a feature flag) the Json<T> because, currently the typing is lost on this element on the OpenAPI generated file.

@juhaku
Copy link
Owner

juhaku commented Sep 26, 2024

@ElysaSrc Could you elaborate bit more how you would like the support to be? How it should behave?

@ElysaSrc
Copy link
Author

ElysaSrc commented Sep 26, 2024

@ElysaSrc Could you elaborate bit more how you would like the support to be? How it should behave?

Currently I must annotate all Json fields of my struct that is requested though sqlx with the inner type, I was wondering if maybe it could be inferred so when we have a json it directly infer a T type in the openapi scheme ?

@juhaku
Copy link
Owner

juhaku commented Oct 1, 2024

Okay, so it should infer the inner type. This needs some looking into to assess what can be done there.

@juhaku juhaku added enhancement New feature or request investigate Futher investigation needed before other action labels Oct 1, 2024
@juhaku
Copy link
Owner

juhaku commented Oct 10, 2024

@ElysaSrc
All I get is:

error[E0412]: cannot find type `Json` in module `sqlx::types`
   --> utoipa/src/lib.rs:650:45
    |
650 | impl<T: ToSchema> ToSchema for sqlx::types::Json<T> where sqlx::types::Json<T>: PartialSchema {}
    |                                             ^^^^ not found in `sqlx::types`

error[E0412]: cannot find type `Json` in module `sqlx::types`
   --> utoipa/src/lib.rs:650:72
    |
650 | impl<T: ToSchema> ToSchema for sqlx::types::Json<T> where sqlx::types::Json<T>: PartialSchema {}
    |                                                                        ^^^^ not found in `sqlx::types`

error[E0412]: cannot find type `Json` in module `sqlx::types`
   --> utoipa/src/lib.rs:745:50
    |
745 | impl<T: ToSchema> PartialSchema for sqlx::types::Json<T> {
    |                                                  ^^^^ not found in `sqlx::types`

With crates dependency:

sqlx = { version = "0.8", default-features = false, features = [
    "json",
], optional = true }

Somehow it does not "find" the type even though it is there and LSP is able to access it anyhow. Does it need some sort of DB to be there before it can actually be used, Should not since the only feature flag that it seemingly requires is the "json" even though it is not explicitly mentioned.

This is quite weird and IMO should not happen, nevertheless it hinders the implementation quite a bit.

Another approach is to implement generic support for anything that seems like a Json<T> so that utoipa would just treat it as a container type and return the schema of wrapped T as is by baking this support functionality to utoipa-gen. Though I am not sure whether this would have some unwanted side effects with other Json types.

@ElysaSrc
Copy link
Author

Somehow it does not "find" the type even though it is there and LSP is able to access it anyhow. Does it need some sort of DB to be there before it can actually be used, Should not since the only feature flag that it seemingly requires is the "json" even though it is not explicitly mentioned.

When using sqlx, requests are checked at compile time so it requieres either a cache or a live database, but my guess here is it should not be an issue with just parsing the type ?

This is quite weird and IMO should not happen, nevertheless it hinders the implementation quite a bit.

Another approach is to implement generic support for anything that seems like a Json<T> so that utoipa would just treat it as a container type and return the schema of wrapped T as is by baking this support functionality to utoipa-gen. Though I am not sure whether this would have some unwanted side effects with other Json types.

I'm not aware enough of utoipa internals to have an opinion here, I'd say that the less it messes up with current usages the better it is. Maybe add an optional feature flag for such behavior ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request investigate Futher investigation needed before other action
Projects
Status: No status
Development

No branches or pull requests

2 participants