From aeb7152e4276f5157d7f72b795711195428295ea Mon Sep 17 00:00:00 2001 From: Kurt Wolf Date: Thu, 15 Feb 2024 12:32:45 -0500 Subject: [PATCH] fix test with serde(default) --- core/src/extractor/record.rs | 2 +- libninja/src/rust/lower_hir.rs | 2 +- libninja/tests/all_of/main.rs | 9 ++++----- libninja/tests/all_of/transaction.rs | 14 +++++++------- mir_rust/src/import.rs | 2 +- mir_rust/src/lib.rs | 2 +- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/core/src/extractor/record.rs b/core/src/extractor/record.rs index b240bd4..8b9174b 100644 --- a/core/src/extractor/record.rs +++ b/core/src/extractor/record.rs @@ -131,7 +131,7 @@ fn create_record_from_all_of(name: &str, all_of: &[ReferenceOr], schema_ let props = item.properties(); for (name, schema) in props { let mut field = create_field(schema, spec); - if !item.required().iter().any(|s| s == name) { + if !field.ty.is_iterable() && !item.required().iter().any(|s| s == name) { field.optional = true; } fields.insert(name.to_string(), field); diff --git a/libninja/src/rust/lower_hir.rs b/libninja/src/rust/lower_hir.rs index ea4bfba..c47f2c8 100644 --- a/libninja/src/rust/lower_hir.rs +++ b/libninja/src/rust/lower_hir.rs @@ -23,7 +23,6 @@ impl FieldExt for HirField { let mut decorators = Vec::new(); let rust_ident = name.to_rust_ident(); if rust_ident.0 != name { - let name = &name; if self.flatten { decorators.push(quote! { #[serde(flatten)] @@ -39,6 +38,7 @@ impl FieldExt for HirField { }); } } + dbg!(self.optional); if self.optional { decorators.push(quote! { #[serde(default, skip_serializing_if = "Option::is_none")] diff --git a/libninja/tests/all_of/main.rs b/libninja/tests/all_of/main.rs index e7f32a2..58cd75a 100644 --- a/libninja/tests/all_of/main.rs +++ b/libninja/tests/all_of/main.rs @@ -1,10 +1,10 @@ use openapiv3::{OpenAPI, Schema}; use pretty_assertions::assert_eq; -/// Tests that the `allOf` keyword is handled correctly. -use ln_core::{ConfigFlags}; use hir::{HirSpec, Record}; -use ln_core::extractor::{extract_api_operations, extract_records}; +/// Tests that the `allOf` keyword is handled correctly. +use ln_core::ConfigFlags; +use ln_core::extractor::extract_records; const TRANSACTION: &str = include_str!("transaction.yaml"); const TRANSACTION_RS: &str = include_str!("transaction.rs"); @@ -15,8 +15,7 @@ const RESTRICTION_BACS_RS: &str = include_str!("restriction_bacs.rs"); fn record_for_schema(name: &str, schema: &str, spec: &OpenAPI) -> Record { let schema = serde_yaml::from_str::(schema).unwrap(); - let mut record = ln_core::extractor::create_record(name, &schema, spec); - record + ln_core::extractor::create_record(name, &schema, spec) } fn formatted_code(record: Record, spec: &HirSpec) -> String { diff --git a/libninja/tests/all_of/transaction.rs b/libninja/tests/all_of/transaction.rs index 1c83cc3..456f2ed 100644 --- a/libninja/tests/all_of/transaction.rs +++ b/libninja/tests/all_of/transaction.rs @@ -4,20 +4,20 @@ pub struct Transaction { #[serde(flatten)] pub transaction_base: TransactionBase, ///The date that the transaction was authorized. Dates are returned in an [ISO 8601](https://wikipedia.org/wiki/ISO_8601) format ( `YYYY-MM-DD` ). - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default, skip_serializing_if = "Option::is_none")] pub authorized_date: Option, /**Date and time when a transaction was authorized in [ISO 8601](https://wikipedia.org/wiki/ISO_8601) format ( `YYYY-MM-DDTHH:mm:ssZ` ). This field is returned for select financial institutions and comes as provided by the institution. It may contain default time values (such as 00:00:00).*/ - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default, skip_serializing_if = "Option::is_none")] pub authorized_datetime: Option>, ///The counterparties present in the transaction. Counterparties, such as the financial institutions, are extracted by Plaid from the raw description. - #[serde(skip_serializing_if = "Option::is_none")] - pub counterparties: Option>, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub counterparties: Vec, /**Date and time when a transaction was posted in [ISO 8601](https://wikipedia.org/wiki/ISO_8601) format ( `YYYY-MM-DDTHH:mm:ssZ` ). This field is returned for select financial institutions and comes as provided by the institution. It may contain default time values (such as 00:00:00).*/ - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default, skip_serializing_if = "Option::is_none")] pub datetime: Option>, /**The channel used to make a payment. `online:` transactions that took place online. @@ -28,10 +28,10 @@ This field is returned for select financial institutions and comes as provided b This field replaces the `transaction_type` field.*/ pub payment_channel: String, - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default, skip_serializing_if = "Option::is_none")] pub personal_finance_category: Option, ///A link to the icon associated with the primary personal finance category. The logo will always be 100x100 pixels. - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default, skip_serializing_if = "Option::is_none")] pub personal_finance_category_icon_url: Option, pub transaction_code: String, } diff --git a/mir_rust/src/import.rs b/mir_rust/src/import.rs index c39d90b..a373da2 100644 --- a/mir_rust/src/import.rs +++ b/mir_rust/src/import.rs @@ -12,7 +12,7 @@ impl ToRustCode for Import { alias, imports, vis, - feature, + .. } = import; if path.ends_with('*') { let path = syn::parse_str::(&path[..path.len() - 3]).unwrap(); diff --git a/mir_rust/src/lib.rs b/mir_rust/src/lib.rs index f7c600a..882a0e4 100644 --- a/mir_rust/src/lib.rs +++ b/mir_rust/src/lib.rs @@ -35,7 +35,7 @@ impl ToRustCode for Function { annotations, ret, public, - generic, + .. } = self; let annotations = annotations .into_iter()