okapi icon indicating copy to clipboard operation
okapi copied to clipboard

Unable to borrow value returned from definitions_mut() function

Open pranav-joshi opened this issue 2 years ago • 1 comments

example code: [Note - I have added the references correctly. Also, I am new to Rust, sorry in advance for my naiveite]

impl OpenApiResponderInner for XYZ{
    fn responses(
        gen: &mut OpenApiGenerator,
    ) -> std::result::Result<Responses, OpenApiError> {
    let mut content = Map::new();
        let settings = schemars::gen::SchemaSettings::default().with(|s| {
            s.option_nullable = true;
            s.option_add_null_type = false;
            s.definitions_path = "#/components/schemas/".to_string();
        });

        let mut schema_generator = settings.into_generator();
        let schema = schema_generator.root_schema_for::<ErrorCode>();

        let definitions = gen.schema_generator().definitions_mut(); //error[E0596]: cannot borrow data in a `&` reference as mutable
        for (key, value) in schema.definitions {
            definitions.insert(key, value);
        }
}

pranav-joshi avatar Jul 24 '23 17:07 pranav-joshi

Could you maybe add some more info on what you want to do? It looks like you are trying to do something complicated that could maybe be solved in a much easier way.

It looks like you are trying to create a custom response. But that is what this crate/package does for you. Maybe take a look at this (or the other) example. This shows you how you can use this package. https://github.com/GREsau/okapi/blob/345e11769759db84beb35c5d47daccdc381acf9e/examples/json-web-api/src/main.rs You mostly just define a struct of some kind and use that as a function return parameter. As you see here: https://github.com/GREsau/okapi/blob/345e11769759db84beb35c5d47daccdc381acf9e/examples/json-web-api/src/main.rs#L27 if you want it to be optional (not required) you can use Option<_> as you see here: https://github.com/GREsau/okapi/blob/345e11769759db84beb35c5d47daccdc381acf9e/examples/json-web-api/src/main.rs#L40

If you have questions, feel free to ask!

ralpha avatar Nov 09 '23 01:11 ralpha