schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Option<bool> doesn't get required in schema

Open jakajancar opened this issue 4 years ago • 1 comments

(cf. Issue #11: Option<bool> gets required in schema)

Type:

pub struct MyType {
    my_prop: Option<String>
}

gets written into a schema as not required.

When generating a TypeScript type for this, you (correctly) get:

interface MyType {
    myProp: string | null | undefined
    // or
    myProp?: string | null
}

Either way, the user of my API sees string | null | undefined, which is not the case: my API will always return the property (I do not use #[serde(skip_serializing_if = "Option::is_none")] or similar).

The behavior should be configurable. If you're ingesting stuff, you might want to be loose. But if you're documenting your own API, you might want to be specific (Robustness principle).

jakajancar avatar Jun 26 '21 23:06 jakajancar

This is a special case of #48 - the generated schema currently describes the deserialization contract (where the property may be null or omitted), but you want the serialization contract (where the property will never be omitted)

GREsau avatar Jun 05 '24 20:06 GREsau

Closing in favour of https://github.com/GREsau/schemars/issues/48

GREsau avatar Aug 18 '24 14:08 GREsau