Skip to content

serde(transparent) and schemars(rename) interaction #489

@mdepta42

Description

@mdepta42

Hi, is there a way to get #[serde(transparent)] behaviour of inlining the inner type along with custom schema name from `#[schemars(rename = "...")]?

Here's an example

use schemars::JsonSchema;
use serde::Serialize;

#[derive(Serialize, JsonSchema)]
pub struct Anonymous<T>(T);

#[derive(Serialize, JsonSchema)]
#[serde(transparent)]
#[schemars(rename = "Alias")]
pub struct AliasNewType(Anonymous<i32>);

#[derive(Serialize, JsonSchema)]
#[schemars(rename = "Wrapped{T}")]
pub struct Wrapper<T>(T);

fn main() {
    println!(
        "{:#}",
        schemars::schema_for!(Wrapper<AliasNewType>).to_value()
    );
}

In the case above I'd expect that #[schemars(rename = "Alias")] would override serde's attributes (which is otherwise always the case as far as I know), and yet it doesn't here:

{
  "$defs": {
    "Anonymous": {
      "format": "int32",
      "type": "integer"
    }
  },
  "$ref": "#/$defs/Anonymous",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "WrappedAnonymous"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions