Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phil/ts codegen fix #1855

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/derive-typescript/src/codegen/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Mapper {
return AST::Unknown;
}
// Is this an enum? Just emit the variants.
if let Some(enum_) = &shape.enum_ {
if let Some(enum_) = shape.enum_.as_ref().filter(|e| !e.is_empty()) {
return AST::Union {
variants: enum_
.iter()
Expand Down
1 change: 1 addition & 0 deletions crates/derive-typescript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ fn validate(validate: derive::request::Validate) -> anyhow::Result<derive::respo

let types_url = format!("{project_root}/{GENERATED_PREFIX}/{}.ts", collection.name);
let types_content = codegen::types_ts(&collection, &transforms);
tracing::debug!(%types_content, "generated TS types");

let mut generated_files: Vec<(String, String)> = vec![
(types_url.clone(), types_content.clone()),
Expand Down
78 changes: 78 additions & 0 deletions crates/doc/src/shape/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,84 @@ mod test {
);
}

// This test documents the behavior in the corneer case where the
// intersection of two object schemas forbids an enum property.
#[test]
fn test_enum_property_empty_intersection() {
let shape = shape_from(
r#"{
"type": "object",
"allOf": [
{
"properties": {
"foo": {
"type": "integer",
"enum": [1, 2, 3]
}
}
},
{
"properties": {
"bar": { "type": "string" }
},
"additionalProperties": false
}
]
}"#,
);

let foo_shape = shape
.object
.properties
.iter()
.find(|p| p.name.as_ref() == "foo")
.unwrap();

// The `type_` is empty because the property is not allowed to be present.
// Note that the `enum_` is still `Some`, though the set of values is empty.
insta::assert_debug_snapshot!(foo_shape, @r###"
ObjProperty {
name: "foo",
is_required: false,
shape: Shape {
type_: ,
enum_: Some(
[],
),
title: None,
description: None,
reduction: Unset,
provenance: Inline,
default: None,
secret: None,
annotations: {},
array: ArrayShape {
additional_items: None,
max_items: None,
min_items: 0,
tuple: [],
},
numeric: NumericShape {
minimum: None,
maximum: None,
},
object: ObjShape {
additional_properties: None,
pattern_properties: [],
properties: [],
},
string: StringShape {
content_encoding: None,
content_type: None,
format: None,
max_length: None,
min_length: 0,
},
},
}
"###);
}

#[test]
fn test_enum_type_extraction() {
assert_eq!(
Expand Down
Loading