poem_openapi

Derive Macro Union

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

Define an 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
rename_allRename all the mapping name according to the given case convention. The possible values are “lowercase”, “UPPERCASE”, “PascalCase”, “camelCase”, “snake_case”, “SCREAMING_SNAKE_CASE”, “kebab-case”, “SCREAMING-KEBAB-CASE”.stringY

§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),
}