You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am facing a problem when I define some helper types-wrappers, like axum::Json<T>:
structEncodeHex<T>(T);
where T might be any type, not implementing ToSchema. I then define a custom impl Serialize and that's it. I would like it to be presented in OpenAPI as just EncodeHex container, without any notion of the generic argument. For example: "EncodeHex - generic container for some value, encoded as a hex."
Problems:
I could not derive ToSchema for this type, as it would require T: ToSchema, which breaks things for me.
I could implement ToSchema myself, but then the problem appears in places where I refer to EncodeHex<T>:
#[derive(ToSchema,Serialize)]structTransaction{payload:EncodeHex<SomeBinaryData>// ^^^^^^^^^^^^^^ the trait bound `SomeBinaryData: ToSchema` is not satisfied}
I could workaround that by adding #[schema(schema_with = EncodeHex::<()>::schema)], but that doesn't work in case of newtypes (i.e. tuple structs) (and the schema isn't automatically recursively included in the global set):
Alternative: put the actual representation value inside of the container instead of T, e.g. EncodeHex(String). Downsides: isn't lazy; loses some type safety; problematic when e.g. T needs to be serialized and it could fail, thus populating the rest of the codebase with the need to handle possible errors. If it is T, the error could be handled when everything is serialized at once, later.
Is there a way to achieve what I want in a clean way?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! Fantastic project!
I am facing a problem when I define some helper types-wrappers, like
axum::Json<T>
:where
T
might be any type, not implementingToSchema
. I then define a customimpl Serialize
and that's it. I would like it to be presented in OpenAPI as justEncodeHex
container, without any notion of the generic argument. For example: "EncodeHex
- generic container for some value, encoded as a hex."Problems:
ToSchema
for this type, as it would requireT: ToSchema
, which breaks things for me.ToSchema
myself, but then the problem appears in places where I refer toEncodeHex<T>
:#[schema(schema_with = EncodeHex::<()>::schema)]
, but that doesn't work in case of newtypes (i.e. tuple structs) (and the schema isn't automatically recursively included in the global set):Alternative: put the actual representation value inside of the container instead of
T
, e.g.EncodeHex(String)
. Downsides: isn't lazy; loses some type safety; problematic when e.g.T
needs to be serialized and it could fail, thus populating the rest of the codebase with the need to handle possible errors. If it isT
, the error could be handled when everything is serialized at once, later.Is there a way to achieve what I want in a clean way?
Beta Was this translation helpful? Give feedback.
All reactions