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

no field on type Update when using serde #6

Open
Bernie opened this issue Feb 11, 2022 · 1 comment
Open

no field on type Update when using serde #6

Bernie opened this issue Feb 11, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@Bernie
Copy link

Bernie commented Feb 11, 2022

Hi, I am trying to use Mongo and Bson derived traits while having some of the fields remain handled by serde in order to have customized serialization/de-serialization for that field.

I have a very basic example here:

#[derive(Debug, Serialize, Deserialize)]
pub struct MySerdeType {
    embedded_attr: String
}

#[derive(Debug, Bson, Mongo)]
#[mongo(collection = "my_docs", field, filter, update)]
pub struct MyDoc {
    mongo_attr: String,
    #[mongo(serde)]
    #[bson(serde)]
    serde_attr: MySerdeType
}

Unfortunately, this fails to compile due to 'no field serde_attr on type my_doc::Update'.

If I remove the update attribute, then this compiles, but the model will not implement the AsUpdate or Update traits. Is this expected? Thanks!

@alexkornitzer alexkornitzer self-assigned this Feb 12, 2022
@fscc-alexkornitzer fscc-alexkornitzer added the bug Something isn't working label Feb 14, 2022
fscc-alexkornitzer added a commit that referenced this issue Feb 14, 2022
Due to a lack of derive testing unsurprisingly there are bugs... This
one was due to `#[bson(serde)]` not being correctly handled when
creating derived `Update` structures #6. This should now be fixed.
@fscc-alexkornitzer
Copy link
Contributor

Hi Bernie,

Thanks for raising this and providing a replication example, good spot. This is due to a lack of derive testing. I believe I have a fix and am going to get that pushed live now with a patch bump. I have tested it with the following example:

use mongod::{AsFilter, AsUpdate, Bson, Comparator, Mongo};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct MySerdeType {
    embedded_attr: String,
}

#[derive(Debug, Bson, Mongo)]
#[mongo(collection = "my_docs", field, filter, update)]
pub struct MyDoc {
    mongo_attr: String,
    #[bson(serde)]
    serde_attr: MySerdeType,
}

#[tokio::main]
async fn main() {
    let client = mongod::Client::new();
    let doc = MyDoc {
        mongo_attr: "foo".to_owned(),
        serde_attr: MySerdeType {
            embedded_attr: "bar".to_owned(),
        },
    };
    let result = client.insert(vec![doc]).await.unwrap();
    println!("(index: oid) {:?}", result);

    let mut filter = MyDoc::filter();
    filter.mongo_attr = Some(Comparator::Eq("foo".to_owned()));
    let mut update = MyDoc::update();
    update.serde_attr = Some(MySerdeType {
        embedded_attr: "updated".to_owned(),
    });
    let updates = mongod::Updates {
        set: Some(update),
        ..Default::default()
    };
    let updated = client.update::<MyDoc, _, _>(filter, updates).await.unwrap();
    println!("updated {} documents", updated);

    let mut filter = MyDoc::filter();
    filter.mongo_attr = Some(Comparator::Eq("foo".to_owned()));
    let result = client.find_one::<MyDoc, _>(filter).await.unwrap();
    println!("(index: oid) {:?}", result);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants