Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeSpaceSettings::with_replacement only applies to types generated from $refs #583

Open
miwig opened this issue May 10, 2024 · 2 comments

Comments

@miwig
Copy link

miwig commented May 10, 2024

It would be nice to be able to replace types that are created "inline", e.g. for array items.

@ahl
Copy link
Collaborator

ahl commented May 11, 2024

The replace logic may be a little haphazard--it could be that it would be more coherent to do in a pass after after processing the schema(s). Can you share the schema you're working with and perhaps show what you'd like the eventual output to look like? Thanks.

@miwig
Copy link
Author

miwig commented May 11, 2024

Sure.

{
  "title": "MyList",
  "type": "object",
  "properties": {
    "foo": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "bar": { "type": "string" }
        }
      }
    }
  }
}

processed with:

use std::fs::File;

use typify::{TypeSpace, TypeSpaceSettings};

fn main() -> anyhow::Result<()> {
    let mut type_space = TypeSpace::new(&TypeSpaceSettings::default().with_replacement(
        "MyListFooItem",
        "HandwrittenFoo",
        [].into_iter(),
    ));

    let f = File::open("test-rename.schema.json")?;
    let schema: schemars::schema::Schema = serde_json::from_reader(f)?;
    type_space.add_type(&schema)?;

    let contents = format!(
        "{}\n{}",
        "use serde::{Deserialize, Serialize};",
        prettyplease::unparse(&syn::parse2::<syn::File>(type_space.to_stream()).unwrap())
    );

    println!("{}", contents);

    Ok(())
}

produces

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MyList {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub foo: Vec<MyListFooItem>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MyListFooItem {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bar: Option<String>,
}

I would want it to produce

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MyList {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub foo: Vec<HandwrittenFoo>,
}

My current workaround is to either pre-process the schema as json first, replacing "items" with a $ref, or to just do string replacement on the generated source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants