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

How to auto generate references for enum variants #201

Open
universalmind303 opened this issue Feb 3, 2023 · 1 comment
Open

How to auto generate references for enum variants #201

universalmind303 opened this issue Feb 3, 2023 · 1 comment

Comments

@universalmind303
Copy link

Is there any way to force schemars to use references for enum variants? When inlining, many tools can't recognize the title or description for that object, and just shows up as (object).

For example.

pub enum StringExpr {
    #[serde(rename = "$starts_with")]
    StartsWith(String),
    #[serde(rename = "$ends_with")]
    EndsWith(String),
}

and this will generate something like this:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "StringExpr",
  "oneOf": [
    {
      "type": "object",
      "required": [
        "$starts_with"
      ],
      "properties": {
        "$starts_with": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    {
      "type": "object",
      "required": [
        "$ends_with"
      ],
      "properties": {
        "$ends_with": {
          "type": "string"
        }
      },
      "additionalProperties": false
    }
  ]
}

But what would be preferred is if there was some way to make all of those inner objects references by default.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "StringExpr",
  "examples": [
    {
      "$starts_with": "bar"
    }
  ],
  "oneOf": [
    {
      "$ref": "#/definitions/StringExpr/starts_with"
    },
    {
      "$ref": "#/definitions/StringExpr/ends_with"
    }
  ],
  "definitions": {
    "StringExpr": {
      "starts_with": {
        "type": "object",
        "required": [
          "$starts_with"
        ],
        "properties": {
          "$starts_with": {
            "type": "string"
          }
        },
        "additionalProperties": false
      },
      "ends_with": {
        "type": "object",
        "required": [
          "$ends_with"
        ],
        "properties": {
          "$ends_with": {
            "type": "string"
          }
        },
        "additionalProperties": false
      }
    }
  }
}
@universalmind303
Copy link
Author

this actually looks like a duplicate of #157

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