msrd0/gotham_restful

Rewriting of OpenAPI types with the same name

Closed this issue · 2 comments

Hi

I've noticed that if we have for example 2 Resources handlers of which uses the same Request name (I guess the same applies to Response) got to be using the same schema (structure) where in reality they use different.

My guess because some type of HashMap is used with keys as structure names

Probably somewhere here

self.add_schema_impl(name, schema);

Example

pub mod res1 {
  #[derive(Resource)]
  #[resource(requst)]
  pub struct Resource1;
 
  #[derive(OpenapiType, Deserialize, Serialize)]
  struct Request {
      id: String,
  }

  #[create]
  async fn requst(req: Request)  -> Success<String> {
     String::new().into()
  }
}

pub mod res2 {
  #[derive(Resource)]
  #[resource(requst)]
  pub struct Resource2;
 
  #[derive(OpenapiType, Deserialize, Serialize)]
  struct Request {
      anotherId: String,
  }

  #[create]
  async fn requst(req: Request)  -> Success<String> {
     String::new().into()
  }
}

Once we will add them in router and check openapi_doc we will see that both of them have the same request, which is in reality not true.

Thank you.

msrd0 commented

Yes, I'm aware of this. I believe this is documented somewhere, but I can't find it right now, so maybe I should make this behaviour more clear in the documentation.

The issue is actually much more involved than just not using a hashmap in this crate - the openapi_type crate will already produce an invalid schema if you have code like this:

mod foo1 {
    struct Foo { id: u64 }
}
mod foo2 {
    struct Foo { another_id: u64 }
}

struct Bar {
    foo1: foo1::Foo,
    foo2: foo2::Foo
}

The schema will not make any difference between these two types. For now, you will have to ensure manually that all types have unique names.

Thank you for the clarification.
And sorry that I did not tried to make sure that it did documented somewhere 😅

Then I'm closing it.

Take care.