openapi
openapi copied to clipboard
Better ergonomics for Operations
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
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.
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.
Since I needed this, I already implemented it in my fork https://github.com/IovoslavIovchev/openapi/blob/master/src/v3_0/schema.rs#L186