openapi icon indicating copy to clipboard operation
openapi copied to clipboard

Better ergonomics for Operations

Open JordiPolo opened this issue 8 years ago • 3 comments

I've started working on code based on this library and working with Operations is difficult I want to do something like:

for operation in operations {
  ...
}

But each operation is a different field in the structure, I do not know if it's possible to iterate them in Rust

JordiPolo avatar May 15 '17 14:05 JordiPolo

I'm not sure that the swagger spec itself is designed for that at the moment each path maps to a path item object when I started on this journey my assumptions where the same as yours and what the struct that represents this is called Operations. In testing deserializing more openapi examples in the wild I came across some examples using the shared parameters which broke that assumption. I think maybe it makes sense to provide an inherent impl for the Operations object which collects the HTTP methods into a vec to support this kind of iteration.

softprops avatar May 20 '17 17:05 softprops

I found myself in a similar situation today. I need to iterate over all operations (it's just easier with my current code). So far I have created an enum for all of the possible method verbs and I turn the Options into iterators and chain them, so something like:

let get = item.get.as_ref().map(|x| (Method::Get, x));
let post = // ....

get.into_iter().chain(post) // and so on

While this works, I think it would be more suitable/ergonomic for all operations to be a map (let's assume a BTreeMap) and the key should be something like the Method enum I mentioned. I believe this is possible with #[serde(flatten)].

I am willing to work on researching and/or implementing this, if it's deemed appropriate.

IovoslavIovchev avatar May 07 '22 15:05 IovoslavIovchev

Since I needed this, I already implemented it in my fork https://github.com/IovoslavIovchev/openapi/blob/master/src/v3_0/schema.rs#L186

IovoslavIovchev avatar May 30 '22 06:05 IovoslavIovchev