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

Generating definition types for enums with serde tag #244

Open
vron opened this issue Sep 8, 2023 · 3 comments
Open

Generating definition types for enums with serde tag #244

vron opened this issue Sep 8, 2023 · 3 comments

Comments

@vron
Copy link

vron commented Sep 8, 2023

Hi,

First of all - thanks for a very usefull library. I'm using it for synchronizing types from rust for a large project.

However, one issue I have is that I would like to be able to refer to the structs that are parts of enums directly - but schemars seems to inline those struct (which I think is a bug?)

Consider the following input:

use schemars::{schema_for, JsonSchema};

#[derive(JsonSchema)]
pub struct MyStruct {
    pub my_int: i32,
    pub my_bool: bool,
    pub my_nullable_enum: Option<MyEnum>,
}

#[derive(JsonSchema)]
#[serde(tag = "type")]
pub enum MyEnum {
    StringNewType(String),
    StructVariant(SubType),
}

#[derive(JsonSchema)]
pub struct SubType {
    pub aa: i64,
}

fn main() {
    let schema = schema_for!(MyStruct);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}

Which generates:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "required": [
    "my_bool",
    "my_int"
  ],
  "properties": {
    "my_bool": {
      "type": "boolean"
    },
    "my_int": {
      "type": "integer",
      "format": "int32"
    },
    "my_nullable_enum": {
      "anyOf": [
        {
          "$ref": "#/definitions/MyEnum"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "definitions": {
    "MyEnum": {
      "oneOf": [
        {
          "type": [
            "object",
            "string"
          ],
          "required": [
            "type"
          ],
          "properties": {
            "type": {
              "type": "string",
              "enum": [
                "StringNewType"
              ]
            }
          }
        },
        {
          "type": "object",
          "required": [
            "aa",
            "type"
          ],
          "properties": {
            "aa": {
              "type": "integer",
              "format": "int64"
            },
            "type": {
              "type": "string",
              "enum": [
                "StructVariant"
              ]
            }
          }
        }
      ]
    }
  }
}

Note in particular that no definition is generated for SubType which means that I am unable to generate types that can be refered to in code generated from the schema.

@vron
Copy link
Author

vron commented Sep 20, 2023

Hi,

Is there any interest in this? If I (with some pointers) tried to implement a PR for this - would it be likely to be accepted? @GREsau

@vron
Copy link
Author

vron commented Sep 21, 2023

Closing - found #157 which this seems to be duplicate of - my question stands though if there would be interest in a PR to adress this?

@vron vron closed this as completed Sep 21, 2023
@vron
Copy link
Author

vron commented Sep 21, 2023

While debugging turns out this is actually not a duplicate! The other issue is about getting a ref for the enum type - this issue is about getting a ref to the struct inside the enum type.

@vron vron reopened this Sep 21, 2023
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