Marwes / schemafy

Crate for generating rust types from a json schema
MIT License
242 stars 51 forks source link

Generated structure naming inconsistency? #53

Closed KnowZero closed 3 years ago

KnowZero commented 3 years ago

So I ran this to see how things end up:

use serde_json;
use schemafy_lib::Expander;

fn main() {

    let json = r#"{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Test Schema",
  "type": "object",
  "properties": {
    "method": {
      "type": "string"
    },
    "fetch": {
      "type": "object",
      "properties": {
        "url": {
          "type": "string"
        }
      },
      "required": [
        "url"
      ]
    },

    "process": {
      "type": "object",
      "properties": {
        "replace": {
          "type": "object",
          "properties": {
            "url": {
              "type": "string"
            }
          },
          "required": [
            "url"
          ]
        }
      },
      "required": [
        "replace"
      ]
    }
  },
  "required": [
    "method",
    "fetch",
    "process"
  ]
}"#; 
    let schema = serde_json::from_str(json).unwrap();
    let mut expander = Expander::new(Some("Test Schema"), "::schemafy_core::", &schema);
    let res = expander.expand(&schema);

    println!("{}", 
        res.to_string()
            .replace(" # [","\n\n# [")
            .replace("pub struct ","\npub struct ")
            .replace(", pub ",",\n\tpub ")
            .replace("] pub ","]\n\tpub ")
            .replace("{ pub ","{\n\tpub ")
            .replace(" }","\n}")
        );

}

And I got this result:

# [derive (Clone , PartialEq , Debug , Deserialize , Serialize)] 
pub struct TestSchemaFetch {
        pub url : String
}

# [derive (Clone , PartialEq , Debug , Deserialize , Serialize)] 
pub struct TestSchemaFetchProcessReplace {
        pub url : String
}

# [derive (Clone , PartialEq , Debug , Deserialize , Serialize)] 
pub struct TestSchemaFetchProcess {
        pub replace : TestSchemaFetchProcessReplace
}

# [derive (Clone , PartialEq , Debug , Deserialize , Serialize)]

# [serde (rename = "Test Schema")] 
pub struct TestSchema {
        pub fetch : TestSchemaFetch ,
        pub method : String ,
        pub process : TestSchemaFetchProcess
}

Shouldn't TestSchemaFetchProcess be TestSchemaProcess as process is not a child of fetch but a neighbor?

bemyak commented 3 years ago

@KnowZero can you please check it on the master branch? I highly suspect that it was fixed some time ago. You'll need to update the dependencies section in your Cargo.toml:

schemafy = { git="https://github.com/Marwes/schemafy.git", branch = "master" }
KnowZero commented 3 years ago

Yey! Thanks, it worked when I used: (since I am using the lib here)

schemafy_lib = { git="https://github.com/Marwes/schemafy.git", branch = "master" }

Is it possible to get a release into cargo? Or is there a wait on some other issue/feature?

bemyak commented 3 years ago

@KnowZero , great to hear that it helped :)

@Marwes , do you have any plans for making a release? I'd like to add a few more features, but right now I don't have time to work on them, unfortunately :disappointed:

Marwes commented 3 years ago

Relased 0.6.0