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

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

Open
pranav-joshi opened this issue Jul 24, 2023 · 1 comment
Open
Labels
rocket-okapi This affects the rocket-okapi crate

Comments

@pranav-joshi
Copy link

pranav-joshi commented Jul 24, 2023

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
Copy link
Collaborator

ralpha commented Nov 9, 2023

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:

fn get_all_users() -> Json<Vec<User>> {

if you want it to be optional (not required) you can use Option<_> as you see here:
fn get_user(id: u64) -> Option<Json<User>> {

If you have questions, feel free to ask!

@ralpha ralpha added the rocket-okapi This affects the rocket-okapi crate label Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rocket-okapi This affects the rocket-okapi crate
Projects
None yet
Development

No branches or pull requests

2 participants