Marwes / schemafy

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

Add support for one_of type #51

Closed bemyak closed 3 years ago

bemyak commented 3 years ago

This PR adds support for one_of field type. It relies on serde's untagged enums feature, meaning that schemafy will try to guess which enum variant is the most suitable.

One caveat here is that serde is a bit too eager to find at least one match, so if an object matches several enum variants, the first will be chosen. I see this as a minor thing, since validating incoming JSONs is not a goal of this project. However, this peculiarity breaks negative tests from the Test Suite, so I had to copy-paste the successful scenarios to a regular test file.

Apart from one_of feature, this PR also fixes how relative schema paths are resolved. The previous implementation relied on CARGO_MANIFEST_DIR env variable, which may not be present in some use cases, e.g. debugging a test using Debug button in rust-analyzer.