GREsau / okapi

OpenAPI (AKA Swagger) document generation for Rust projects
MIT License
578 stars 103 forks source link

Unable to borrow value returned from definitions_mut() function #129

Open pranav-joshi opened 1 year ago

pranav-joshi commented 1 year ago

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);
        }
}
ralpha commented 8 months ago

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!