|
3 | 3 | //! Defines input structures for `schema!` and `schema_type!` macros. |
4 | 4 |
|
5 | 5 | use syn::{ |
6 | | - Ident, LitStr, Token, Type, bracketed, parenthesized, |
| 6 | + bracketed, parenthesized, |
7 | 7 | parse::{Parse, ParseStream}, |
8 | 8 | punctuated::Punctuated, |
| 9 | + Ident, LitStr, Token, Type, |
9 | 10 | }; |
10 | 11 |
|
11 | 12 | /// Input for the schema! macro |
@@ -90,6 +91,7 @@ impl Parse for SchemaInput { |
90 | 91 | /// Or: `schema_type!(NewTypeName from SourceType, ignore)` - skip Schema derive |
91 | 92 | /// Or: `schema_type!(NewTypeName from SourceType, name = "CustomName")` - custom `OpenAPI` name |
92 | 93 | /// Or: `schema_type!(NewTypeName from SourceType, rename_all = "camelCase")` - serde `rename_all` |
| 94 | +#[allow(clippy::struct_excessive_bools)] |
93 | 95 | pub struct SchemaTypeInput { |
94 | 96 | /// The new type name to generate |
95 | 97 | pub new_type: Ident, |
@@ -123,6 +125,9 @@ pub struct SchemaTypeInput { |
123 | 125 | /// Whether to generate a multipart/form-data struct (derives `TryFromMultipart` instead of serde) |
124 | 126 | /// Use `multipart` bare keyword to set this to true. |
125 | 127 | pub multipart: bool, |
| 128 | + /// Whether to omit fields that have database defaults (sea_orm `default_value` or `primary_key`). |
| 129 | + /// Use `omit_default` bare keyword to set this to true. |
| 130 | + pub omit_default: bool, |
126 | 131 | } |
127 | 132 |
|
128 | 133 | /// Mode for the `partial` keyword in `schema_type`! |
@@ -204,6 +209,7 @@ impl Parse for SchemaTypeInput { |
204 | 209 | let mut schema_name = None; |
205 | 210 | let mut rename_all = None; |
206 | 211 | let mut multipart = false; |
| 212 | + let mut omit_default = false; |
207 | 213 |
|
208 | 214 | // Parse optional parameters |
209 | 215 | while input.peek(Token![,]) { |
@@ -293,11 +299,15 @@ impl Parse for SchemaTypeInput { |
293 | 299 | // bare `multipart` - derive TryFromMultipart instead of serde |
294 | 300 | multipart = true; |
295 | 301 | } |
| 302 | + "omit_default" => { |
| 303 | + // bare `omit_default` - omit fields with database defaults |
| 304 | + omit_default = true; |
| 305 | + } |
296 | 306 | _ => { |
297 | 307 | return Err(syn::Error::new( |
298 | 308 | ident.span(), |
299 | 309 | format!( |
300 | | - "unknown parameter: `{ident_str}`. Expected `omit`, `pick`, `rename`, `add`, `clone`, `partial`, `ignore`, `name`, `rename_all`, or `multipart`" |
| 310 | + "unknown parameter: `{ident_str}`. Expected `omit`, `pick`, `rename`, `add`, `clone`, `partial`, `ignore`, `name`, `rename_all`, `multipart`, or `omit_default`" |
301 | 311 | ), |
302 | 312 | )); |
303 | 313 | } |
@@ -325,6 +335,7 @@ impl Parse for SchemaTypeInput { |
325 | 335 | schema_name, |
326 | 336 | rename_all, |
327 | 337 | multipart, |
| 338 | + omit_default, |
328 | 339 | }) |
329 | 340 | } |
330 | 341 | } |
@@ -699,4 +710,34 @@ mod tests { |
699 | 710 | assert!(input.multipart); |
700 | 711 | assert!(matches!(input.partial, Some(PartialMode::All))); |
701 | 712 | } |
| 713 | + |
| 714 | + #[test] |
| 715 | + fn test_parse_schema_type_input_with_omit_default() { |
| 716 | + let tokens = quote::quote!(CreateUser from Model, omit_default); |
| 717 | + let input: SchemaTypeInput = syn::parse2(tokens).unwrap(); |
| 718 | + assert!(input.omit_default); |
| 719 | + } |
| 720 | + |
| 721 | + #[test] |
| 722 | + fn test_parse_schema_type_input_with_omit_default_and_omit() { |
| 723 | + let tokens = quote::quote!(CreateUser from Model, omit_default, omit = ["password"]); |
| 724 | + let input: SchemaTypeInput = syn::parse2(tokens).unwrap(); |
| 725 | + assert!(input.omit_default); |
| 726 | + assert_eq!(input.omit.unwrap(), vec!["password"]); |
| 727 | + } |
| 728 | + |
| 729 | + #[test] |
| 730 | + fn test_parse_schema_type_input_with_omit_default_and_pick() { |
| 731 | + let tokens = quote::quote!(CreateUser from Model, omit_default, pick = ["name", "email"]); |
| 732 | + let input: SchemaTypeInput = syn::parse2(tokens).unwrap(); |
| 733 | + assert!(input.omit_default); |
| 734 | + assert_eq!(input.pick.unwrap(), vec!["name", "email"]); |
| 735 | + } |
| 736 | + |
| 737 | + #[test] |
| 738 | + fn test_parse_schema_type_input_omit_default_defaults_to_false() { |
| 739 | + let tokens = quote::quote!(CreateUser from User); |
| 740 | + let input: SchemaTypeInput = syn::parse2(tokens).unwrap(); |
| 741 | + assert!(!input.omit_default); |
| 742 | + } |
702 | 743 | } |
0 commit comments