Skip to content

Codegen issues with string enums #862

@aumetra

Description

@aumetra

First up, thank you for your work on typify, great crate. Helped me a few times already.

Right now I'm attempting to build a system to handle glTF files, and apparently typify has some issues with their string enum definitions.

Their definitions look as follows (this is one example, there are multiple of those in their schemas):

[...]
"interpolation": {
            "description": "Interpolation algorithm.",
            "default": "LINEAR",
            "gltf_detailedDescription": "Interpolation algorithm.",
            "anyOf": [
                {
                    "const": "LINEAR",
                    "description": "The animated values are linearly interpolated between keyframes. When targeting a rotation, spherical linear interpolation (slerp) **SHOULD** be used to interpolate quaternions. The number of output elements **MUST** equal the number of input elements."
                },
                {
                    "const": "STEP",
                    "description": "The animated values remain constant to the output of the first keyframe, until the next keyframe. The number of output elements **MUST** equal the number of input elements."
                },
                {
                    "const": "CUBICSPLINE",
                    "description": "The animation's interpolation is computed using a cubic spline with specified tangents. The number of output elements **MUST** equal three times the number of input elements. For each input element, the output stores three elements, an in-tangent, a spline vertex, and an out-tangent. There **MUST** be at least two keyframes when using this interpolation."
                },
                {
                    "type": "string"
                }
            ]
        }
[...]

where I'd expect a codegen such as:

#[derive(Deserialize, Serialize, Default)]
pub enum Interpolation {
  #[default]
  #[serde(rename = "LINEAR")]
  #[doc = "The animated values are linearly interpolated between keyframes. When targeting a rotation, spherical linear interpolation (slerp) **SHOULD** be used to interpolate quaternions. The number of output elements **MUST** equal the number of input elements."]
  Linear,
  #[serde(rename = "STEP")]
  #[doc = "BLAH"]
  Step,
  #[serde(rename = "CUBICSPLINE")]
  #[doc = "BLAH x2"]
  Cubicspline,
  #[serde(other)]
  Other(String),
}

In its current state, typify just errors out (which is better than producing faulty definitions, I guess).

Backtrace
Error: value does not conform to the given schema

Stack backtrace:
   0: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from
             at /home/aumetra/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/anyhow-1.0.98/src/backtrace.rs:27:14
   1: <core::result::Result<T,F> as core::ops::try_trait::FromResidual<core::result::Result<core::convert::Infallible,E>>>::from_residual
             at /home/aumetra/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:2050:27
   2: xtask::generate_structs::do_it
             at ./xtask/src/generate_structs.rs:68:5
   3: xtask::main
             at ./xtask/src/main.rs:28:44
   4: core::ops::function::FnOnce::call_once
             at /home/aumetra/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
   5: std::sys::backtrace::__rust_begin_short_backtrace
             at /home/aumetra/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/backtrace.rs:152:18
   6: std::rt::lang_start::{{closure}}
             at /home/aumetra/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:199:18
   7: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/core/src/ops/function.rs:284:13
   8: std::panicking::try::do_call
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panicking.rs:589:40
   9: std::panicking::try
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panicking.rs:552:19
  10: std::panic::catch_unwind
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panic.rs:359:14
  11: std::rt::lang_start_internal::{{closure}}
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/rt.rs:168:24
  12: std::panicking::try::do_call
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panicking.rs:589:40
  13: std::panicking::try
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panicking.rs:552:19
  14: std::panic::catch_unwind
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panic.rs:359:14
  15: std::rt::lang_start_internal
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/rt.rs:164:5
  16: std::rt::lang_start
             at /home/aumetra/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:198:5
  17: main
  18: <unknown>
  19: __libc_start_main
  20: _start

By patching out some of the sanity checks, I got it to emit some definitions, and the definitions look like this:

#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct AnimationSamplerInterpolation {
    #[serde(
        flatten,
        default,
        skip_serializing_if = "::std::option::Option::is_none"
    )]
    pub subtype_0: ::std::option::Option<::serde_json::Value>,
    #[serde(
        flatten,
        default,
        skip_serializing_if = "::std::option::Option::is_none"
    )]
    pub subtype_1: ::std::option::Option<::serde_json::Value>,
    #[serde(
        flatten,
        default,
        skip_serializing_if = "::std::option::Option::is_none"
    )]
    pub subtype_2: ::std::option::Option<::serde_json::Value>,
    #[serde(
        flatten,
        default,
        skip_serializing_if = "::std::option::Option::is_none"
    )]
    pub subtype_3: ::std::option::Option<::std::string::String>,
}

Where I can sorta get where the generator is coming from when it generated it.


Unfortunately I don't have too much knowledge about the internals to be able to come up with a solution that generates correct types in this scenario.

Metadata

Metadata

Assignees

No one assigned

    Labels

    fancy typesSchema construction that requires a new type or custom serde impls

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions