use odk_schema::{
CompiledField, CompiledModel, CompiledSchema, CompiledType, CompiledUnion, FieldConstraints,
LiteralValue, ScalarType,
};
#[derive(Debug, Clone)]
pub struct ProtobufOptions {
pub package: String,
}
#[derive(Debug, Clone, Default)]
pub struct EmitOptions {
pub protobuf: Option<ProtobufOptions>,
pub json_schema: bool,
}
pub fn emit_options_for(emitters: &[String], namespace: &str) -> EmitOptions {
let protobuf = emitters
.iter()
.any(|e| e == "@typespec/protobuf")
.then(|| ProtobufOptions {
package: sanitize_identifier(namespace).to_lowercase(),
});
let json_schema = emitters.iter().any(|e| e == "@typespec/json-schema");
EmitOptions {
protobuf,
json_schema,
}
}
pub fn compiled_to_typespec(schema: &CompiledSchema, namespace: &str) -> String {
compiled_to_typespec_with_options(schema, namespace, &EmitOptions::default())
}
pub fn unsupported_feature(schema: &CompiledSchema) -> Option<String> {
schema
.unions
.iter()
.find(|u| u.is_data_carrying())
.map(|u| {
format!(
"`odk compile` does not yet support the data-carrying (tagged) union `{}`; the \
TypeSpec discriminator path is not implemented, so the discriminator would be \
dropped from the OpenAPI / JSON Schema output. Use `odk generate -t rust,typescript` \
for tagged unions.",
u.name
)
})
}
pub fn compiled_to_typespec_with_options(
schema: &CompiledSchema,
namespace: &str,
options: &EmitOptions,
) -> String {
let mut lines = Vec::new();
let protobuf = options.protobuf.is_some();
let mut imports: Vec<String> = schema.imports.clone();
if protobuf && !imports.iter().any(|i| i == "@typespec/protobuf") {
imports.push("@typespec/protobuf".to_string());
}
if options.json_schema && !imports.iter().any(|i| i == "@typespec/json-schema") {
imports.push("@typespec/json-schema".to_string());
}
for import in &imports {
lines.push(format!("import \"{}\";", import));
}
if !imports.is_empty() {
lines.push(String::new());
}
for import in &imports {
if let Some(using_ns) = import_to_using_namespace(import) {
lines.push(format!("using {};", using_ns));
}
}
if !imports.is_empty() {
lines.push(String::new());
}
if let Some(pb) = &options.protobuf {
lines.push(format!(
"@package({{ name: \"{}\" }})",
escape_string(&pb.package)
));
}
if options.json_schema {
lines.push("@TypeSpec.JsonSchema.jsonSchema".to_string());
}
lines.push(format!("namespace {};", sanitize_identifier(namespace)));
lines.push(String::new());
for union in &schema.unions {
lines.push(emit_union(union, protobuf));
lines.push(String::new());
}
for model in &schema.models {
lines.push(emit_model(model, protobuf));
lines.push(String::new());
}
for op in &schema.operations {
lines.push(emit_operation(op));
lines.push(String::new());
}
for raw_block in &schema.raw_blocks {
if let Some(desc) = &raw_block.description {
lines.push(format!("// {}", desc));
}
lines.push(raw_block.content.clone());
lines.push(String::new());
}
lines.join("\n")
}
fn emit_union(union: &CompiledUnion, protobuf: bool) -> String {
let mut lines = Vec::new();
if let Some(desc) = &union.description {
lines.push(format!("/** {} */", desc));
}
let is_string_enum = union.variants.iter().all(|v| {
matches!(
&v.variant_type,
Some(CompiledType::Literal {
value: LiteralValue::String(_)
})
)
});
if is_string_enum {
lines.push(format!("enum {} {{", sanitize_identifier(&union.name)));
for (index, variant) in union.variants.iter().enumerate() {
if protobuf {
lines.push(format!(
" {}: {},",
sanitize_identifier(&variant.name),
index
));
} else if let Some(CompiledType::Literal {
value: LiteralValue::String(s),
}) = &variant.variant_type
{
if is_valid_identifier(&variant.name) && variant.name == *s {
lines.push(format!(" {},", variant.name));
} else {
lines.push(format!(
" {}: \"{}\",",
sanitize_identifier(&variant.name),
s
));
}
}
}
lines.push("}".to_string());
} else {
let variant_types: Vec<String> = union
.variants
.iter()
.map(|v| {
v.variant_type
.as_ref()
.map(|t| type_to_string(t, false))
.unwrap_or_else(|| v.name.clone())
})
.collect();
lines.push(format!(
"alias {} = {};",
sanitize_identifier(&union.name),
variant_types.join(" | ")
));
}
lines.join("\n")
}
fn emit_model(model: &CompiledModel, protobuf: bool) -> String {
let mut lines = Vec::new();
if let Some(desc) = &model.description {
lines.push(format!("/** {} */", desc));
}
for decorator in model.extensions.decorators() {
lines.push(decorator.to_string());
}
if protobuf {
lines.push("@message".to_string());
}
lines.push(format!("model {} {{", sanitize_identifier(&model.name)));
for (idx, field) in model.fields.iter().enumerate() {
lines.push(emit_field(field, idx + 1, protobuf));
}
lines.push("}".to_string());
lines.join("\n")
}
fn emit_field(field: &CompiledField, index: usize, protobuf: bool) -> String {
let mut decorators = Vec::new();
if protobuf {
decorators.push(format!("@field({})", index));
}
for decorator in field.extensions.decorators() {
decorators.push(decorator.to_string());
}
if let Some(c) = &field.constraints {
decorators.extend(constraints_to_decorators(c));
}
let decorator_str = if decorators.is_empty() {
String::new()
} else {
format!("{} ", decorators.join(" "))
};
let optional_mark = if field.optional.unwrap_or(false) {
"?"
} else {
""
};
let type_str = type_to_string(&field.field_type, protobuf);
format!(
" {}{}{}: {};",
decorator_str,
sanitize_identifier(&field.name),
optional_mark,
type_str
)
}
fn constraints_to_decorators(c: &FieldConstraints) -> Vec<String> {
let mut decorators = Vec::new();
if let Some(v) = c.min_length {
decorators.push(format!("@minLength({})", v));
}
if let Some(v) = c.max_length {
decorators.push(format!("@maxLength({})", v));
}
if let Some(v) = c.min_value {
decorators.push(format!("@minValue({})", v));
}
if let Some(v) = c.max_value {
decorators.push(format!("@maxValue({})", v));
}
if let Some(v) = &c.pattern {
decorators.push(format!("@pattern(\"{}\")", escape_string(v)));
}
if let Some(v) = c.multiple_of {
decorators.push(format!("@multipleOf({})", v));
}
if let Some(v) = &c.format {
decorators.push(format!("@format(\"{}\")", v));
}
decorators
}
fn emit_operation(op: &odk_schema::CompiledOperation) -> String {
let mut lines = Vec::new();
if let Some(desc) = &op.description {
lines.push(format!("/** {} */", desc));
}
for directive in &op.directives {
let args_str = if directive.arguments.is_empty() {
String::new()
} else {
let args: Vec<String> = directive
.arguments
.iter()
.map(|(k, v)| format!("{}: {}", k, v))
.collect();
format!("({})", args.join(", "))
};
lines.push(format!("@{}{}", directive.name, args_str));
}
let params = if op.variables.is_empty() {
String::new()
} else {
let param_strs: Vec<String> = op
.variables
.iter()
.map(|v| {
let opt = if v.required { "" } else { "?" };
format!(
"{}{}: {}",
sanitize_identifier(&v.name),
opt,
type_to_string(&v.variable_type, false)
)
})
.collect();
param_strs.join(", ")
};
let return_type = type_to_string(&op.output, false);
lines.push(format!(
"op {}({}): {};",
sanitize_identifier(&op.name),
params,
return_type
));
lines.join("\n")
}
fn type_to_string(t: &CompiledType, protobuf: bool) -> String {
match t {
CompiledType::Scalar { name } => scalar_to_typespec(name, protobuf),
CompiledType::Array { items, .. } => format!("{}[]", type_to_string(items, protobuf)),
CompiledType::Ref { name } => sanitize_identifier(name),
CompiledType::Literal { value } => match value {
LiteralValue::String(s) => format!("\"{}\"", escape_string(s)),
LiteralValue::Number(n) => n.to_string(),
LiteralValue::Bool(b) => b.to_string(),
},
CompiledType::Union { variants } => variants
.iter()
.map(|v| type_to_string(v, protobuf))
.collect::<Vec<_>>()
.join(" | "),
CompiledType::Object { fields } => {
let field_strs: Vec<String> = fields
.iter()
.map(|f| {
let opt = if f.optional.unwrap_or(false) { "?" } else { "" };
format!(
"{}{}: {}",
f.name,
opt,
type_to_string(&f.field_type, protobuf)
)
})
.collect();
format!("{{ {} }}", field_strs.join("; "))
}
}
}
fn scalar_to_typespec(scalar: &ScalarType, protobuf: bool) -> String {
if protobuf {
match scalar {
ScalarType::Date
| ScalarType::Datetime
| ScalarType::UtcDateTime
| ScalarType::OffsetDateTime
| ScalarType::PlainDate
| ScalarType::PlainTime
| ScalarType::Duration
| ScalarType::Url
| ScalarType::Unknown => return "string".to_string(),
_ => {}
}
}
match scalar {
ScalarType::String => "string".to_string(),
ScalarType::Int32 => "int32".to_string(),
ScalarType::Int64 => "int64".to_string(),
ScalarType::Safeint => "safeint".to_string(),
ScalarType::Float32 => "float32".to_string(),
ScalarType::Float64 => "float64".to_string(),
ScalarType::Boolean => "boolean".to_string(),
ScalarType::Date => "plainDate".to_string(),
ScalarType::Datetime => "utcDateTime".to_string(),
ScalarType::UtcDateTime => "utcDateTime".to_string(),
ScalarType::OffsetDateTime => "offsetDateTime".to_string(),
ScalarType::PlainDate => "plainDate".to_string(),
ScalarType::PlainTime => "plainTime".to_string(),
ScalarType::Duration => "duration".to_string(),
ScalarType::Bytes => "bytes".to_string(),
ScalarType::Url => "url".to_string(),
ScalarType::Unknown => "unknown".to_string(),
}
}
fn sanitize_identifier(name: &str) -> String {
let mut sanitized: String = name
.chars()
.map(|c| {
if c.is_alphanumeric() || c == '_' {
c
} else {
'_'
}
})
.collect();
if sanitized.is_empty() {
sanitized = "_".to_string();
}
if sanitized.chars().next().unwrap().is_numeric() {
sanitized = format!("_{}", sanitized);
}
sanitized
}
fn is_valid_identifier(name: &str) -> bool {
if name.is_empty() {
return false;
}
let first = name.chars().next().unwrap();
if !first.is_alphabetic() && first != '_' {
return false;
}
name.chars().all(|c| c.is_alphanumeric() || c == '_')
}
fn escape_string(s: &str) -> String {
s.replace('\\', "\\\\").replace('"', "\\\"")
}
fn import_to_using_namespace(import: &str) -> Option<&'static str> {
match import {
"@typespec/http" => Some("TypeSpec.Http"),
"@typespec/rest" => Some("TypeSpec.Rest"),
"@typespec/openapi" => Some("TypeSpec.OpenAPI"),
"@typespec/openapi3" => Some("TypeSpec.OpenAPI"),
"@typespec/protobuf" => Some("TypeSpec.Protobuf"),
"@typespec/versioning" => Some("TypeSpec.Versioning"),
"@typespec/xml" => Some("TypeSpec.Xml"),
"@typespec/events" => Some("TypeSpec.Events"),
"@typespec/streams" => Some("TypeSpec.Streams"),
"@typespec/sse" => Some("TypeSpec.SSE"),
"@omnidotdev/typespec-drizzle" => Some("Drizzle"),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compile_allows_a_string_enum() {
let mut schema = CompiledSchema::new("S");
schema.add_union(string_enum("Status", &["draft", "published"]));
assert!(unsupported_feature(&schema).is_none());
}
#[test]
fn compile_rejects_a_data_carrying_union() {
let mut schema = CompiledSchema::new("S");
schema.add_union(CompiledUnion {
name: "OpNode".to_string(),
tag: Some("op".to_string()),
variants: vec![odk_schema::CompiledUnionVariant {
name: "cube".to_string(),
variant_type: Some(CompiledType::Object { fields: vec![] }),
}],
..Default::default()
});
let err = unsupported_feature(&schema).expect("tagged union should be rejected");
assert!(err.contains("OpNode") && err.contains("compile"), "{err}");
}
#[test]
fn test_simple_schema() {
let mut schema = CompiledSchema::new("TestAPI");
let mut user = CompiledModel::new("User");
user.add_field(CompiledField::new("id", CompiledType::string()));
user.add_field(CompiledField::new("name", CompiledType::string()).optional());
schema.add_model(user);
let tsp = compiled_to_typespec(&schema, "TestAPI");
assert!(tsp.contains("namespace TestAPI;"));
assert!(tsp.contains("model User {"));
assert!(tsp.contains("id: string;"));
assert!(tsp.contains("name?: string;"));
assert!(!tsp.contains("@jsonSchema"));
assert!(!tsp.contains("using TypeSpec"));
}
#[test]
fn test_schema_with_imports() {
let mut schema = CompiledSchema::new("TestAPI");
schema.add_import("@typespec/http");
schema.add_import("@omnidotdev/typespec-drizzle");
let tsp = compiled_to_typespec(&schema, "TestAPI");
assert!(tsp.contains("import \"@typespec/http\";"));
assert!(tsp.contains("import \"@omnidotdev/typespec-drizzle\";"));
assert!(tsp.contains("using TypeSpec.Http;"));
assert!(tsp.contains("using Drizzle;"));
}
#[test]
fn json_schema_target_scopes_emission_to_the_namespace() {
let mut schema = CompiledSchema::new("TestAPI");
schema.add_model(CompiledModel::new("User"));
let opts = EmitOptions {
json_schema: true,
..Default::default()
};
let tsp = compiled_to_typespec_with_options(&schema, "TestAPI", &opts);
assert!(tsp.contains("import \"@typespec/json-schema\";"));
assert!(tsp.contains("@TypeSpec.JsonSchema.jsonSchema"));
assert!(!tsp.contains("using TypeSpec.JsonSchema;"));
}
#[test]
fn test_safeint_from_ts_ir_round_trips_to_typespec() {
let schema: CompiledSchema = serde_json::from_str(
r#"{"name":"T","models":[{"name":"M","fields":[
{"name":"seed","type":{"kind":"scalar","name":"safeint"},"optional":true}
]}],"unions":[]}"#,
)
.expect("safeint IR should deserialize");
let tsp = compiled_to_typespec(&schema, "T");
assert!(tsp.contains("seed?: safeint;"), "got:\n{tsp}");
}
fn protobuf_schema() -> CompiledSchema {
let mut schema = CompiledSchema::new("Blog");
let mut post = CompiledModel::new("Post");
post.add_field(CompiledField::new("id", CompiledType::int64()));
post.add_field(CompiledField::new("title", CompiledType::string()));
schema.add_model(post);
schema
}
#[test]
fn test_protobuf_injects_package_on_namespace() {
let schema = protobuf_schema();
let opts = EmitOptions {
protobuf: Some(ProtobufOptions {
package: "blog".to_string(),
}),
..Default::default()
};
let tsp = compiled_to_typespec_with_options(&schema, "Blog", &opts);
assert!(
tsp.contains("@package({ name: \"blog\" })\nnamespace Blog;"),
"expected @package decorator before namespace, got:\n{tsp}"
);
}
#[test]
fn test_protobuf_auto_imports_and_using() {
let schema = protobuf_schema();
let opts = EmitOptions {
protobuf: Some(ProtobufOptions {
package: "blog".to_string(),
}),
..Default::default()
};
let tsp = compiled_to_typespec_with_options(&schema, "Blog", &opts);
assert!(tsp.contains("import \"@typespec/protobuf\";"));
assert!(tsp.contains("using TypeSpec.Protobuf;"));
}
#[test]
fn test_protobuf_decorates_models_and_fields() {
let schema = protobuf_schema();
let opts = EmitOptions {
protobuf: Some(ProtobufOptions {
package: "blog".to_string(),
}),
..Default::default()
};
let tsp = compiled_to_typespec_with_options(&schema, "Blog", &opts);
assert!(tsp.contains("@message\nmodel Post {"), "got:\n{tsp}");
assert!(tsp.contains("@field(1) id: int64;"), "got:\n{tsp}");
assert!(tsp.contains("@field(2) title: string;"), "got:\n{tsp}");
}
#[test]
fn test_protobuf_does_not_duplicate_existing_import() {
let mut schema = protobuf_schema();
schema.add_import("@typespec/protobuf");
let opts = EmitOptions {
protobuf: Some(ProtobufOptions {
package: "blog".to_string(),
}),
..Default::default()
};
let tsp = compiled_to_typespec_with_options(&schema, "Blog", &opts);
assert_eq!(
tsp.matches("import \"@typespec/protobuf\";").count(),
1,
"protobuf import must not be duplicated, got:\n{tsp}"
);
}
#[test]
fn test_emit_options_none_without_protobuf_target() {
let emitters = vec![
"@typespec/openapi3".to_string(),
"@typespec/json-schema".to_string(),
];
let opts = emit_options_for(&emitters, "Blog");
assert!(opts.protobuf.is_none());
}
#[test]
fn test_emit_options_derives_lowercase_package_from_namespace() {
let emitters = vec!["@typespec/protobuf".to_string()];
let opts = emit_options_for(&emitters, "BlogAPI");
let pb = opts.protobuf.expect("protobuf options expected");
assert_eq!(pb.package, "blogapi");
}
fn string_enum(name: &str, members: &[&str]) -> CompiledUnion {
CompiledUnion {
name: name.to_string(),
variants: members
.iter()
.map(|m| odk_schema::CompiledUnionVariant {
name: m.to_string(),
variant_type: Some(CompiledType::Literal {
value: LiteralValue::String(m.to_string()),
}),
})
.collect(),
tag: None,
rename_all: None,
description: None,
extensions: Default::default(),
source: None,
}
}
#[test]
fn test_protobuf_enums_use_integer_values() {
let mut schema = CompiledSchema::new("Blog");
schema.add_union(string_enum("Status", &["draft", "published"]));
let opts = EmitOptions {
protobuf: Some(ProtobufOptions {
package: "blog".to_string(),
}),
..Default::default()
};
let tsp = compiled_to_typespec_with_options(&schema, "Blog", &opts);
assert!(tsp.contains("enum Status {"), "got:\n{tsp}");
assert!(tsp.contains("draft: 0,"), "got:\n{tsp}");
assert!(tsp.contains("published: 1,"), "got:\n{tsp}");
}
#[test]
fn test_string_enum_keeps_string_form_without_protobuf() {
let mut schema = CompiledSchema::new("Blog");
schema.add_union(string_enum("Status", &["draft", "published"]));
let tsp = compiled_to_typespec(&schema, "Blog");
assert!(tsp.contains(" draft,"), "got:\n{tsp}");
assert!(!tsp.contains("draft: 0"), "got:\n{tsp}");
}
#[test]
fn test_protobuf_maps_temporal_scalars_to_string() {
let mut schema = CompiledSchema::new("Blog");
let mut event = CompiledModel::new("Event");
event.add_field(CompiledField::new(
"createdAt",
CompiledType::Scalar {
name: ScalarType::UtcDateTime,
},
));
schema.add_model(event);
let opts = EmitOptions {
protobuf: Some(ProtobufOptions {
package: "blog".to_string(),
}),
..Default::default()
};
let tsp = compiled_to_typespec_with_options(&schema, "Blog", &opts);
assert!(tsp.contains("@field(1) createdAt: string;"), "got:\n{tsp}");
}
#[test]
fn test_temporal_scalar_unchanged_without_protobuf() {
let mut schema = CompiledSchema::new("Blog");
let mut event = CompiledModel::new("Event");
event.add_field(CompiledField::new(
"createdAt",
CompiledType::Scalar {
name: ScalarType::UtcDateTime,
},
));
schema.add_model(event);
let tsp = compiled_to_typespec(&schema, "Blog");
assert!(tsp.contains("createdAt: utcDateTime;"), "got:\n{tsp}");
}
#[test]
fn test_no_protobuf_decorators_without_option() {
let schema = protobuf_schema();
let tsp = compiled_to_typespec(&schema, "Blog");
assert!(!tsp.contains("@package"));
assert!(!tsp.contains("@message"));
assert!(!tsp.contains("@field("));
assert!(!tsp.contains("import \"@typespec/protobuf\";"));
}
}