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

Add support for serialize_with and deserialize_with #322

Closed
banool opened this issue Jul 18, 2022 · 3 comments
Closed

Add support for serialize_with and deserialize_with #322

banool opened this issue Jul 18, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@banool
Copy link
Contributor

banool commented Jul 18, 2022

Hey, imagine I have code like this:

struct MyThing {
    my_other_thing: MyOtherThing
}

struct MyOtherThing {
    bytes: HexEncodedBytes,
}

struct HexEncodedBytes(Vec<u8>);

As it is now, you can't impl NewType on HexEncodedBytes as it doesn't impl ToHeader. That's not important though, imagine I want to make my API return HexEncodedBytes as a string like 0x32434. To get around this, I have to make 2 new structs: MyThingWrapper, MyOtherThingWrapper, and then a new newtype that for example encodes the Vec<u8> in some other way (a string, b64, etc). On top of that, I have to impl From<MyThingWrapper> for MyThing and all of the other types too.

As you can see, this ends up being a lot of additional wrapping code. Instead, what I'd like to do this this:

struct MyThing {
    my_other_thing: MyOtherThing
}

struct MyOtherThing {
    #[oai(serialize_with = "my_serializer", deserialize_with = "my_deserializer")]
    bytes: HexEncodedBytes,
}

struct HexEncodedBytes(Vec<u8>);

fn my_serializer(value: Vec<u8>) -> String {
    // convert
}

fn my_deserializer(value: String) -> Vec<u8> {
    // convert
}

Either that, or just pass in a serializer. With this, the spec should say that this field is a String, not a Vec<u8>.

Is there some way we could add this feature?

Thanks!

@banool banool added the enhancement New feature or request label Jul 18, 2022
@banool
Copy link
Contributor Author

banool commented Jul 18, 2022

Okay I'm digging deeper into a better way to do this, and I'm currently impling Type, ParseFromJSON, etc. on my own for my types. This is working fairly well so far but the moment I get to a type I don't own, I struggle. This type is a newtype so I can't impl remote either. Perhaps including remote as part of the ask in #320 would be a solution to this part of the problem.

@banool
Copy link
Contributor Author

banool commented Jul 19, 2022

I'm going to close this for now, I don't think this is a well-thought-out suggestion as it is.

@banool banool closed this as completed Jul 19, 2022
@BatmanAoD
Copy link

For future readers, serialize_with and deserialize_with were added in #749.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants