schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Option<bool> doesn't get required in schema

Open jakajancar opened this issue 3 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