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

Panic when generating an API for a struct with an enum serde(flatten)ed into a struct. #1247

Open
blinsay opened this issue Jan 31, 2025 · 0 comments

Comments

@blinsay
Copy link

blinsay commented Jan 31, 2025

Following up on #1244, I've been trying to use a struct with a serde(flatten) field as a return type, and I'm getting a panic when generating an openapi description. The panic seems to only happen when calling write on a generated stub description - I can serve traffic fine!

Here's a repro:

use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::RequestContext;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

fn main() {
    let description = example_api_mod::stub_api_description().unwrap();
    let openapi = description.openapi("example", "0.1.0".parse().unwrap());

    openapi.write(&mut std::io::stdout().lock()).unwrap();
}

#[derive(Serialize, Deserialize, JsonSchema)]
struct CoolStruct {
    #[serde(flatten)]
    cool_enum: CoolEnum,

    another_thing: u16,
}

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
enum CoolEnum {
    Foo(Foo),
    Bar(Bar),
}

#[derive(Serialize, Deserialize, JsonSchema)]
struct Foo {
    thing_one: String,
}

#[derive(Serialize, Deserialize, JsonSchema)]
struct Bar {
    thing_one: String,
    thing_two: String,
}

#[dropshot::api_description]
trait ExampleApi {
    type Context;

    #[endpoint {
        method = GET,
        path = "/hello",
    }]
    async fn hello(
        ctx: RequestContext<Self::Context>,
    ) -> Result<HttpResponseOk<CoolStruct>, HttpError>;
}
Here's the full panic
thread 'main' panicked at /Users/benl/.cargo/registry/src/index.crates.io-6f17d22bba15001f/dropshot-0.15.1/src/schema_util.rs:466:31:
a schema can't have both a type and subschemas:
{
  "type": "object",
  "oneOf": [
    {
      "type": "object",
      "required": [
        "thing_one",
        "type"
      ],
      "properties": {
        "thing_one": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "enum": [
            "Foo"
          ]
        }
      }
    },
    {
      "type": "object",
      "required": [
        "thing_one",
        "thing_two",
        "type"
      ],
      "properties": {
        "thing_one": {
          "type": "string"
        },
        "thing_two": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "enum": [
            "Bar"
          ]
        }
      }
    }
  ],
  "required": [
    "another_thing"
  ],
  "properties": {
    "another_thing": {
      "type": "integer",
      "format": "uint16",
      "minimum": 0.0
    }
  }
}
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