This PR addresses a current hole in the TypeScript code generation for
the API server protocol. Fields that are marked as "Optional<>" in the
Rust code are serialized such that the value is omitted when it is
deserialized — appearing as `undefined`, but the TS type indicates
(incorrectly) that it is always defined but possibly `null`. This can
lead to subtle errors that the TypeScript compiler doesn't catch. The
fix is to include the `#[ts(optional_fields = nullable)]` macro for all
protocol structs that contain one or more `Optional<>` fields.
This PR also includes a new test that validates that all TS protocol
code containing "| null" in its type is marked optional ("?") to catch
cases where `#[ts(optional_fields = nullable)]` is omitted.
34 lines
951 B
Rust
34 lines
951 B
Rust
use schemars::JsonSchema;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use std::path::PathBuf;
|
|
use ts_rs::TS;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, JsonSchema, TS)]
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
|
pub enum ParsedCommand {
|
|
Read {
|
|
cmd: String,
|
|
name: String,
|
|
/// (Best effort) Path to the file being read by the command. When
|
|
/// possible, this is an absolute path, though when relative, it should
|
|
/// be resolved against the `cwd`` that will be used to run the command
|
|
/// to derive the absolute path.
|
|
path: PathBuf,
|
|
},
|
|
ListFiles {
|
|
cmd: String,
|
|
#[ts(optional = nullable)]
|
|
path: Option<String>,
|
|
},
|
|
Search {
|
|
cmd: String,
|
|
#[ts(optional = nullable)]
|
|
query: Option<String>,
|
|
#[ts(optional = nullable)]
|
|
path: Option<String>,
|
|
},
|
|
Unknown {
|
|
cmd: String,
|
|
},
|
|
}
|