Skip to content

Support individually untagged enum values #388

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

Open
flying-sheep opened this issue Apr 23, 2025 · 0 comments · May be fixed by #389
Open

Support individually untagged enum values #388

flying-sheep opened this issue Apr 23, 2025 · 0 comments · May be fixed by #389

Comments

@flying-sheep
Copy link

flying-sheep commented Apr 23, 2025

Serde allows to add #[serde(untagged)] to individual enum variants.

See here for an example of the working serde code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=75fc42b60b77cca8eeeb8fab53a3f78c

If I add schemars to this, the generated schema is wrong:

#![allow(dead_code)]

use serde_derive::{Serialize, Deserialize};
use schemars::{schema_for, JsonSchema};

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
enum StructuralSubElement {
    Title,
    Subtitle,
    Decoration,
    Docinfo,
    #[serde(untagged)]
    SubStructure(SubStructure),
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
enum SubStructure {
    Topic, Sidebar, Transition, Section, BodyElement
}

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

The resulting schema has this wrong "const": "SubStructure" constraint:

{
	"StructuralSubElement": {
		"oneOf": [
			...,
			{
				"type": "object",
				"properties": {
					"type": { "type": "string", "const": "SubStructure" }
				},
				"$ref": "#/$defs/SubStructure",
				"required": ["type"]
			}
		]
	}
}

Maybe related: #311

@flying-sheep flying-sheep linked a pull request Apr 23, 2025 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant