-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Labels
Description
Is your feature request related to a problem? Please describe.
When generating an endpoint response with(yaml):
paths:
/somePath:
get:
summary: Get all models
operationId: getModels
responses:
'200':
description: Successful operation
content:
application/json:
schema:
type: object
description: A generic JSON object containing model datathe generator creates a Rust type like this:
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Object(serde_json::Value);However, the inner serde_json::Value field is private.
This makes it cumbersome to return a types::Object directly from endpoint handlers.
Current behavior
Because the field is private, I currently have to serialize and deserialize manually:
let model = MyModel { ... };
let v = serde_json::to_string(&model)?;
let obj = serde_json::from_str::<types::Object>(&v)
.map_err(|e| e.to_string())?;This works, but it’s inefficient and adds unnecessary conversions.
Describe the solution you'd like
It would be much simpler if the generated Object looked like this:
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Object(pub serde_json::Value);That way, you could write directly:
let response = serde_json::to_value(my_model)
.map(types::Object)
.map_err(|e| e.to_string());No extra conversions or string parsing needed.
Describe alternatives you've considered
impl the object with a new fn:
impl Object {
/// Creates a new `Object` from any serializable value.
pub fn new<T: serde::Serialize>(value: T) -> Self {
Self(serde_json::to_value(value).expect("Failed to serialize value into JSON"))
}
}```