From cd9730525bb09c25d83a6d3ac1f5dc21d782e212 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Sun, 15 Sep 2024 19:49:42 +0100 Subject: [PATCH] Port rename tests to new integration test style --- .../expected/schema-name-const-generics.json | 14 ---- .../tests/expected/schema-name-custom.json | 47 ----------- .../tests/expected/schema-name-default.json | 47 ----------- .../expected/schema-name-mixed-generics.json | 63 --------------- schemars/tests/integration/main.rs | 1 + schemars/tests/integration/schema_name.rs | 81 +++++++++++++++++++ schemars/tests/schema_name.rs | 77 ------------------ schemars_derive/src/lib.rs | 5 +- 8 files changed, 86 insertions(+), 249 deletions(-) delete mode 100644 schemars/tests/expected/schema-name-const-generics.json delete mode 100644 schemars/tests/expected/schema-name-custom.json delete mode 100644 schemars/tests/expected/schema-name-default.json delete mode 100644 schemars/tests/expected/schema-name-mixed-generics.json create mode 100644 schemars/tests/integration/schema_name.rs delete mode 100644 schemars/tests/schema_name.rs diff --git a/schemars/tests/expected/schema-name-const-generics.json b/schemars/tests/expected/schema-name-const-generics.json deleted file mode 100644 index 3c2edccb..00000000 --- a/schemars/tests/expected/schema-name-const-generics.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "const-generics-z-42", - "type": "object", - "properties": { - "foo": { - "type": "integer", - "format": "int32" - } - }, - "required": [ - "foo" - ] -} \ No newline at end of file diff --git a/schemars/tests/expected/schema-name-custom.json b/schemars/tests/expected/schema-name-custom.json deleted file mode 100644 index 70b4d983..00000000 --- a/schemars/tests/expected/schema-name-custom.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "a-new-name-Array_of_string-int32-int32", - "type": "object", - "properties": { - "t": { - "type": "integer", - "format": "int32" - }, - "u": { - "type": "null" - }, - "v": { - "type": "boolean" - }, - "w": { - "type": "array", - "items": { - "type": "string" - } - }, - "inner": { - "$ref": "#/$defs/another-new-name" - } - }, - "required": [ - "t", - "u", - "v", - "w", - "inner" - ], - "$defs": { - "another-new-name": { - "type": "object", - "properties": { - "foo": { - "type": "integer", - "format": "int32" - } - }, - "required": [ - "foo" - ] - } - } -} \ No newline at end of file diff --git a/schemars/tests/expected/schema-name-default.json b/schemars/tests/expected/schema-name-default.json deleted file mode 100644 index 2243ddd7..00000000 --- a/schemars/tests/expected/schema-name-default.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "MyStruct_for_int32_and_null_and_boolean_and_Array_of_string", - "type": "object", - "properties": { - "t": { - "type": "integer", - "format": "int32" - }, - "u": { - "type": "null" - }, - "v": { - "type": "boolean" - }, - "w": { - "type": "array", - "items": { - "type": "string" - } - }, - "inner": { - "$ref": "#/$defs/MySimpleStruct" - } - }, - "required": [ - "t", - "u", - "v", - "w", - "inner" - ], - "$defs": { - "MySimpleStruct": { - "type": "object", - "properties": { - "foo": { - "type": "integer", - "format": "int32" - } - }, - "required": [ - "foo" - ] - } - } -} \ No newline at end of file diff --git a/schemars/tests/expected/schema-name-mixed-generics.json b/schemars/tests/expected/schema-name-mixed-generics.json deleted file mode 100644 index 85c6792e..00000000 --- a/schemars/tests/expected/schema-name-mixed-generics.json +++ /dev/null @@ -1,63 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "MixedGenericStruct_for_MyStruct_for_int32_and_null_and_boolean_and_Array_of_string_and_42_and_z", - "type": "object", - "properties": { - "generic": { - "$ref": "#/$defs/MyStruct_for_int32_and_null_and_boolean_and_Array_of_string" - }, - "foo": { - "type": "integer", - "format": "int32" - } - }, - "required": [ - "generic", - "foo" - ], - "$defs": { - "MyStruct_for_int32_and_null_and_boolean_and_Array_of_string": { - "type": "object", - "properties": { - "t": { - "type": "integer", - "format": "int32" - }, - "u": { - "type": "null" - }, - "v": { - "type": "boolean" - }, - "w": { - "type": "array", - "items": { - "type": "string" - } - }, - "inner": { - "$ref": "#/$defs/MySimpleStruct" - } - }, - "required": [ - "t", - "u", - "v", - "w", - "inner" - ] - }, - "MySimpleStruct": { - "type": "object", - "properties": { - "foo": { - "type": "integer", - "format": "int32" - } - }, - "required": [ - "foo" - ] - } - } -} \ No newline at end of file diff --git a/schemars/tests/integration/main.rs b/schemars/tests/integration/main.rs index d05d3d88..2e3ae843 100644 --- a/schemars/tests/integration/main.rs +++ b/schemars/tests/integration/main.rs @@ -30,6 +30,7 @@ mod inline_subschemas; mod macros; mod remote_derive; mod same_name; +mod schema_name; #[cfg(feature = "semver1")] mod semver; #[cfg(feature = "smallvec1")] diff --git a/schemars/tests/integration/schema_name.rs b/schemars/tests/integration/schema_name.rs new file mode 100644 index 00000000..e04a7477 --- /dev/null +++ b/schemars/tests/integration/schema_name.rs @@ -0,0 +1,81 @@ +use crate::prelude::*; + +#[derive(JsonSchema, Deserialize, Serialize, Default)] +struct SimpleStruct { + foo: i32, +} + +#[derive(JsonSchema, Deserialize, Serialize, Default)] +#[schemars(rename = "new-name")] +struct RenamedSimpleStruct { + foo: i32, +} + +#[test] +fn simple() { + test!(SimpleStruct) + .custom(|schema, _| assert_eq!(schema.get("title"), Some(&"SimpleStruct".into()))); + + test!(RenamedSimpleStruct) + .custom(|schema, _| assert_eq!(schema.get("title"), Some(&"new-name".into()))); +} + +#[derive(JsonSchema, Deserialize, Serialize, Default)] +struct TypeParams { + t: T, + u: U, + v: V, + w: W, +} + +#[derive(JsonSchema, Deserialize, Serialize, Default)] +#[schemars(rename = "new-name-{W}-{T}-{T}")] +struct RenamedTypeParams { + t: T, + u: U, + v: V, + w: W, +} + +#[test] +fn type_params() { + test!(TypeParams).custom(|schema, _| { + assert_eq!( + schema.get("title"), + Some(&"TypeParams_for_uint8_and_string_and_boolean_and_null".into()) + ) + }); + + test!(RenamedTypeParams).custom(|schema, _| { + assert_eq!( + schema.get("title"), + Some(&"new-name-null-uint8-uint8".into()) + ) + }); +} + +#[derive(JsonSchema, Deserialize, Serialize, Default)] +struct ConstGeneric { + #[schemars(range(max = INT))] + foo: i32, +} + +#[derive(JsonSchema, Deserialize, Serialize, Default)] +#[schemars(rename = "new-name-{INT}")] +struct RenamedConstGeneric { + #[schemars(range(max = INT))] + foo: i32, +} + +#[test] +fn const_generics() { + test!(ConstGeneric<123, 'X'>).custom(|schema, _| { + assert_eq!( + schema.get("title"), + Some(&"ConstGeneric_for_123_and_X".into()) + ) + }); + + test!(RenamedConstGeneric<123, 'X'>) + .custom(|schema, _| assert_eq!(schema.get("title"), Some(&"new-name-123".into()))); +} diff --git a/schemars/tests/schema_name.rs b/schemars/tests/schema_name.rs deleted file mode 100644 index ebd8a528..00000000 --- a/schemars/tests/schema_name.rs +++ /dev/null @@ -1,77 +0,0 @@ -mod util; -use schemars::JsonSchema; -use util::*; - -#[allow(dead_code)] -#[derive(JsonSchema)] -struct MyStruct { - t: T, - u: U, - v: V, - w: W, - inner: MySimpleStruct, -} - -#[allow(dead_code)] -#[derive(JsonSchema)] -struct MySimpleStruct { - foo: i32, -} - -#[test] -fn default_name_multiple_type_params() -> TestResult { - test_default_generated_schema::>>("schema-name-default") -} - -#[allow(dead_code)] -#[derive(JsonSchema)] -#[serde(rename = "a-new-name-{W}-{T}-{T}")] -#[schemars(rename_all = "camelCase")] -struct MyRenamedStruct { - t: T, - u: U, - v: V, - w: W, - inner: MySimpleRenamedStruct, -} - -#[allow(dead_code)] -#[derive(JsonSchema)] -#[serde(rename = "this-attribute-is-ignored")] -#[schemars(rename = "another-new-name")] -struct MySimpleRenamedStruct { - foo: i32, -} - -#[test] -fn overriden_with_rename_multiple_type_params() -> TestResult { - test_default_generated_schema::>>( - "schema-name-custom", - ) -} - -#[allow(dead_code)] -#[derive(JsonSchema)] -#[schemars(rename = "const-generics-{BAR}-")] -struct ConstGenericStruct { - foo: i32, -} - -#[test] -fn overriden_with_rename_const_generics() -> TestResult { - test_default_generated_schema::>("schema-name-const-generics") -} - -#[allow(dead_code)] -#[derive(JsonSchema)] -struct MixedGenericStruct { - generic: T, - foo: i32, -} - -#[test] -fn default_name_mixed_generics() -> TestResult { - test_default_generated_schema::>, 42, 'z'>>( - "schema-name-mixed-generics", - ) -} diff --git a/schemars_derive/src/lib.rs b/schemars_derive/src/lib.rs index 557a276c..8d8ccd00 100644 --- a/schemars_derive/src/lib.rs +++ b/schemars_derive/src/lib.rs @@ -126,7 +126,10 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result