logo

Derive Macro poem_openapi::Union

source · []
#[derive(Union)]
{
    // Attributes available to this derive:
    #[oai]
}
Expand description

Define a OpenAPI discriminator object.

Macro parameters

AttributedescriptionTypeOptional
discriminator_nameThe name of the property in the payload that will hold the discriminator value.stringY
one_ofValidates the value against exactly one of the subschemasboolY
external_docsSpecify a external resource for extended documentationstringY

Item parameters

AttributedescriptionTypeOptional
mappingRename the payload value. (Default is the object name)stringY

Example with discriminator

use poem_openapi::{Object, Union};

#[derive(Object, Debug, PartialEq)]
struct A {
    v1: i32,
    v2: String,
}

#[derive(Object, Debug, PartialEq)]
struct B {
    v3: f32,
}

#[derive(Union, Debug, PartialEq)]
#[oai(discriminator_name = "type")]
enum MyObj {
    A(A),
    B(B),
}

Example without discriminator

use poem_openapi::{Object, Union};

#[derive(Object, Debug, PartialEq)]
struct A {
    v1: i32,
    v2: String,
}

#[derive(Union, Debug, PartialEq)]
enum MyObj {
    A(A),
    B(bool),
    C(String),
}