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

Add attribute to optionally inline schemas when deriving #353

Open
michaeltlombardi opened this issue Nov 19, 2024 · 0 comments
Open

Add attribute to optionally inline schemas when deriving #353

michaeltlombardi opened this issue Nov 19, 2024 · 0 comments

Comments

@michaeltlombardi
Copy link

As a developer and schema maintainer, I want to control which subschemas are inlined and which are placed in $defs.

I have a use-case where I want to define a struct that inlines a particular subschema but uses $defs for external schemas (creating a canonically bundled schema).

For example, I would like to be able to define something like the following (contrived) example:

mod tstoy {
    mod config {
        #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
        #[serde(rename_all = "camelCase")]
        pub enum Scope {
          Machine,
          User,
        }

        #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
        pub struct UpdateSettings {
            pub automatic: Option<bool>,
            pub frequency: Option<int>,
        }

        #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
        pub struct Instance {
            // This property should only be a $ref, no $defs
            #[serde(rename = "_exist", default)]
            #[schemars(schema_with = "dsc_rdk::standard_properties::reference_exist")]
            pub exist: bool,
            
            pub scope: ConfigScope,
            
            // Even though this property normally would be a $ref to an entry in $defs,
            // I want to inline the subschema without having to implement JsonSchema for
            // the struct.
            #[schemars(inline)]
            pub update: Option<UpdateSettings>,
        }
     }
  }

And have it emit something like the following (hand-authored) schema:

{
    "type": "object",
    "required": ["scope"]
    "properties": {
        "_exist": { "$ref": "https://schemas.example.com/v1/standard_properties/exist.json" },
        "scope": {
            "type": "string"
            "enum": ["machine", "user"]
        },
        "update": {
            "automatic": { "type": "boolean" }
            "frequency": { "type": "integer" }
        }
    },
    // I know that right now I'd have to insert this myself
    "$defs": {
        "https://schemas.example.com/v1/standard_properties/exist.json": {
            "type": "boolean,
            "default": true
        }
    }
}

I can probably figure out how to write appropriate transformers for this for my own usage, but I would prefer to be able to control whether a particular item in my struct is inlined without having to add extra custom handling.

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

1 participant