| 1 | #![allow(clippy::approx_constant)] |
| 2 | |
| 3 | use crate::prelude::*; |
| 4 | use pretty_assertions::assert_eq; |
| 5 | |
| 6 | static THREE: f64 = 3.0; |
| 7 | |
| 8 | #[allow(dead_code)] |
| 9 | #[derive(JsonSchema)] |
| 10 | #[schemars(extend("obj" = {"array": [null, ()]}))] |
| 11 | #[schemars(extend("3" = THREE), extend("pi" = THREE + 0.14))] |
| 12 | struct Struct { |
| 13 | #[schemars(extend("foo" = "bar"))] |
| 14 | value: Value, |
| 15 | #[schemars(extend("type" = ["number", "string"]))] |
| 16 | int: i32, |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn extend_struct() { |
| 21 | test!(Struct).assert_snapshot().custom(|schema, _| { |
| 22 | assert_eq!(schema.get("obj"), Some(&json!({ "array": [null, null] }))); |
| 23 | assert_eq!(schema.get("3"), Some(&json!(3.0))); |
| 24 | assert_eq!(schema.get("pi"), Some(&json!(3.14))); |
| 25 | assert_eq!( |
| 26 | schema.as_value().pointer("/properties/value"), |
| 27 | Some(&json!({ "foo": "bar" })) |
| 28 | ); |
| 29 | assert_eq!( |
| 30 | schema.as_value().pointer("/properties/int/type"), |
| 31 | Some(&json!(["number", "string"])) |
| 32 | ); |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | #[allow(dead_code)] |
| 37 | #[derive(JsonSchema)] |
| 38 | #[schemars(extend("obj" = {"array": [null, ()]}))] |
| 39 | #[schemars(extend("3" = THREE), extend("pi" = THREE + 0.14))] |
| 40 | struct TupleStruct( |
| 41 | #[schemars(extend("foo" = "bar"))] Value, |
| 42 | #[schemars(extend("type" = ["number", "string"]))] usize, |
| 43 | ); |
| 44 | |
| 45 | #[test] |
| 46 | fn extend_tuple_struct() { |
| 47 | test!(TupleStruct).assert_snapshot().custom(|schema, _| { |
| 48 | assert_eq!(schema.get("obj"), Some(&json!({ "array": [null, null] }))); |
| 49 | assert_eq!(schema.get("3"), Some(&json!(3.0))); |
| 50 | assert_eq!(schema.get("pi"), Some(&json!(3.14))); |
| 51 | assert_eq!( |
| 52 | schema.as_value().pointer("/prefixItems/0"), |
| 53 | Some(&json!({ "foo": "bar" })) |
| 54 | ); |
| 55 | assert_eq!( |
| 56 | schema.as_value().pointer("/prefixItems/1/type"), |
| 57 | Some(&json!(["number", "string"])) |
| 58 | ); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | #[allow(dead_code)] |
| 63 | #[derive(JsonSchema)] |
| 64 | #[schemars(extend("foo" = "bar"))] |
| 65 | enum ExternalEnum { |
| 66 | #[schemars(extend("foo" = "bar"))] |
| 67 | Unit, |
| 68 | #[schemars(extend("foo" = "bar"))] |
| 69 | NewType(Value), |
| 70 | #[schemars(extend("foo" = "bar"))] |
| 71 | Tuple(i32, bool), |
| 72 | #[schemars(extend("foo" = "bar"))] |
| 73 | Struct { i: i32, b: bool }, |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn extend_externally_tagged_enum() { |
| 78 | test!(ExternalEnum).assert_snapshot().custom(|schema, _| { |
| 79 | assert_eq!(schema.get("foo"), Some(&json!("bar"))); |
| 80 | |
| 81 | for i in 0..4 { |
| 82 | assert_eq!( |
| 83 | schema.as_value().pointer(&format!("/oneOf/{i}/foo")), |
| 84 | Some(&json!("bar")) |
| 85 | ); |
| 86 | } |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | #[allow(dead_code)] |
| 91 | #[derive(JsonSchema)] |
| 92 | #[schemars(tag = "t", extend("foo" = "bar"))] |
| 93 | enum InternalEnum { |
| 94 | #[schemars(extend("foo" = "bar"))] |
| 95 | Unit, |
| 96 | #[schemars(extend("foo" = "bar"))] |
| 97 | NewType(Value), |
| 98 | #[schemars(extend("foo" = "bar"))] |
| 99 | Struct { i: i32, b: bool }, |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn extend_internally_tagged_enum() { |
| 104 | test!(InternalEnum).assert_snapshot().custom(|schema, _| { |
| 105 | assert_eq!(schema.get("foo"), Some(&json!("bar"))); |
| 106 | |
| 107 | for i in 0..3 { |
| 108 | assert_eq!( |
| 109 | schema.as_value().pointer(&format!("/oneOf/{i}/foo")), |
| 110 | Some(&json!("bar")) |
| 111 | ); |
| 112 | } |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | #[allow(dead_code)] |
| 117 | #[derive(JsonSchema)] |
| 118 | #[schemars(tag = "t", content = "c", extend("foo" = "bar"))] |
| 119 | enum AdjacentEnum { |
| 120 | #[schemars(extend("foo" = "bar"))] |
| 121 | Unit, |
| 122 | #[schemars(extend("foo" = "bar"))] |
| 123 | NewType(Value), |
| 124 | #[schemars(extend("foo" = "bar"))] |
| 125 | Tuple(i32, bool), |
| 126 | #[schemars(extend("foo" = "bar"))] |
| 127 | Struct { i: i32, b: bool }, |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn extend_adjacently_tagged_enum() { |
| 132 | test!(AdjacentEnum).assert_snapshot().custom(|schema, _| { |
| 133 | assert_eq!(schema.get("foo"), Some(&json!("bar"))); |
| 134 | |
| 135 | for i in 0..4 { |
| 136 | assert_eq!( |
| 137 | schema.as_value().pointer(&format!("/oneOf/{i}/foo")), |
| 138 | Some(&json!("bar")) |
| 139 | ); |
| 140 | } |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | #[allow(dead_code)] |
| 145 | #[derive(JsonSchema)] |
| 146 | #[schemars(untagged, extend("foo" = "bar"))] |
| 147 | enum UntaggedEnum { |
| 148 | #[schemars(extend("foo" = "bar"))] |
| 149 | Unit, |
| 150 | #[schemars(extend("foo" = "bar"))] |
| 151 | NewType(Value), |
| 152 | #[schemars(extend("foo" = "bar"))] |
| 153 | Tuple(i32, bool), |
| 154 | #[schemars(extend("foo" = "bar"))] |
| 155 | Struct { i: i32, b: bool }, |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn extend_untagged_enum() { |
| 160 | test!(UntaggedEnum).assert_snapshot().custom(|schema, _| { |
| 161 | assert_eq!(schema.get("foo"), Some(&json!("bar"))); |
| 162 | |
| 163 | for i in 0..4 { |
| 164 | assert_eq!( |
| 165 | schema.as_value().pointer(&format!("/anyOf/{i}/foo")), |
| 166 | Some(&json!("bar")) |
| 167 | ); |
| 168 | } |
| 169 | }); |
| 170 | } |