FastUI
FastUI copied to clipboard
Proposal: using generic types in schema
This is just a proposal, I can make the PR and help in contributing. I think the doc generation is quite useful, and endpoint can be still reused by other clients. but what's lacking the most, is that you cannot explain much about included details.
I came up with this prototype,
TValue = _t.TypeVar('TValue')
TLabel = _t.TypeVar('TLabel')
class SelectOption(pydantic.BaseModel, _t.Generic[TValue, TLabel]):
value: TValue
label: TLabel
class SelectGroup(pydantic.BaseModel, _t.Generic[TValue, TLabel]):
label: TLabel
options: _t.List[SelectOption[TValue, TLabel]]
# Now, SelectOptions can be used with specific types for value and label
SelectOptions = _t.Union[_t.List[SelectOption[TValue, TLabel]], _t.List[SelectGroup[TValue, TLabel]]]
class SelectSearchResponse(pydantic.BaseModel, _t.Generic[TValue, TLabel]):
options: _t.Union[_t.List[SelectOption[TValue, TLabel]], _t.List[SelectGroup[TValue, TLabel]]]
and then created a model using: SelectSearchResponse[UUID, str] which you can see the result below:
{
"$defs":{
"SelectGroup_UUID_str_":{
"properties":{
"label":{
"title":"Label",
"type":"string"
},
"options":{
"items":{
"$ref":"#/$defs/SelectOption_UUID_str_"
},
"title":"Options",
"type":"array"
}
},
"required":[
"label",
"options"
],
"title":"SelectGroup[UUID, str]",
"type":"object"
},
"SelectOption_UUID_str_":{
"properties":{
"value":{
"format":"uuid",
"title":"Value",
"type":"string"
},
"label":{
"title":"Label",
"type":"string"
}
},
"required":[
"value",
"label"
],
"title":"SelectOption[UUID, str]",
"type":"object"
}
},
"properties":{
"options":{
"anyOf":[
{
"items":{
"$ref":"#/$defs/SelectOption_UUID_str_"
},
"type":"array"
},
{
"items":{
"$ref":"#/$defs/SelectGroup_UUID_str_"
},
"type":"array"
}
],
"title":"Options"
}
},
"required":[
"options"
],
"title":"SelectSearchResponse[UUID, str]",
"type":"object"
}
Ye, we want better support for generic types.