anixe / doku

fn(Code) -> Docs
MIT License
80 stars 5 forks source link

different output for enum struct variant and enum tuple variant of struct #33

Closed CobaltCause closed 1 year ago

CobaltCause commented 1 year ago

Enum struct variant

Code

use doku::Document;
use serde::Deserialize;

#[derive(Deserialize, Debug, Document)]
struct Config {
    foo: Foo,
}

#[derive(Deserialize, Debug, Document)]
struct Foo {
    #[serde(flatten)]
    bar: Bar,
}

#[derive(Deserialize, Debug, Document)]
#[serde(untagged)]
enum Bar {
    Baz {
        wat: String,
    },

    Qux {
        huh: String,
        wut: String,
    },
}

fn main() {
    println!("{}", doku::to_json::<Config>());
}

Output

{
  "foo": {

      "wat": "string"
      // or
      "huh": "string",
      "wut": "string"
  }
}

Enum tuple variant of struct

Code

use doku::Document;
use serde::Deserialize;

#[derive(Deserialize, Debug, Document)]
struct Config {
    foo: Foo,
}

#[derive(Deserialize, Debug, Document)]
struct Foo {
    #[serde(flatten)]
    bar: Bar,
}

#[derive(Deserialize, Debug, Document)]
#[serde(untagged)]
enum Bar {
    Baz {
        wat: String,
    },

    Qux(Qux),
}

#[derive(Deserialize, Debug, Document)]
struct Qux {
    huh: String,
    wut: String,
}

fn main() {
    println!("{}", doku::to_json::<Config>());
}

Output

{
  "foo": {

      "wat": "string"
      // or
      {
        "huh": "string",
        "wut": "string"
      }
  }
}

Note the extra brackets around the huh and wut fields. Is this expected behavior? It seems like a bug because serde handles these two cases identically.

Patryk27 commented 1 year ago

Oh, nice catch - I'll take a look tomorrow!

Patryk27 commented 1 year ago

Hi, I've just published v0.21.1 with this change - if you do cargo update -p doku, you should get the fix.

If anything else looks off, lemme know 🙂