| 1 | use craterust-analyzer cargo schemars 1.2.2 crate/::preluderust-analyzer cargo schemars 1.2.2 prelude/::*; |
| 2 | use pretty_assertionsrust-analyzer cargo pretty_assertions 1.4.1 crate/::assert_eqrust-analyzer cargo pretty_assertions 1.4.1 assert_eq!; |
| 3 | use schemarsrust-analyzer cargo schemars 1.2.2 crate/::Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#; |
| 5 | |
| 6 | // This test ensures that `extend` and `transform` attributes are applied after other attributes, |
| 7 | // and transforms are applied in the order they are defined. |
| 8 | |
| 9 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(JsonSchemarust-analyzer cargo schemars_derive 1.2.2 JsonSchema:, Deserializerust-analyzer cargo serde_derive 1.0.229 Deserialize:, Serializerust-analyzer cargo serde_derive 1.0.229 Serialize:)] |
| 10 | #[schemars(transform = suffix_descriptionrust-analyzer cargo schemars 1.2.2 attr_order/suffix_description()., description = "[Overwritten]", extend("description" = "The enum", "suffix" = "..."))] |
| 11 | #[serde(untagged)] |
| 12 | enum Untaggedrust-analyzer cargo schemars 1.2.2 attr_order/Untagged# { |
| 13 | #[schemarsrust-analyzer cargo schemars_derive 1.2.2 schemars:(transform = suffix_descriptionrust-analyzer cargo schemars 1.2.2 attr_order/suffix_description()., description = "The variant", extend("suffix" = "?"))] |
| 14 | Arust-analyzer cargo schemars 1.2.2 attr_order/Untagged#A# { |
| 15 | #[schemars(transform = suffix_descriptionrust-analyzer cargo schemars 1.2.2 attr_order/suffix_description()., description = "The field", extend("suffix" = "!"))] |
| 16 | #[schemars(range(min = 1), transform = remove_minimum_and_defaultrust-analyzer cargo schemars 1.2.2 attr_order/remove_minimum_and_default().)] |
| 17 | #[serde(default)] |
| 18 | irust-analyzer cargo schemars 1.2.2 attr_order/Untagged#A#i.: i32, |
| 19 | }, |
| 20 | } |
| 21 | |
| 22 | fn suffix_descriptionrust-analyzer cargo schemars 1.2.2 attr_order/suffix_description().(schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) { |
| 23 | let minimum = schema.getrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get().("minimum").maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().); |
| 24 | |
| 25 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Stringrust-analyzer cargo serde_json 1.0.151 value/Value#String#(suffix)) = schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("suffix") else { |
| 26 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("expected `suffix` to be present and a string"); |
| 27 | }; |
| 28 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Stringrust-analyzer cargo serde_json 1.0.151 value/Value#String#(description)) = schema.get_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get_mut().("description") else { |
| 29 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("expected `description` to be present and a string"); |
| 30 | }; |
| 31 | |
| 32 | description.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&suffix); |
| 33 | |
| 34 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(minimum) = minimum { |
| 35 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(description, " (At least {})", minimum).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | fn remove_minimum_and_defaultrust-analyzer cargo schemars 1.2.2 attr_order/remove_minimum_and_default().(schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) { |
| 40 | schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("minimum"); |
| 41 | schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("default"); |
| 42 | } |
| 43 | |
| 44 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 45 | fn attributes_applied_in_orderrust-analyzer cargo schemars 1.2.2 attr_order/attributes_applied_in_order().() { |
| 46 | testrust-analyzer cargo schemars 1.2.2 test!!(Untaggedrust-analyzer cargo schemars 1.2.2 attr_order/Untagged#).assert_snapshotrust-analyzer cargo schemars 1.2.2 test_helper/impl#[`TestHelper<T>`]assert_snapshot().().customrust-analyzer cargo schemars 1.2.2 test_helper/impl#[`TestHelper<T>`]custom().(|schema, _| { |
| 47 | assert_eqrust-analyzer cargo pretty_assertions 1.4.1 assert_eq!!(schema.pointerrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]pointer().("/description"), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&jsonrust-analyzer cargo serde_json 1.0.151 macros/json!!("The enum..."))); |
| 48 | assert_eqrust-analyzer cargo pretty_assertions 1.4.1 assert_eq!!( |
| 49 | schema.pointerrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]pointer().("/anyOf/0/description"), |
| 50 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&jsonrust-analyzer cargo serde_json 1.0.151 macros/json!!("The variant?")) |
| 51 | ); |
| 52 | assert_eqrust-analyzer cargo pretty_assertions 1.4.1 assert_eq!!( |
| 53 | schema.pointerrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]pointer().("/anyOf/0/properties/i/description"), |
| 54 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&jsonrust-analyzer cargo serde_json 1.0.151 macros/json!!("The field! (At least 1)")) |
| 55 | ); |
| 56 | assert_eqrust-analyzer cargo pretty_assertions 1.4.1 assert_eq!!(schema.pointerrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]pointer().("/anyOf/0/properties/i/default"), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 57 | assert_eqrust-analyzer cargo pretty_assertions 1.4.1 assert_eq!!(schema.pointerrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]pointer().("/anyOf/0/properties/i/minimum"), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 58 | }); |
| 59 | } |