Skip to content

Commit 90c3a50

Browse files
authored
feat(rust/signed-doc): Not required content-type during decoding (#534)
* wip * fix clippy
1 parent 2150630 commit 90c3a50

File tree

6 files changed

+19
-44
lines changed

6 files changed

+19
-44
lines changed

rust/signed_doc/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl ContentBuilder {
9292
json: &serde_json::Value,
9393
) -> anyhow::Result<SignaturesBuilder> {
9494
anyhow::ensure!(
95-
self.metadata.content_type()? == ContentType::Json,
95+
self.metadata.content_type() == Some(ContentType::Json),
9696
"Already set metadata field `content-type` is not JSON value"
9797
);
9898

rust/signed_doc/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,8 @@ impl CatalystSignedDocument {
137137
}
138138

139139
/// Return document `ContentType`.
140-
///
141-
/// # Errors
142-
/// - Missing 'content-type' field.
143-
pub fn doc_content_type(&self) -> anyhow::Result<ContentType> {
140+
#[must_use]
141+
pub fn doc_content_type(&self) -> Option<ContentType> {
144142
self.inner.metadata.content_type()
145143
}
146144

rust/signed_doc/src/metadata/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ impl Metadata {
6767
}
6868

6969
/// Returns the Document Content Type, if any.
70-
///
71-
/// # Errors
72-
/// - Missing 'content-type' field.
73-
pub fn content_type(&self) -> anyhow::Result<ContentType> {
70+
pub fn content_type(&self) -> Option<ContentType> {
7471
self.0
7572
.get(&SupportedLabel::ContentType)
7673
.and_then(SupportedField::try_as_content_type_ref)
7774
.copied()
78-
.ok_or(anyhow::anyhow!("Missing 'content-type' field"))
7975
}
8076

8177
/// Returns the Document Content Encoding, if any.
@@ -180,9 +176,6 @@ impl Metadata {
180176
if metadata.doc_ver().is_err() {
181177
report.missing_field("ver", REPORT_CONTEXT);
182178
}
183-
if metadata.content_type().is_err() {
184-
report.missing_field("content-type", REPORT_CONTEXT);
185-
}
186179

187180
Ok(metadata)
188181
}
@@ -224,7 +217,7 @@ impl Display for Metadata {
224217
writeln!(f, " type: {:?},", self.doc_type().ok())?;
225218
writeln!(f, " id: {:?},", self.doc_id().ok())?;
226219
writeln!(f, " ver: {:?},", self.doc_ver().ok())?;
227-
writeln!(f, " content_type: {:?},", self.content_type().ok())?;
220+
writeln!(f, " content_type: {:?},", self.content_type())?;
228221
writeln!(f, " content_encoding: {:?},", self.content_encoding())?;
229222
writeln!(f, " additional_fields: {{")?;
230223
writeln!(f, " ref: {:?}", self.doc_ref())?;

rust/signed_doc/src/validator/rules/content.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl ContentRule {
6262
};
6363

6464
let template_validator = |template_doc: &CatalystSignedDocument| {
65-
let Ok(template_content_type) = template_doc.doc_content_type() else {
65+
let Some(template_content_type) = template_doc.doc_content_type() else {
6666
doc.report().missing_field(
6767
"content-type",
6868
&format!("{context}, referenced document must have a content-type field"),

rust/signed_doc/src/validator/rules/content_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl ContentTypeRule {
2222
doc: &CatalystSignedDocument,
2323
) -> anyhow::Result<bool> {
2424
if let Self::NotSpecified = &self {
25-
if let Ok(content_type) = doc.doc_content_type() {
25+
if let Some(content_type) = doc.doc_content_type() {
2626
doc.report().unknown_field(
2727
"content-type",
2828
content_type.to_string().as_str(),
@@ -32,7 +32,7 @@ impl ContentTypeRule {
3232
}
3333
}
3434
if let Self::Specified { exp } = &self {
35-
let Ok(content_type) = doc.doc_content_type() else {
35+
let Some(content_type) = doc.doc_content_type() else {
3636
doc.report().missing_field(
3737
"content-type",
3838
"Cannot get a content type field during the field validation",

rust/signed_doc/tests/decoding.rs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ fn signed_doc_with_missing_header_field_case(field: &'static str) -> TestCase {
158158
// protected headers (metadata fields)
159159
e.bytes({
160160
let mut p_headers = Encoder::new(Vec::new());
161-
p_headers.map(4)?;
162-
if field != "content-type" {
163-
p_headers.u8(3)?.encode(ContentType::Json)?;
164-
}
161+
p_headers.map(3)?;
165162
if field != "type" {
166163
p_headers.str("type")?.encode(&doc_type)?;
167164
}
@@ -198,9 +195,6 @@ fn signed_doc_with_missing_header_field_case(field: &'static str) -> TestCase {
198195
valid_doc: false,
199196
post_checks: Some(Box::new({
200197
move |doc| {
201-
if field == "content-type" {
202-
anyhow::ensure!(doc.doc_meta().content_type().is_err());
203-
}
204198
if field == "type" {
205199
anyhow::ensure!(doc.doc_meta().doc_type().is_err());
206200
}
@@ -234,17 +228,10 @@ fn signed_doc_with_random_header_field_case(field: &'static str) -> TestCase {
234228
let mut rand_buf = [0u8; 128];
235229
rng.try_fill(&mut rand_buf)?;
236230

237-
let is_required_header = ["content-type", "type", "id", "ver"]
238-
.iter()
239-
.any(|v| v == &field);
231+
let is_required_header = ["type", "id", "ver"].iter().any(|v| v == &field);
240232

241233
let mut p_headers = Encoder::new(Vec::new());
242-
p_headers.map(if is_required_header { 4 } else { 5 })?;
243-
if field == "content-type" {
244-
p_headers.u8(3)?.encode_with(rand_buf, &mut ())?;
245-
} else {
246-
p_headers.u8(3)?.encode(ContentType::Json)?;
247-
}
234+
p_headers.map(if is_required_header { 3 } else { 4 })?;
248235
if field == "type" {
249236
p_headers.str("type")?.encode_with(rand_buf, &mut ())?;
250237
} else {
@@ -287,6 +274,7 @@ fn signed_doc_with_random_header_field_case(field: &'static str) -> TestCase {
287274
valid_doc: false,
288275
post_checks: Some(Box::new({
289276
move |doc| {
277+
anyhow::ensure!(doc.doc_meta().content_type().is_none());
290278
anyhow::ensure!(doc.doc_meta().content_encoding().is_none());
291279
anyhow::ensure!(doc.doc_meta().doc_ref().is_none());
292280
anyhow::ensure!(doc.doc_meta().template().is_none());
@@ -295,9 +283,6 @@ fn signed_doc_with_random_header_field_case(field: &'static str) -> TestCase {
295283
anyhow::ensure!(doc.doc_meta().parameters().is_none());
296284
anyhow::ensure!(doc.doc_meta().collaborators().is_empty());
297285

298-
if field == "content-type" {
299-
anyhow::ensure!(doc.doc_meta().content_type().is_err());
300-
}
301286
if field == "type" {
302287
anyhow::ensure!(doc.doc_meta().doc_type().is_err());
303288
}
@@ -594,7 +579,7 @@ fn signed_doc_with_minimal_metadata_fields_case() -> TestCase {
594579
anyhow::ensure!(doc.doc_type()? == &doc_type);
595580
anyhow::ensure!(doc.doc_id()? == uuid_v7);
596581
anyhow::ensure!(doc.doc_ver()? == uuid_v7);
597-
anyhow::ensure!(doc.doc_content_type()? == ContentType::Json);
582+
anyhow::ensure!(doc.doc_content_type() == Some(ContentType::Json));
598583
anyhow::ensure!(
599584
doc.encoded_content() == serde_json::to_vec(&serde_json::Value::Null)?
600585
);
@@ -682,7 +667,7 @@ fn signed_doc_with_complete_metadata_fields_case() -> TestCase {
682667
anyhow::ensure!(doc.doc_meta().doc_ref() == Some(&refs));
683668
anyhow::ensure!(doc.doc_meta().template() == Some(&refs));
684669
anyhow::ensure!(doc.doc_meta().reply() == Some(&refs));
685-
anyhow::ensure!(doc.doc_content_type()? == ContentType::Json);
670+
anyhow::ensure!(doc.doc_content_type() == Some(ContentType::Json));
686671
anyhow::ensure!(doc.encoded_content() == serde_json::to_vec(&serde_json::Value::Null)?);
687672
anyhow::ensure!(doc.kids().len() == 1);
688673
anyhow::ensure!(!doc.is_deprecated()?);
@@ -735,7 +720,7 @@ fn minimally_valid_tagged_signed_doc() -> TestCase {
735720
anyhow::ensure!(doc.doc_type()? == &doc_type);
736721
anyhow::ensure!(doc.doc_id()? == uuid_v7);
737722
anyhow::ensure!(doc.doc_ver()? == uuid_v7);
738-
anyhow::ensure!(doc.doc_content_type()? == ContentType::Json);
723+
anyhow::ensure!(doc.doc_content_type() == Some(ContentType::Json));
739724
anyhow::ensure!(doc.doc_meta().doc_ref().is_none());
740725
anyhow::ensure!(doc.doc_meta().template().is_none());
741726
anyhow::ensure!(doc.doc_meta().reply().is_none());
@@ -791,7 +776,7 @@ fn minimally_valid_untagged_signed_doc() -> TestCase {
791776
anyhow::ensure!(doc.doc_type()? == &doc_type);
792777
anyhow::ensure!(doc.doc_id()? == uuid_v7);
793778
anyhow::ensure!(doc.doc_ver()? == uuid_v7);
794-
anyhow::ensure!(doc.doc_content_type()? == ContentType::Json);
779+
anyhow::ensure!(doc.doc_content_type() == Some(ContentType::Json));
795780
anyhow::ensure!(doc.doc_meta().doc_ref().is_none());
796781
anyhow::ensure!(doc.doc_meta().template().is_none());
797782
anyhow::ensure!(doc.doc_meta().reply().is_none());
@@ -1100,7 +1085,7 @@ fn signed_doc_with_non_strict_deterministic_decoding_wrong_order() -> TestCase {
11001085
anyhow::ensure!(doc.doc_type()? == &doc_type);
11011086
anyhow::ensure!(doc.doc_id()? == uuid_v7);
11021087
anyhow::ensure!(doc.doc_ver()? == uuid_v7);
1103-
anyhow::ensure!(doc.doc_content_type()? == ContentType::Json);
1088+
anyhow::ensure!(doc.doc_content_type() == Some(ContentType::Json));
11041089
anyhow::ensure!(
11051090
doc.encoded_content() == serde_json::to_vec(&serde_json::Value::Null)?
11061091
);
@@ -1159,7 +1144,7 @@ fn signed_doc_with_non_supported_metadata_invalid() -> TestCase {
11591144
anyhow::ensure!(doc.doc_type()? == &doc_type);
11601145
anyhow::ensure!(doc.doc_id()? == uuid_v7);
11611146
anyhow::ensure!(doc.doc_ver()? == uuid_v7);
1162-
anyhow::ensure!(doc.doc_content_type()? == ContentType::Json);
1147+
anyhow::ensure!(doc.doc_content_type() == Some(ContentType::Json));
11631148
anyhow::ensure!(
11641149
doc.encoded_content() == serde_json::to_vec(&serde_json::Value::Null)?
11651150
);
@@ -1227,7 +1212,7 @@ fn signed_doc_with_kid_in_id_form_invalid() -> TestCase {
12271212
anyhow::ensure!(doc.doc_type()? == &doc_type);
12281213
anyhow::ensure!(doc.doc_id()? == uuid_v7);
12291214
anyhow::ensure!(doc.doc_ver()? == uuid_v7);
1230-
anyhow::ensure!(doc.doc_content_type()? == ContentType::Json);
1215+
anyhow::ensure!(doc.doc_content_type() == Some(ContentType::Json));
12311216
anyhow::ensure!(
12321217
doc.encoded_content() == serde_json::to_vec(&serde_json::Value::Null)?
12331218
);
@@ -1323,7 +1308,6 @@ fn catalyst_signed_doc_decoding_test() {
13231308
signed_doc_with_valid_alias_case("brand_id"),
13241309
signed_doc_with_valid_alias_case("campaign_id"),
13251310
signed_doc_with_valid_alias_case("parameters"),
1326-
signed_doc_with_missing_header_field_case("content-type"),
13271311
signed_doc_with_missing_header_field_case("type"),
13281312
signed_doc_with_missing_header_field_case("id"),
13291313
signed_doc_with_missing_header_field_case("ver"),

0 commit comments

Comments
 (0)