Skip to content

[REQ] [Rust Axum] generated Object wrapper makes returning JSON inefficient (inner value not pub) #22143

@Adnan-Sabic

Description

@Adnan-Sabic

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 data

the 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"))
    }
}```

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions